py_ali_subtitle.py 17 KB

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