py_ali.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #coding=utf-8
  2. #!/usr/bin/python
  3. import sys
  4. sys.path.append('..')
  5. from base.spider import Spider
  6. import json
  7. import requests
  8. import time
  9. import re
  10. class Spider(Spider): # 元类 默认的元类 type
  11. def getName(self):
  12. return "阿里云盘"
  13. def init(self,extend=""):
  14. print("============{0}============".format(extend))
  15. pass
  16. def homeContent(self,filter):
  17. result = {}
  18. return result
  19. def homeVideoContent(self):
  20. result = {}
  21. return result
  22. def categoryContent(self,tid,pg,filter,extend):
  23. result = {}
  24. return result
  25. def searchContent(self,key,quick):
  26. result = {}
  27. return result
  28. def isVideoFormat(self,url):
  29. pass
  30. def manualVideoCheck(self):
  31. pass
  32. def playerContent(self,flag,id,vipFlags):
  33. if flag == 'AliYun':
  34. return self.originContent(flag,id,vipFlags)
  35. elif flag == 'AliYun原画':
  36. return self.fhdContent(flag,id,vipFlags)
  37. else:
  38. return {}
  39. def fhdContent(self,flag,id,vipFlags):
  40. self.login()
  41. ids = id.split('+')
  42. shareId = ids[0]
  43. shareToken = ids[1]
  44. fileId = ids[2]
  45. category = ids[3]
  46. url = self.getDownloadUrl(shareId,shareToken,fileId,category)
  47. print(url)
  48. noRsp = requests.get(url,headers=self.header, allow_redirects=False,verify = False)
  49. realUrl = ''
  50. if 'Location' in noRsp.headers:
  51. realUrl = noRsp.headers['Location']
  52. if 'location' in noRsp.headers and len(realUrl) == 0 :
  53. realUrl = noRsp.headers['location']
  54. newHeader = {
  55. "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36",
  56. "referer":"https://www.aliyundrive.com/",
  57. }
  58. result = {
  59. 'parse':'0',
  60. 'playUrl':'',
  61. 'url':realUrl,
  62. 'header':newHeader
  63. }
  64. return result
  65. def originContent(self,flag,id,vipFlags):
  66. self.login()
  67. ids = id.split('+')
  68. shareId = ids[0]
  69. shareToken = ids[1]
  70. fileId = ids[2]
  71. url = '{0}?do=push_agent&api=python&type=m3u8&share_id={1}&file_id={2}'.format(self.localProxyUrl,shareId,fileId)
  72. result = {
  73. 'parse':'0',
  74. 'playUrl':'',
  75. 'url':url,
  76. 'header':''
  77. }
  78. # shareToken = self.getToken(shareId,'')
  79. # self.getMediaSlice(shareId,shareToken,fileId)
  80. # map = {
  81. # 'share_id':'p1GJYEqgeb2',
  82. # 'file_id':'62ed1b95b1048d60ffc246669f5e0999e90b8c2f',
  83. # 'media_id':'1'
  84. # }
  85. # self.proxyMedia(map)
  86. return result
  87. def detailContent(self,array):
  88. tid = array[0]
  89. # shareId = self.regStr(href,'www.aliyundrive.com\\/s\\/([^\\/]+)(\\/folder\\/([^\\/]+))?')
  90. # todo =========================================================================================
  91. m = re.search('www.aliyundrive.com\\/s\\/([^\\/]+)(\\/folder\\/([^\\/]+))?', tid)
  92. col = m.groups()
  93. shareId = col[0]
  94. fileId = col[2]
  95. infoUrl = 'https://api.aliyundrive.com/adrive/v3/share_link/get_share_by_anonymous'
  96. infoForm = {'share_id':shareId}
  97. infoRsp = requests.post(infoUrl,json = infoForm,headers=self.header)
  98. infoJo = json.loads(infoRsp.text)
  99. infoJa = []
  100. if 'file_infos' in infoJo:
  101. infoJa = infoJo['file_infos']
  102. if len(infoJa) <= 0 :
  103. return ''
  104. fileInfo = {}
  105. # todo
  106. fileInfo = infoJa[0]
  107. print(fileId)
  108. if fileId == None or len(fileId) <= 0:
  109. fileId = fileInfo['file_id']
  110. vodList = {
  111. 'vod_id':tid,
  112. 'vod_name':infoJo['share_name'],
  113. 'vod_pic':infoJo['avatar'],
  114. 'vod_content':tid,
  115. 'vod_play_from':'AliYun$$$AliYun原画'
  116. }
  117. fileType = fileInfo['type']
  118. if fileType != 'folder':
  119. if fileType != 'file' or fileInfo['category'] != video:
  120. return ''
  121. fileId = 'root'
  122. shareToken = self.getToken(shareId,'')
  123. hashMap = {}
  124. self.listFiles(hashMap,shareId,shareToken,fileId)
  125. if not hashMap:
  126. return ''
  127. sortedMap = sorted(hashMap.items(), key=lambda x: x[0])
  128. arrayList = []
  129. playList = []
  130. for sm in sortedMap:
  131. arrayList.append(sm[0]+'$'+sm[1])
  132. playList.append('#'.join(arrayList))
  133. playList.append('#'.join(arrayList))
  134. vodList['vod_play_url'] = '$$$'.join(playList)
  135. result = {
  136. 'list':[vodList]
  137. }
  138. return result
  139. authorization = ''
  140. timeoutTick = 0
  141. localTime = 0
  142. expiresIn = 0
  143. shareTokenMap = {}
  144. expiresMap = {}
  145. localMedia = {}
  146. header = {
  147. "Referer":"https://www.aliyundrive.com/",
  148. "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36"
  149. }
  150. localProxyUrl = 'http://127.0.0.1:UndCover/proxy'
  151. def redirectResponse(tUrl):
  152. rsp = requests.get(tUrl, allow_redirects=False,verify = False)
  153. if 'Location' in rsp.headers:
  154. return redirectResponse(rsp.headers['Location'])
  155. else:
  156. return rsp
  157. def getDownloadUrl(self,shareId,token,fileId,category):
  158. lShareId = shareId
  159. lFileId = fileId
  160. params = {
  161. "share_id": lShareId,
  162. "category": "live_transcoding",
  163. "file_id": lFileId,
  164. "template_id": ""
  165. }
  166. customHeader = self.header.copy()
  167. customHeader['x-share-token'] = token
  168. customHeader['authorization'] = self.authorization
  169. url = 'https://api.aliyundrive.com/v2/file/get_share_link_video_preview_play_info'
  170. if category == 'video':
  171. rsp = requests.post(url,json = params,headers=customHeader)
  172. rspJo = json.loads(rsp.text)
  173. lShareId = rspJo['share_id']
  174. lFileId = rspJo['file_id']
  175. jo = {
  176. }
  177. if category == 'video':
  178. jo['share_id'] = lShareId
  179. jo['file_id'] = lFileId
  180. jo['expire_sec'] = 600
  181. if category == 'audio':
  182. jo['share_id'] = lShareId
  183. jo['file_id'] = lFileId
  184. jo['get_audio_play_info'] = True
  185. downloadUrl = 'https://api.aliyundrive.com/v2/file/get_share_link_download_url'
  186. downloadRsp = requests.post(downloadUrl,json = jo,headers=customHeader)
  187. resultJo = json.loads(downloadRsp.text)
  188. return resultJo['download_url']
  189. def getMediaSlice(self,shareId,token,fileId):
  190. params = {
  191. "share_id": shareId,
  192. "category": "live_transcoding",
  193. "file_id": fileId,
  194. "template_id": ""
  195. }
  196. customHeader = self.header.copy()
  197. customHeader['x-share-token'] = token
  198. customHeader['authorization'] = self.authorization
  199. url = 'https://api.aliyundrive.com/v2/file/get_share_link_video_preview_play_info'
  200. rsp = requests.post(url,json = params,headers=customHeader)
  201. rspJo = json.loads(rsp.text)
  202. quality = ['FHD','HD','SD']
  203. videoList = rspJo['video_preview_play_info']['live_transcoding_task_list']
  204. highUrl = ''
  205. for q in quality:
  206. if len(highUrl) > 0:
  207. break
  208. for video in videoList:
  209. if(video['template_id'] == q):
  210. highUrl = video['url']
  211. break
  212. if len(highUrl) == 0:
  213. highUrl = videoList[0]['url']
  214. noRsp = requests.get(highUrl,headers=self.header, allow_redirects=False,verify = False)
  215. m3u8Url = ''
  216. if 'Location' in noRsp.headers:
  217. m3u8Url = noRsp.headers['Location']
  218. if 'location' in noRsp.headers and len(m3u8Url) == 0 :
  219. m3u8Url = noRsp.headers['location']
  220. m3u8Rsp = requests.get(m3u8Url,headers=self.header)
  221. m3u8Content = m3u8Rsp.text
  222. tmpArray = m3u8Url.split('/')[0:-1]
  223. host = '/'.join(tmpArray) + '/'
  224. m3u8List = []
  225. mediaMap = {}
  226. slices = m3u8Content.split("\n")
  227. count = 0
  228. for slice in slices:
  229. tmpSlice = slice
  230. if 'x-oss-expires' in tmpSlice:
  231. count = count + 1
  232. mediaMap[str(count)] = host+tmpSlice
  233. tmpSlice = "{0}?do=push_agent&api=python&type=media&share_id={1}&file_id={2}&media_id={3}".format(self.localProxyUrl,shareId,fileId,count)
  234. m3u8List.append(tmpSlice)
  235. self.localMedia[fileId] = mediaMap
  236. return '\n'.join(m3u8List)
  237. def proxyMedia(self,map):
  238. shareId = map['share_id']
  239. fileId = map['file_id']
  240. mediaId = map['media_id']
  241. shareToken = self.getToken(shareId,'')
  242. refresh = False
  243. url = ''
  244. ts = 0
  245. if fileId in self.localMedia:
  246. fileMap = self.localMedia[fileId]
  247. if mediaId in fileMap:
  248. url = fileMap[mediaId]
  249. if len(url) > 0:
  250. ts = int(self.regStr(url,"x-oss-expires=(\\d+)&"))
  251. # url = self.localMedia[fileId][mediaId]
  252. # ts = int(self.regStr(url,"x-oss-expires=(\\d+)&"))
  253. self.localTime = int(time.time())
  254. if ts - self.localTime <= 60:
  255. self.getMediaSlice(shareId,shareToken,fileId)
  256. url = self.localMedia[fileId][mediaId]
  257. action = {
  258. 'url':url,
  259. 'header':self.header,
  260. 'param':'',
  261. 'type':'stream',
  262. 'after':''
  263. }
  264. print(action)
  265. return [200, "video/MP2T", action, ""]
  266. def proxyM3U8(self,map):
  267. shareId = map['share_id']
  268. fileId = map['file_id']
  269. shareToken = self.getToken(shareId,'')
  270. content = self.getMediaSlice(shareId,shareToken,fileId)
  271. action = {
  272. 'url':'',
  273. 'header':'',
  274. 'param':'',
  275. 'type':'string',
  276. 'after':''
  277. }
  278. return [200, "application/octet-stream", action, content]
  279. def localProxy(self,param):
  280. self.login()
  281. typ = param['type']
  282. if typ == "m3u8":
  283. return self.proxyM3U8(param)
  284. if typ == "media":
  285. return self.proxyMedia(param)
  286. return None
  287. def getToken(self,shareId,sharePwd):
  288. self.localTime = int(time.time())
  289. shareToken = ''
  290. if shareId in self.shareTokenMap:
  291. shareToken = self.shareTokenMap[shareId]
  292. # todo
  293. expire = self.expiresMap[shareId]
  294. if len(shareToken) > 0 and expire - self.localTime > 600:
  295. return shareToken
  296. params = {
  297. 'share_id':shareId,
  298. 'share_pwd':sharePwd
  299. }
  300. url = 'https://api.aliyundrive.com/v2/share_link/get_share_token'
  301. rsp = requests.post(url,json = params,headers=self.header)
  302. jo = json.loads(rsp.text)
  303. newShareToken = jo['share_token']
  304. self.expiresMap[shareId] = self.localTime + int(jo['expires_in'])
  305. self.shareTokenMap[shareId] = newShareToken
  306. print(self.expiresMap)
  307. print(self.shareTokenMap)
  308. return newShareToken
  309. def listFiles(self,map,shareId,shareToken,fileId):
  310. url = 'https://api.aliyundrive.com/adrive/v3/file/list'
  311. newHeader = self.header.copy()
  312. newHeader['x-share-token'] = shareToken
  313. params = {
  314. 'image_thumbnail_process':'image/resize,w_160/format,jpeg',
  315. 'image_url_process':'image/resize,w_1920/format,jpeg',
  316. 'limit':200,
  317. 'order_by':'updated_at',
  318. 'order_direction':'DESC',
  319. 'parent_file_id':fileId,
  320. 'share_id':shareId,
  321. 'video_thumbnail_process':'video/snapshot,t_1000,f_jpg,ar_auto,w_300'
  322. }
  323. maker = ''
  324. arrayList = []
  325. for i in range(1,51):
  326. if i >= 2 and len(maker) == 0:
  327. break
  328. params['marker'] = maker
  329. rsp = requests.post(url,json = params,headers=newHeader)
  330. jo = json.loads(rsp.text)
  331. ja = jo['items']
  332. for jt in ja:
  333. if jt['type'] == 'folder':
  334. arrayList.append(jt['file_id'])
  335. else:
  336. if 'video' in jt['mime_type'] or 'video' in jt['category']:
  337. repStr = jt['name'].replace("#", "_").replace("$", "_")
  338. map[repStr] = shareId + "+" + shareToken + "+" + jt['file_id'] + "+" + jt['category']
  339. # print(repStr,shareId + "+" + shareToken + "+" + jt['file_id'])
  340. maker = jo['next_marker']
  341. i = i + 1
  342. for item in arrayList:
  343. self.listFiles(map,shareId,shareToken,item)
  344. def login(self):
  345. self.localTime = int(time.time())
  346. url = 'https://api.aliyundrive.com/token/refresh'
  347. if len(self.authorization) == 0 or self.timeoutTick - self.localTime <= 600:
  348. form = {
  349. 'refresh_token':'4acb3ad2f2254ba1b566279f7cd98ba3'
  350. }
  351. if len(self.extend) > 0:
  352. form['refresh_token'] = self.extend
  353. print(form)
  354. rsp = requests.post(url,json = form,headers=self.header)
  355. jo = json.loads(rsp.text)
  356. print(jo)
  357. self.authorization = jo['token_type'] + ' ' + jo['access_token']
  358. self.expiresIn = int(jo['expires_in'])
  359. self.timeoutTick = self.localTime + self.expiresIn
  360. # print(self.authorization)
  361. # print(self.timeoutTick)
  362. # print(self.localTime)
  363. # print(self.expiresIn)