-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
54 lines (48 loc) · 2.1 KB
/
base.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
import sys, json
import pathlib
from api import TwitterAPI
class Base(object):
def __init__(self, username : str, api : TwitterAPI):
self.username = username
self.api = api
self.parent_dir = 'data'
self.media_dir = str(pathlib.Path(self.parent_dir, self.username))
self.user_json_filename = str(pathlib.Path(self.media_dir, 'user.json'))
self.tweets_json_filename = str(pathlib.Path(self.media_dir, 'tweets.json'))
def _change_parent_dir(self, dir: str):
self.parent_dir = dir
self.media_dir = str(pathlib.Path(self.parent_dir, self.username))
self.user_json_filename = str(pathlib.Path(self.media_dir, 'user.json'))
self.tweets_json_filename = str(pathlib.Path(self.media_dir, 'tweets.json'))
def _read_user_json(self) -> bool:
try:
with open(self.user_json_filename, 'r') as f:
self.user_json = json.load(f)
return True
except FileNotFoundError:
# print('[!] %s file not found' % self.user_json_filename)
return False
except AttributeError as error:
print('[!] attribute error reading user json for %s: %s' % (self.username, error))
return False
except:
print('[!] unexpected error reading user json for %s: %s' % (self.username, sys.exc_info()[0]))
return False
print('[!] unknown error')
return False
def _read_tweets_json(self) -> bool:
try:
with open(self.tweets_json_filename, 'r') as f:
self.tweets_json = json.load(f)
return True
except FileNotFoundError:
#print('[!] %s file not found' % self.tweets_json_filename)
return False
except AttributeError as error:
print('[!] attribute error reading tweet json for %s: %s' % (self.username, error))
return False
except:
print('[!] unexpected error reading tweet json for %s: %s' % (self.username, sys.exc_info()[0]))
return False
print('[!] unknown error')
return False