1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
| from random import randint from urllib.parse import unquote,quote,parse_qs import requests import urllib.request import json import time as t import datetime import zlib import re import xml.etree.ElementTree as ET import oss2
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE', }
def get_response_iqiyi(url): req = urllib.request.Request(url) req.add_header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36") response = urllib.request.urlopen(req).read() return response
def get_response(url): response = requests.get(url) response.encoding = 'utf-8' return response.text
def judgeIllegalChar(str): illegal = False for char in ["<", ">", "&", "\u0000", "\b"]: if char in str: illegal = True break return illegal
def make_response_head(): return '<?xml version="1.0" encoding="UTF-8"?>\n<i>\n'
def make_response_foot(): return '</i>'
def make_response_body(timepoint, content, ct=1, size=20, color=16777215, unixtime=int(t.mktime(datetime.datetime.now().timetuple())), uid=0): return '\t<d p="{},{},{},{},{},0,{},26732601000067074">{}</d>\n'.format(timepoint, ct, size, color, unixtime, uid, content)
def mgtv(url): cid = url.split('/')[4] vid = url.split('/')[5].split('?')[0].strip('.html') title = re.search(r'partName:"(.*?)",', get_response(url)) if title is not None: title = title.group(1) else: title = 'Unknow' contents = set() ret = make_response_head()
time = 0 total = 0 cnt = 0 while True: result = get_response('https://galaxy.bz.mgtv.com/rdbarrage?vid=' + vid + '&cid=' + cid + '&time=' + str(time)) danmu = json.loads(result) if danmu['data']['items'] == None: break for j in danmu['data']['items']: total += 1 if judgeIllegalChar(j['content']): continue timepoint = j['time'] / 1000 uid = j['uid'] content = j['content'] if content not in contents: cnt += 1 contents.add(content) ret += make_response_body(timepoint=timepoint, content=content, uid=uid) time = danmu['data']['next'] ret += make_response_foot() print("Download {} danmakus, Select {} danmakus\nfinish.".format(total, cnt)) return [title, ret]
def tencentvideo(url): video_info = json.loads( str([s for s in get_response(url).split('\n') if 'VIDEO_INFO' in str(s)]).strip('[\'var VIDEO_INFO = ').strip( '\']')) duration = video_info['duration'] title = video_info['title'] vid = video_info['vid'] targetid = json.loads(get_response('http://bullet.video.qq.com/fcgi-bin/target/regist?otype=json&vid=' + vid).strip( 'QZOutputJson=').strip(';'))['targetid'] contents = set() ret = make_response_head()
total = 0 cnt = 0 for i in range(int(duration) // 30 + 1): timestamp = i * 30 danmu = json.loads( get_response('http://mfm.video.qq.com/danmu?timestamp=' + str(timestamp) + '&target_id=' + targetid), strict=False) for j in danmu['comments']: total += 1 if judgeIllegalChar(j['content']): continue timepoint = j['timepoint'] ct = 1 size = 20 if "color" in j["content_style"]: content_style = json.loads(j["content_style"]) color = int(content_style["color"], 16) else: color = 16777215 unixtime = int(t.mktime(datetime.datetime.now().timetuple())) content = j['content'] if content not in contents: cnt += 1 contents.add(content) ret += '\t<d p="{},{},{},{},{},0,0,26732601000067074">{}</d>\n'.format(timepoint, ct, size, color, unixtime, content) ret += make_response_foot() print("Download {} danmakus, Select {} danmakus\nfinish.".format(total, cnt)) return [title, ret]
def youku(url): res = get_response(url) title = re.search(r'<title>(.*)</title>', res).group(1).split('—')[0] iid = re.search(r'videoId: \'(\d*)\'', res).group(1) duration = float(re.search(r'seconds: \'(.*)\',', res).group(1)) contents = set()
total = 0 cnt = 0 ret = make_response_head() for mat in range(int(duration) // 60 + 1): response = get_response('https://service.danmu.youku.com/list?mat=' + str(mat) + '&ct=1001&iid=' + iid) danmu = json.loads(response) for i in range(len(danmu["result"])): total += 1 if judgeIllegalChar(danmu["result"][i]["content"]): continue playat = danmu["result"][i]["playat"] / 1000 if "color" in danmu["result"][i]["propertis"]: propertis = json.loads(danmu["result"][i]["propertis"]) color = propertis["color"] else: color = 16777215 content = danmu["result"][i]["content"] if content not in contents: contents.add(content) cnt += 1 ret += make_response_body(timepoint=playat, color=color, content=content) ret += make_response_foot() print("Download {} danmakus, Select {} danmakus\nfinish.".format(total, cnt)) return [title, ret]
def iqiyi(url): ret = get_response(url) return ['iqiyi',ret] page_info = json.loads(re.search(r"page-info='(.*)' :video-info=", get_response(url)).group(1)) duration_str = page_info['duration'].split(':') duration = 0 for i in range(len(duration_str) - 1): duration = (duration + int(duration_str[i])) * 60 duration = duration + int(duration_str[-1]) title = page_info['tvName'] albumid = page_info['albumId'] tvid = page_info['tvId'] categoryid = page_info['cid'] page = duration // (60 * 5) + 1 contents = set()
total = 0 cnt = 0 ret = make_response_head() for i in range(duration // (60 * 5) + 1): dec = zlib.decompressobj(32 + zlib.MAX_WBITS) b = dec.decompress(get_response_iqiyi( 'http://cmts.iqiyi.com/bullet/' + str(tvid)[-4:-2] + '/' + str(tvid)[-2:] + '/' + str(tvid) + '_300_' + str( i + 1) + '.z?rn=0.' + ''.join(["%s" % randint(0, 9) for num in range(0, 16)]) + '&business=danmu&is_iqiyi=true&is_video_page=true&tvid=' + str( tvid) + '&albumid=' + str(albumid) + '&categoryid=' + str(categoryid) + '&qypid=01010021010000000000')) root = ET.fromstring(b.decode("utf-8")) for bulletInfo in root.iter('bulletInfo'): total += 1 timepoint = bulletInfo[3].text color = int(bulletInfo[5].text, 16) content = bulletInfo[1].text size = bulletInfo[4].text if content not in contents: cnt += 1 contents.add(content) ret += make_response_body(timepoint=timepoint, color=color, content=content, size=size) ret += make_response_foot() print("Download {} danmakus, Select {} danmakus\nfinish.".format(total, cnt)) return [title, ret]
def save2oss(title, xml, download): url = "XML/" + title + ".xml" auth = oss2.Auth('******', '******') endpoint_internal = 'oss-cn-******-internal.aliyuncs.com' endpoint = 'oss-cn-******.aliyuncs.com' bucketName = '****' bucket = oss2.Bucket(auth, endpoint, bucketName) headers = {} if (download): headers['Content-Type']='application/force-download' else: headers['Content-Type']='application/xml'
result = bucket.put_object(url, xml,headers=headers) print('Upload URL:{}\tHTTP status: {}'.format(url, result.status)) return unquote(bucket.sign_url('GET', url, 120),'utf-8')
def bilibili(url): text = get_response(url) keyStr = re.findall(r'"cid":[\d]*', text) if not keyStr: keyStr = re.findall(r'cid=[\d]*', text) key = eval(keyStr[0].split('=')[1]) else: key = eval(keyStr[0].split(':')[1]) commentUrl = 'https://comment.bilibili.com/' + str(key) + '.xml' return commentUrl
def build_response(url,download): if url.find('mgtv.com') >= 0: [title, ret] = mgtv(url) elif url.find('qq.com') >= 0: [title, ret] = tencentvideo(url) elif url.find('youku.com') >= 0: [title, ret] = youku(url) elif url.find('iqiyi.com') >= 0: [title, ret] = iqiyi(url) elif url.find('bilibili.com') >= 0: return bilibili(url) else: return None return save2oss(title, ret, download)
def handler(environ, start_response): if 'QUERY_STRING' not in environ: status = '200 OK' response_headers = [('Content-type', 'text/html; charset=UTF-8')] with open("index.html", "r", encoding="utf-8") as f: ret = f.read() else: query_string = environ['QUERY_STRING'] params = parse_qs(query_string) download = params.get('download',['off'])[0] download = (download == 'on') url = unquote(params['url'][0], 'utf-8') returl = build_response(url,download) if returl is not None: status = '302 Found' response_headers = [('Location', quote(returl,safe='/:?&='))] ret = '' else: status = '200 OK' response_headers = [('Content-type', 'text/html; charset=UTF-8')] ret = "不支持的视频网址" start_response(status, response_headers) return [ret.encode('utf8')]
|