-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
60 lines (47 loc) · 2.24 KB
/
user.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import requests
import json
from base import Base
from api import TwitterAPI
import media_downloader as downloader
import utils
# allocate the user info in json
class User(Base):
def __init__(self, username: str, api: TwitterAPI):
Base.__init__(self, username, api)
# always replace with new json
def archive(self) -> bool:
resp = self.api.get_user_by_screen_name(self.username)
if resp.status_code != 200:
print('[!] unable to get user object for %s: status code %d' % (self.username, resp.status_code))
return False
if len(resp.text) <= 0:
print('[!] invalid response for %s: %s' % (self.username, resp.text))
return False
data = json.loads(resp.text)
if 'errors' in data:
print('[!] errors in user json response for %s: %s' % (self.username, data['errors']))
return False
if not utils.has_keys(data, ['data', 'user', 'result', 'legacy']):
print('[!] invalid user json response for %s: %s' % (self.username, resp.text))
return False
user_json = data['data']['user']['result']
if user_json['legacy']['protected'] is True:
print('[!] %s profile is protected' % self.username)
return False
# download avatar
avatar_image_url = user_json['legacy']['profile_image_url_https']
# remove '_normal' to get the hi-res one
avatar_image_url = avatar_image_url.replace('_normal', '')
downloader.avatar_photo(avatar_image_url, self.media_dir)
# download banner. some user does not have picture banner but instead has colored background
if 'profile_banner_url' in user_json['legacy']:
banner_image_url = user_json['legacy']['profile_banner_url']
downloader.banner_photo(banner_image_url, self.media_dir)
try:
with open(self.user_json_filename, 'w') as outfile:
json.dump(user_json, outfile)
#print('[√] refreshed user json for %s' % self.username)
return True
except:
print('[!] failed to save user json file (%s) for %s' % (self.user_json_filename, self.username))
return False