py_ali.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. sortedMap = sorted(hashMap.items(), key=lambda x: x[0])
  126. arrayList = []
  127. playList = []
  128. for sm in sortedMap:
  129. arrayList.append(sm[0]+'$'+sm[1])
  130. playList.append('#'.join(arrayList))
  131. playList.append('#'.join(arrayList))
  132. vodList['vod_play_url'] = '$$$'.join(playList)
  133. result = {
  134. 'list':[vodList]
  135. }
  136. return result
  137. authorization = ''
  138. timeoutTick = 0
  139. localTime = 0
  140. expiresIn = 0
  141. shareTokenMap = {}
  142. expiresMap = {}
  143. localMedia = {}
  144. header = {
  145. "Referer":"https://www.aliyundrive.com/",
  146. "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"
  147. }
  148. localProxyUrl = 'http://127.0.0.1:UndCover/proxy'
  149. def redirectResponse(tUrl):
  150. rsp = requests.get(tUrl, allow_redirects=False,verify = False)
  151. if 'Location' in rsp.headers:
  152. return redirectResponse(rsp.headers['Location'])
  153. else:
  154. return rsp
  155. def getDownloadUrl(self,shareId,token,fileId,category):
  156. lShareId = shareId
  157. lFileId = fileId
  158. params = {
  159. "share_id": lShareId,
  160. "category": "live_transcoding",
  161. "file_id": lFileId,
  162. "template_id": ""
  163. }
  164. customHeader = self.header.copy()
  165. customHeader['x-share-token'] = token
  166. customHeader['authorization'] = self.authorization
  167. url = 'https://api.aliyundrive.com/v2/file/get_share_link_video_preview_play_info'
  168. if category == 'video':
  169. rsp = requests.post(url,json = params,headers=customHeader)
  170. rspJo = json.loads(rsp.text)
  171. lShareId = rspJo['share_id']
  172. lFileId = rspJo['file_id']
  173. jo = {
  174. }
  175. if category == 'video':
  176. jo['share_id'] = lShareId
  177. jo['file_id'] = lFileId
  178. jo['expire_sec'] = 600
  179. if category == 'audio':
  180. jo['share_id'] = lShareId
  181. jo['file_id'] = lFileId
  182. jo['get_audio_play_info'] = True
  183. downloadUrl = 'https://api.aliyundrive.com/v2/file/get_share_link_download_url'
  184. downloadRsp = requests.post(downloadUrl,json = jo,headers=customHeader)
  185. resultJo = json.loads(downloadRsp.text)
  186. return resultJo['download_url']
  187. def getMediaSlice(self,shareId,token,fileId):
  188. params = {
  189. "share_id": shareId,
  190. "category": "live_transcoding",
  191. "file_id": fileId,
  192. "template_id": ""
  193. }
  194. customHeader = self.header.copy()
  195. customHeader['x-share-token'] = token
  196. customHeader['authorization'] = self.authorization
  197. url = 'https://api.aliyundrive.com/v2/file/get_share_link_video_preview_play_info'
  198. rsp = requests.post(url,json = params,headers=customHeader)
  199. rspJo = json.loads(rsp.text)
  200. quality = ['FHD','HD','SD']
  201. videoList = rspJo['video_preview_play_info']['live_transcoding_task_list']
  202. highUrl = ''
  203. for q in quality:
  204. if len(highUrl) > 0:
  205. break
  206. for video in videoList:
  207. if(video['template_id'] == q):
  208. highUrl = video['url']
  209. break
  210. if len(highUrl) == 0:
  211. highUrl = videoList[0]['url']
  212. noRsp = requests.get(highUrl,headers=self.header, allow_redirects=False,verify = False)
  213. m3u8Url = ''
  214. if 'Location' in noRsp.headers:
  215. m3u8Url = noRsp.headers['Location']
  216. if 'location' in noRsp.headers and len(m3u8Url) == 0 :
  217. m3u8Url = noRsp.headers['location']
  218. m3u8Rsp = requests.get(m3u8Url,headers=self.header)
  219. m3u8Content = m3u8Rsp.text
  220. tmpArray = m3u8Url.split('/')[0:-1]
  221. host = '/'.join(tmpArray) + '/'
  222. m3u8List = []
  223. mediaMap = {}
  224. slices = m3u8Content.split("\n")
  225. count = 0
  226. for slice in slices:
  227. tmpSlice = slice
  228. if 'x-oss-expires' in tmpSlice:
  229. count = count + 1
  230. mediaMap[str(count)] = host+tmpSlice
  231. tmpSlice = "{0}?do=push_agent&api=python&type=media&share_id={1}&file_id={2}&media_id={3}".format(self.localProxyUrl,shareId,fileId,count)
  232. m3u8List.append(tmpSlice)
  233. self.localMedia[fileId] = mediaMap
  234. return '\n'.join(m3u8List)
  235. def proxyMedia(self,map):
  236. shareId = map['share_id']
  237. fileId = map['file_id']
  238. mediaId = map['media_id']
  239. shareToken = self.getToken(shareId,'')
  240. refresh = False
  241. url = ''
  242. ts = 0
  243. if fileId in self.localMedia:
  244. fileMap = self.localMedia[fileId]
  245. if mediaId in fileMap:
  246. url = fileMap[mediaId]
  247. if len(url) > 0:
  248. ts = int(self.regStr(url,"x-oss-expires=(\\d+)&"))
  249. # url = self.localMedia[fileId][mediaId]
  250. # ts = int(self.regStr(url,"x-oss-expires=(\\d+)&"))
  251. self.localTime = int(time.time())
  252. if ts - self.localTime <= 60:
  253. self.getMediaSlice(shareId,shareToken,fileId)
  254. url = self.localMedia[fileId][mediaId]
  255. action = {
  256. 'url':url,
  257. 'header':self.header,
  258. 'param':'',
  259. 'type':'stream',
  260. 'after':''
  261. }
  262. print(action)
  263. return [200, "video/MP2T", action, ""]
  264. def proxyM3U8(self,map):
  265. shareId = map['share_id']
  266. fileId = map['file_id']
  267. shareToken = self.getToken(shareId,'')
  268. content = self.getMediaSlice(shareId,shareToken,fileId)
  269. action = {
  270. 'url':'',
  271. 'header':'',
  272. 'param':'',
  273. 'type':'string',
  274. 'after':''
  275. }
  276. return [200, "application/octet-stream", action, content]
  277. def localProxy(self,param):
  278. typ = param['type']
  279. if typ == "m3u8":
  280. return self.proxyM3U8(param)
  281. if typ == "media":
  282. return self.proxyMedia(param)
  283. return None
  284. def getToken(self,shareId,sharePwd):
  285. self.localTime = int(time.time())
  286. shareToken = ''
  287. if shareId in self.shareTokenMap:
  288. shareToken = self.shareTokenMap[shareId]
  289. # todo
  290. expire = self.expiresMap[shareId]
  291. if len(shareToken) > 0 and expire - self.localTime > 600:
  292. return shareToken
  293. params = {
  294. 'share_id':shareId,
  295. 'share_pwd':sharePwd
  296. }
  297. url = 'https://api.aliyundrive.com/v2/share_link/get_share_token'
  298. rsp = requests.post(url,json = params,headers=self.header)
  299. jo = json.loads(rsp.text)
  300. newShareToken = jo['share_token']
  301. self.expiresMap[shareId] = self.localTime + int(jo['expires_in'])
  302. self.shareTokenMap[shareId] = newShareToken
  303. print(self.expiresMap)
  304. print(self.shareTokenMap)
  305. return newShareToken
  306. def listFiles(self,map,shareId,shareToken,fileId):
  307. url = 'https://api.aliyundrive.com/adrive/v3/file/list'
  308. newHeader = self.header.copy()
  309. newHeader['x-share-token'] = shareToken
  310. params = {
  311. 'image_thumbnail_process':'image/resize,w_160/format,jpeg',
  312. 'image_url_process':'image/resize,w_1920/format,jpeg',
  313. 'limit':200,
  314. 'order_by':'updated_at',
  315. 'order_direction':'DESC',
  316. 'parent_file_id':fileId,
  317. 'share_id':shareId,
  318. 'video_thumbnail_process':'video/snapshot,t_1000,f_jpg,ar_auto,w_300'
  319. }
  320. maker = ''
  321. arrayList = []
  322. for i in range(1,51):
  323. if i >= 2 and len(maker) == 0:
  324. break
  325. params['marker'] = maker
  326. rsp = requests.post(url,json = params,headers=newHeader)
  327. jo = json.loads(rsp.text)
  328. ja = jo['items']
  329. for jt in ja:
  330. if jt['type'] == 'folder':
  331. arrayList.append(jt['file_id'])
  332. else:
  333. if 'video' in jt['mime_type'] or 'video' in jt['category']:
  334. repStr = jt['name'].replace("#", "_").replace("$", "_")
  335. map[repStr] = shareId + "+" + shareToken + "+" + jt['file_id'] + "+" + jt['category']
  336. # print(repStr,shareId + "+" + shareToken + "+" + jt['file_id'])
  337. maker = jo['next_marker']
  338. i = i + 1
  339. for item in arrayList:
  340. self.listFiles(map,shareId,shareToken,item)
  341. def login(self):
  342. self.localTime = int(time.time())
  343. url = 'https://api.aliyundrive.com/token/refresh'
  344. if len(self.authorization) == 0 or self.timeoutTick - self.localTime <= 600:
  345. form = {
  346. 'refresh_token':'f6fcd2d1c0e54601805413721d89e0ca'
  347. }
  348. rsp = requests.post(url,json = form,headers=self.header)
  349. jo = json.loads(rsp.text)
  350. self.authorization = jo['token_type'] + ' ' + jo['access_token']
  351. self.expiresIn = int(jo['expires_in'])
  352. self.timeoutTick = self.localTime + self.expiresIn
  353. # print(self.authorization)
  354. # print(self.timeoutTick)
  355. # print(self.localTime)
  356. # print(self.expiresIn)