-
Notifications
You must be signed in to change notification settings - Fork 1
/
img.py
42 lines (37 loc) · 1.38 KB
/
img.py
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
import requests
import re
import time
import os
import datetime
import random
import string
class Img:
def __init__(self, imgurl):
self.imgurl = imgurl
self.id = self.randomid()
self.imgtype = self.imgtype()
self.path = self.download()
def imgtype(self):
return re.search(".*?\.(jpg|png|gif|jpeg|svg|webp)", self.imgurl).group(1)
def randomid(self):
return str(datetime.datetime.now().strftime("%Y%m%d")) + '-' + ''.join(
random.sample(string.ascii_lowercase + string.digits, 12))
def download(self):
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'}
try:
with open(f"img/{self.id}.{self.imgtype}", 'wb') as f:
f.write(requests.get(self.imgurl, headers=headers).content)
f.close()
return f"img/{self.id}.{self.imgtype}"
except:
try:
# 重下一次
time.sleep(3)
with open(f"img/{self.id}.{self.imgtype}", 'wb') as f:
f.write(requests.get(self.imgurl, headers=headers).content)
f.close()
return f"img/{self.id}.{self.imgtype}"
except:
os.remove(f"img/{self.id}.{self.imgtype}")
return None