-
-
Save zhihaofans/5f09629e5f82125aeee1 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
# coding=utf-8 | |
import os | |
import time | |
import requests | |
def ensure_dir(f): | |
d = os.path.join(os.getcwd(), f) | |
if not os.path.exists(d): | |
os.makedirs(d) | |
return d | |
def unescape(string): | |
global h | |
global word | |
h = -1 | |
word = '' | |
def parse(): | |
global h | |
global word | |
if h == -1: | |
if ch == '%': | |
h = 0 | |
return '' | |
else: | |
return ch | |
elif h == 0: | |
h = 1 | |
word = ch | |
return '' | |
else: | |
h = -1 | |
word += ch | |
return word.decode('hex') | |
return ''.join([parse() for ch in string]) | |
def download_image(folder): | |
r = requests.get('http://cover.acfunwiki.org/cover.php') | |
url = r.url | |
print(url) | |
name = unescape(url[url.rfind('/') + 1:].encode('utf-8')).decode('utf-8') | |
print(name) | |
file_path = os.path.join(folder, name) | |
if not os.path.exists(file_path): | |
image = open(file_path, 'wb') | |
image.write(r.content) | |
image.close() | |
else: | |
print('exist') | |
if __name__ == '__main__': | |
cover_dir = ensure_dir('cover') | |
while True: | |
try: | |
download_image(cover_dir) | |
# time.sleep(5) | |
except (KeyboardInterrupt, SystemExit): | |
raise | |
except Exception as e: | |
print(str(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment