8
8
9
9
import Foundation
10
10
import Marshal
11
+ import SwiftHTTP
11
12
12
13
class Processes : NSObject {
13
14
@@ -82,6 +83,11 @@ class Processes: NSObject {
82
83
task. standardOutput = pipe
83
84
var mpvArgs = [ " \( MPVOption . Miscellaneous. forceMediaTitle) = \( title) " ]
84
85
86
+ if url. contains ( " douyu " ) {
87
+ mpvArgs. append ( contentsOf: [ MPVOption . Network. cookies,
88
+ " \( MPVOption . Network. cookiesFile) = \( getCookies ( for: . douyu) ) " ] )
89
+ }
90
+
85
91
switch Preferences . shared. livePlayer {
86
92
case . iina:
87
93
task. launchPath = Preferences . shared. livePlayer. rawValue
@@ -103,3 +109,42 @@ class Processes: NSObject {
103
109
104
110
105
111
}
112
+
113
+ private extension Processes {
114
+
115
+ func getCookies( for website: LiveSupportList ) -> String {
116
+ switch website {
117
+ case . douyu:
118
+ let douyuCookie = " https://passport.douyu.com/lapi/did/api/get "
119
+ let time = UInt32 ( NSDate ( ) . timeIntervalSinceReferenceDate)
120
+ srand48 ( Int ( time) )
121
+ let random = " \( drand48 ( ) ) "
122
+ let parameters = [ " client_id " : " 1 " ,
123
+ " callback " : ( " jsonp_ " + random) . replacingOccurrences ( of: " . " , with: " " ) ]
124
+ let headers = [ " Referer " : " http://www.douyu.com " ]
125
+
126
+ let httpSemaphore = DispatchSemaphore ( value: 0 )
127
+ var cookiesString = " "
128
+
129
+ HTTP . GET ( douyuCookie, parameters: parameters, headers: headers) { response in
130
+ do {
131
+ var str = response. text
132
+ str = str? . subString ( from: " ( " , to: " ) " )
133
+ let json = try JSONParser . JSONObjectWithData ( str? . data ( using: . utf8) ?? Data ( ) )
134
+ let didStr : String = try json. value ( for: " data.did " )
135
+ cookiesString = """
136
+ ..douyu.com TRUE / FALSE 1535865698 dy_did \( didStr)
137
+ .www.douyu.com TRUE / FALSE 1535865771 acf_did \( didStr)
138
+ """
139
+ } catch let error {
140
+ print ( error)
141
+ }
142
+ httpSemaphore. signal ( )
143
+ }
144
+ httpSemaphore. wait ( )
145
+ return cookiesString
146
+ default :
147
+ return " "
148
+ }
149
+ }
150
+ }
0 commit comments