Created
September 6, 2018 08:30
-
-
Save bigshans/d6186817a322bb92ac2087822c16d182 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
#coding:utf-8 | |
from lxml import etree | |
import urllib.request | |
import os | |
import threading | |
import re | |
import sys | |
import json | |
all_file = [] | |
def feh_display(i): | |
l = len(all_file) | |
if (i == l): | |
for file_dir in all_file: | |
threading.Thread(target=os.system, args=('feh ./' + file_dir,)).start() | |
elif i < l: | |
threading.Thread(target=os.system, args=('feh ./' + all_file[i], )).start() | |
def display(): | |
if len(all_file) == 1: | |
c = input('需要打开吗?(y/n)') | |
if (c == 'y' or c == 'Y'): | |
os.system('feh '+'./' + all_file[0]) | |
else: | |
print('需要打开以下吗?') | |
i = 0 | |
for file_dir in all_file: | |
print('[%d]%s'%(i,file_dir)) | |
i += 1 | |
print('[%d]%s'%(i,'所有都打开')) | |
i += 1 | |
print('[%d]所有都不打开'%(i)) | |
c = map(int, str(input('请选择以上哪些项:')).split(' ')) | |
c = list(c) | |
if i in c: | |
return | |
elif i-1 in c: | |
feh_display(i-1) | |
else: | |
for index in c: | |
feh_display(index) | |
def cover(av): | |
request_url = 'https://www.bilibili.com/video/av'+ av +'.html' | |
headers = { | |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', | |
'Accept-Language':'zh-CN,zh;q=0.9', | |
'Connection':'keep-alive', | |
'Host':'m.bilibili.com', | |
'Host': 'm.bilibili.com', | |
'Referer':'https://www.bilibili.com/video/av' + av, | |
'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Mobile Safari/537.36)' | |
} | |
page = urllib.request.urlopen(urllib.request.Request(request_url, headers=headers)).read().decode('utf-8') | |
page = page.split('\n') | |
data = '' | |
begin = False | |
for eve in page: | |
if eve == '<script type="application/ld+json">': | |
if begin == False: | |
begin = True | |
continue | |
if begin : | |
if eve != '</script>': | |
data += eve | |
else: | |
begin = False | |
break | |
data = re.sub('\s','', data) | |
data = json.loads(data) | |
url = re.findall('(.*)@\w*\.*?',data['images'][0])[0] | |
# os.chdir('~/') | |
if not os.path.exists('cover') : | |
os.mkdir('cover') | |
print('"' + data['title'] + '"的封面正在获取...') | |
file_type = re.findall('\.(\w*)',url)[-1] | |
file_dir = 'cover/' + av + '.' + file_type | |
with open(file_dir,'wb') as f: | |
f.write(urllib.request.urlopen(url).read()) | |
print("获取完毕,保存至./" + file_dir) | |
all_file.append(file_dir) | |
def main(): | |
if len(sys.argv) < 2: | |
print("Usage:") | |
print(" ./cover.py av_num1 av_num2 ...") | |
sys.exit() | |
av = sys.argv[1:] | |
for av_num in av: | |
cover(av_num) | |
display() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment