py_ali_subtitle.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. if not self.login():
  41. return {}
  42. ids = id.split('+')
  43. shareId = ids[0]
  44. shareToken = ids[1]
  45. fileId = ids[2]
  46. category = ids[3]
  47. subtitle = ids[4]
  48. url = self.getDownloadUrl(shareId,shareToken,fileId,category)
  49. noRsp = requests.get(url,headers=self.header, allow_redirects=False,verify = False)
  50. realUrl = ''
  51. if 'Location' in noRsp.headers:
  52. realUrl = noRsp.headers['Location']
  53. if 'location' in noRsp.headers and len(realUrl) == 0 :
  54. realUrl = noRsp.headers['location']
  55. newHeader = {
  56. "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",
  57. "referer":"https://www.aliyundrive.com/",
  58. }
  59. subtitleUrl = self.subtitleContent(id)
  60. result = {
  61. 'parse':'0',
  62. 'playUrl':'',
  63. 'url':realUrl,
  64. 'header':newHeader,
  65. 'subt':subtitleUrl
  66. }
  67. return result
  68. def subtitleContent(self,id):
  69. ids = id.split('+')
  70. shareId = ids[0]
  71. shareToken = ids[1]
  72. fileId = ids[2]
  73. category = ids[3]
  74. subtitle = ids[4]
  75. if len(subtitle) == 0:
  76. return ""
  77. customHeader = self.header.copy()
  78. customHeader['x-share-token'] = shareToken
  79. customHeader['authorization'] = self.authorization
  80. jo = {
  81. "expire_sec": 600,
  82. "share_id": shareId,
  83. "file_id": subtitle,
  84. "image_url_process": "image/resize,w_1920/format,jpeg",
  85. "image_thumbnail_process": "image/resize,w_1920/format,jpeg",
  86. "get_streams_url": True
  87. # ,
  88. # "drive_id": "183237630"
  89. }
  90. downloadUrl = 'https://api.aliyundrive.com/v2/file/get_share_link_download_url'
  91. resultJo = requests.post(downloadUrl,json = jo,headers=customHeader).json()
  92. print(resultJo)
  93. noRsp = requests.get(resultJo['download_url'],headers=self.header, allow_redirects=False,verify = False)
  94. realUrl = ''
  95. if 'Location' in noRsp.headers:
  96. realUrl = noRsp.headers['Location']
  97. if 'location' in noRsp.headers and len(realUrl) == 0 :
  98. realUrl = noRsp.headers['location']
  99. return realUrl
  100. def originContent(self,flag,id,vipFlags):
  101. if not self.login():
  102. return {}
  103. ids = id.split('+')
  104. shareId = ids[0]
  105. shareToken = ids[1]
  106. fileId = ids[2]
  107. subtitle = ids[4]
  108. url = '{0}?do=push_agent&api=python&type=m3u8&share_id={1}&file_id={2}'.format(self.localProxyUrl,shareId,fileId)
  109. subtitleUrl = self.subtitleContent(id)
  110. newHeader = {
  111. "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",
  112. "referer":"https://www.aliyundrive.com/",
  113. }
  114. result = {
  115. 'parse':'0',
  116. 'playUrl':'',
  117. 'url':url,
  118. 'header':newHeader,
  119. 'subt':subtitleUrl
  120. }
  121. return result
  122. def detailContent(self,array):
  123. tid = array[0]
  124. m = re.search('www.aliyundrive.com\\/s\\/([^\\/]+)(\\/folder\\/([^\\/]+))?', tid)
  125. col = m.groups()
  126. shareId = col[0]
  127. fileId = col[2]
  128. infoUrl = 'https://api.aliyundrive.com/adrive/v3/share_link/get_share_by_anonymous'
  129. infoForm = {'share_id':shareId}
  130. infoRsp = requests.post(infoUrl,json = infoForm,headers=self.header)
  131. infoJo = json.loads(infoRsp.text)
  132. infoJa = []
  133. if 'file_infos' in infoJo:
  134. infoJa = infoJo['file_infos']
  135. if len(infoJa) <= 0 :
  136. return ''
  137. fileInfo = {}
  138. fileInfo = infoJa[0]
  139. if fileId == None or len(fileId) <= 0:
  140. fileId = fileInfo['file_id']
  141. vodList = {
  142. 'vod_id':tid,
  143. 'vod_name':infoJo['share_name'],
  144. 'vod_pic':infoJo['avatar'],
  145. 'vod_content':tid,
  146. 'vod_play_from':'AliYun$$$AliYun原画'
  147. }
  148. fileType = fileInfo['type']
  149. if fileType != 'folder':
  150. if fileType != 'file' or fileInfo['category'] != video:
  151. return ''
  152. fileId = 'root'
  153. shareToken = self.getToken(shareId,'')
  154. hashMap = {}
  155. self.listFiles(hashMap,shareId,shareToken,fileId)
  156. sortedMap = sorted(hashMap.items(), key=lambda x: x[0])
  157. arrayList = []
  158. playList = []
  159. for sm in sortedMap:
  160. arrayList.append(sm[0]+'$'+sm[1])
  161. playList.append('#'.join(arrayList))
  162. playList.append('#'.join(arrayList))
  163. vodList['vod_play_url'] = '$$$'.join(playList)
  164. result = {
  165. 'list':[vodList]
  166. }
  167. return result
  168. authorization = ''
  169. timeoutTick = 0
  170. localTime = 0
  171. expiresIn = 0
  172. shareTokenMap = {}
  173. expiresMap = {}
  174. localMedia = {}
  175. header = {
  176. "Referer":"https://www.aliyundrive.com/",
  177. "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"
  178. }
  179. localProxyUrl = 'http://127.0.0.1:UndCover/proxy'
  180. def redirectResponse(tUrl):
  181. rsp = requests.get(tUrl, allow_redirects=False,verify = False)
  182. if 'Location' in rsp.headers:
  183. return redirectResponse(rsp.headers['Location'])
  184. else:
  185. return rsp
  186. def getDownloadUrl(self,shareId,token,fileId,category):
  187. lShareId = shareId
  188. lFileId = fileId
  189. params = {
  190. "share_id": lShareId,
  191. "category": "live_transcoding",
  192. "file_id": lFileId,
  193. "template_id": ""
  194. }
  195. customHeader = self.header.copy()
  196. customHeader['x-share-token'] = token
  197. customHeader['authorization'] = self.authorization
  198. url = 'https://api.aliyundrive.com/v2/file/get_share_link_video_preview_play_info'
  199. if category == 'video':
  200. rsp = requests.post(url,json = params,headers=customHeader)
  201. rspJo = json.loads(rsp.text)
  202. lShareId = rspJo['share_id']
  203. lFileId = rspJo['file_id']
  204. jo = {
  205. }
  206. if category == 'video':
  207. jo['share_id'] = lShareId
  208. jo['file_id'] = lFileId
  209. jo['expire_sec'] = 600
  210. if category == 'audio':
  211. jo['share_id'] = lShareId
  212. jo['file_id'] = lFileId
  213. jo['get_audio_play_info'] = True
  214. downloadUrl = 'https://api.aliyundrive.com/v2/file/get_share_link_download_url'
  215. downloadRsp = requests.post(downloadUrl,json = jo,headers=customHeader)
  216. resultJo = json.loads(downloadRsp.text)
  217. return resultJo['download_url']
  218. def getMediaSlice(self,shareId,token,fileId):
  219. params = {
  220. "share_id": shareId,
  221. "category": "live_transcoding",
  222. "file_id": fileId,
  223. "template_id": ""
  224. }
  225. customHeader = self.header.copy()
  226. customHeader['x-share-token'] = token
  227. customHeader['authorization'] = self.authorization
  228. url = 'https://api.aliyundrive.com/v2/file/get_share_link_video_preview_play_info'
  229. rsp = requests.post(url,json = params,headers=customHeader)
  230. rspJo = json.loads(rsp.text)
  231. quality = ['FHD','HD','SD']
  232. videoList = rspJo['video_preview_play_info']['live_transcoding_task_list']
  233. highUrl = ''
  234. for q in quality:
  235. if len(highUrl) > 0:
  236. break
  237. for video in videoList:
  238. if(video['template_id'] == q):
  239. highUrl = video['url']
  240. break
  241. if len(highUrl) == 0:
  242. highUrl = videoList[0]['url']
  243. noRsp = requests.get(highUrl,headers=self.header, allow_redirects=False,verify = False)
  244. m3u8Url = ''
  245. if 'Location' in noRsp.headers:
  246. m3u8Url = noRsp.headers['Location']
  247. if 'location' in noRsp.headers and len(m3u8Url) == 0 :
  248. m3u8Url = noRsp.headers['location']
  249. m3u8Rsp = requests.get(m3u8Url,headers=self.header)
  250. m3u8Content = m3u8Rsp.text
  251. tmpArray = m3u8Url.split('/')[0:-1]
  252. host = '/'.join(tmpArray) + '/'
  253. m3u8List = []
  254. mediaMap = {}
  255. slices = m3u8Content.split("\n")
  256. count = 0
  257. for slice in slices:
  258. tmpSlice = slice
  259. if 'x-oss-expires' in tmpSlice:
  260. count = count + 1
  261. mediaMap[str(count)] = host+tmpSlice
  262. tmpSlice = "{0}?do=push_agent&api=python&type=media&share_id={1}&file_id={2}&media_id={3}".format(self.localProxyUrl,shareId,fileId,count)
  263. m3u8List.append(tmpSlice)
  264. self.localMedia[fileId] = mediaMap
  265. return '\n'.join(m3u8List)
  266. def proxyMedia(self,map):
  267. shareId = map['share_id']
  268. fileId = map['file_id']
  269. mediaId = map['media_id']
  270. shareToken = self.getToken(shareId,'')
  271. refresh = False
  272. url = ''
  273. ts = 0
  274. if fileId in self.localMedia:
  275. fileMap = self.localMedia[fileId]
  276. if mediaId in fileMap:
  277. url = fileMap[mediaId]
  278. if len(url) > 0:
  279. ts = int(self.regStr(url,"x-oss-expires=(\\d+)&"))
  280. self.localTime = int(time.time())
  281. if ts - self.localTime <= 60:
  282. self.getMediaSlice(shareId,shareToken,fileId)
  283. url = self.localMedia[fileId][mediaId]
  284. action = {
  285. 'url':url,
  286. 'header':self.header,
  287. 'param':'',
  288. 'type':'stream',
  289. 'after':''
  290. }
  291. return [200, "video/MP2T", action, ""]
  292. def proxyM3U8(self,map):
  293. shareId = map['share_id']
  294. fileId = map['file_id']
  295. shareToken = self.getToken(shareId,'')
  296. content = self.getMediaSlice(shareId,shareToken,fileId)
  297. action = {
  298. 'url':'',
  299. 'header':'',
  300. 'param':'',
  301. 'type':'string',
  302. 'after':''
  303. }
  304. return [200, "application/octet-stream", action, content]
  305. def localProxy(self,param):
  306. if not self.login():
  307. return {}
  308. typ = param['type']
  309. if typ == "m3u8":
  310. return self.proxyM3U8(param)
  311. if typ == "media":
  312. return self.proxyMedia(param)
  313. return None
  314. def getToken(self,shareId,sharePwd):
  315. self.localTime = int(time.time())
  316. shareToken = ''
  317. if shareId in self.shareTokenMap:
  318. shareToken = self.shareTokenMap[shareId]
  319. # todo
  320. expire = self.expiresMap[shareId]
  321. if len(shareToken) > 0 and expire - self.localTime > 600:
  322. return shareToken
  323. params = {
  324. 'share_id':shareId,
  325. 'share_pwd':sharePwd
  326. }
  327. url = 'https://api.aliyundrive.com/v2/share_link/get_share_token'
  328. rsp = requests.post(url,json = params,headers=self.header)
  329. jo = json.loads(rsp.text)
  330. newShareToken = jo['share_token']
  331. self.expiresMap[shareId] = self.localTime + int(jo['expires_in'])
  332. self.shareTokenMap[shareId] = newShareToken
  333. # print(self.expiresMap)
  334. # print(self.shareTokenMap)
  335. return newShareToken
  336. def listFiles(self,map,shareId,shareToken,fileId,subtitle={}):
  337. url = 'https://api.aliyundrive.com/adrive/v3/file/list'
  338. newHeader = self.header.copy()
  339. newHeader['x-share-token'] = shareToken
  340. params = {
  341. 'image_thumbnail_process':'image/resize,w_160/format,jpeg',
  342. 'image_url_process':'image/resize,w_1920/format,jpeg',
  343. 'limit':200,
  344. 'order_by':'updated_at',
  345. 'order_direction':'DESC',
  346. 'parent_file_id':fileId,
  347. 'share_id':shareId,
  348. 'video_thumbnail_process':'video/snapshot,t_1000,f_jpg,ar_auto,w_300'
  349. }
  350. maker = ''
  351. arrayList = []
  352. for i in range(1,51):
  353. if i >= 2 and len(maker) == 0:
  354. break
  355. params['marker'] = maker
  356. rsp = requests.post(url,json = params,headers=newHeader)
  357. jo = json.loads(rsp.text)
  358. ja = jo['items']
  359. for jt in ja:
  360. if jt['type'] == 'folder':
  361. arrayList.append(jt['file_id'])
  362. else:
  363. if 'video' in jt['mime_type'] or 'video' in jt['category']:
  364. repStr = jt['name'].replace("#", "_").replace("$", "_").replace(jt['file_extension'],'')[0:-1]
  365. map[repStr] = shareId + "+" + shareToken + "+" + jt['file_id'] + "+" + jt['category'] + "+"
  366. elif 'others' == jt['category'] and ('srt' == jt['file_extension'] or 'ass' == jt['file_extension']):
  367. repStr = jt['name'].replace("#", "_").replace("$", "_").replace(jt['file_extension'],'')[0:-1]
  368. subtitle[repStr] = jt['file_id']
  369. maker = jo['next_marker']
  370. i = i + 1
  371. for item in arrayList:
  372. self.listFiles(map,shareId,shareToken,item,subtitle)
  373. for key in map.keys():
  374. for subKey in subtitle.keys():
  375. if key in subKey and map[key][-1] == "+":
  376. map[key]=map[key]+subtitle[subKey]
  377. break
  378. def login(self):
  379. self.localTime = int(time.time())
  380. url = 'https://api.aliyundrive.com/token/refresh'
  381. if len(self.authorization) == 0 or self.timeoutTick - self.localTime <= 600:
  382. form = {
  383. 'refresh_token':'ca2ff3cb92bd4522851b8d895e7f205b'
  384. }
  385. if len(self.extend) > 0:
  386. form['refresh_token'] = self.extend
  387. rsp = requests.post(url,json = form,headers=self.header)
  388. jo = json.loads(rsp.text)
  389. if rsp.status_code == 200:
  390. self.authorization = jo['token_type'] + ' ' + jo['access_token']
  391. self.expiresIn = int(jo['expires_in'])
  392. self.timeoutTick = self.localTime + self.expiresIn
  393. return True
  394. return False
  395. else:
  396. return True
  397. # print(self.authorization)
  398. # print(self.timeoutTick)
  399. # print(self.localTime)
  400. # print(self.expiresIn)