slight restructuring around tracking our posts
This commit is contained in:
+77
-29
@@ -15,10 +15,13 @@ class TalentTweetQueue:
|
||||
TalentTweetQueue.instance = self
|
||||
self.queue_path = util.get_queue_path()
|
||||
self.queue_backup_path = util.get_queue_backup_path()
|
||||
self.current_ttweet_path = f'{util.get_project_dir()}/_current_ttweet.txt'
|
||||
self.finished_ttweets_path = f'{util.get_project_dir()}/finished_ttweets.txt'
|
||||
self.is_good = True
|
||||
self.__sorted = False
|
||||
self.finished_user_timestamps = dict()
|
||||
self.ttweets_dict = dict()
|
||||
self.good = False # if true, overwrite queue.txt on destruction
|
||||
self.__sorted = False
|
||||
self.finished_ttweets = list()
|
||||
|
||||
## file check, backup copy
|
||||
if os.path.exists(self.queue_backup_path):
|
||||
@@ -30,59 +33,104 @@ class TalentTweetQueue:
|
||||
|
||||
## initialize structures
|
||||
# user timestamps
|
||||
with open(self.queue_path, 'r') as f:
|
||||
for line in f:
|
||||
tokens = line.split()
|
||||
if len(tokens) == 0: continue
|
||||
try:
|
||||
with open(self.queue_path, 'r') as f:
|
||||
for line in f:
|
||||
tokens = line.split()
|
||||
if len(tokens) == 0: continue
|
||||
|
||||
if tokens[0][0] != '#':
|
||||
print(f'Stopped finding user timestamps at {line}')
|
||||
# reached end of accounts list
|
||||
break
|
||||
if tokens[2] != '-1':
|
||||
self.finished_user_timestamps[int(tokens[1])] = float(tokens[2])
|
||||
|
||||
# tweets
|
||||
with open(self.queue_path, 'r') as f: # reset seek head
|
||||
# Get existing queued TalentTweets
|
||||
for line in f:
|
||||
tokens = line.split()
|
||||
if len(tokens) == 0 or tokens[0][0] == '#':
|
||||
continue
|
||||
ttweet = tt.TalentTweet.deserialize(line)
|
||||
self.ttweets_dict[ttweet.tweet_id] = ttweet
|
||||
print(f'Found {len(self.finished_user_timestamps)} scraped accounts and {len(self.ttweets_dict)} tweets in queue.')
|
||||
if tokens[0][0] != '#':
|
||||
print(f'Stopped finding user timestamps at {line}')
|
||||
# reached end of accounts list
|
||||
break
|
||||
if tokens[2] != '-1':
|
||||
self.finished_user_timestamps[int(tokens[1])] = float(tokens[2])
|
||||
except: pass
|
||||
# ttweets
|
||||
try:
|
||||
with open(self.queue_path, 'r') as f: # reset seek head
|
||||
# Get existing queued TalentTweets
|
||||
for line in f:
|
||||
tokens = line.split()
|
||||
if len(tokens) == 0 or tokens[0][0] == '#':
|
||||
continue
|
||||
ttweet = tt.TalentTweet.deserialize(line)
|
||||
self.ttweets_dict[ttweet.tweet_id] = ttweet
|
||||
print(f'Found {len(self.finished_user_timestamps)} scraped accounts and {len(self.ttweets_dict)} tweets in queue.')
|
||||
except: pass
|
||||
# finished ttweets
|
||||
try:
|
||||
with open(self.finished_ttweets_path, 'r') as f:
|
||||
for line in f:
|
||||
self.finished_ttweets.append(int(line))
|
||||
except: pass
|
||||
|
||||
|
||||
def is_empty(self):
|
||||
return self.get_count() <= 0
|
||||
|
||||
def add_ttweet(self, ttweet):
|
||||
self.__sorted = False
|
||||
self.ttweets_dict[ttweet.tweet_id] = ttweet
|
||||
|
||||
def get_ttweet(self, id):
|
||||
return self.ttweets_dict[id]
|
||||
return self.ttweets_dict[id]
|
||||
|
||||
def get_next_ttweet(self):
|
||||
if os.path.exists(self.current_ttweet_path):
|
||||
with open(self.current_ttweet_path, 'r') as f:
|
||||
return tt.TalentTweet.deserialize(f.readline())
|
||||
|
||||
self.__sort_ttweets_dict()
|
||||
key = list(self.ttweets_dict.keys())[0]
|
||||
ttweet = self.ttweets_dict.pop(key)
|
||||
with open(self.current_ttweet_path, 'w') as f:
|
||||
f.write(ttweet.serialize())
|
||||
self.is_good = False
|
||||
return ttweet
|
||||
|
||||
def get_ttweets_dict(self):
|
||||
self.__sort_ttweets_dict() if not self.__sorted else None
|
||||
return self.ttweets_dict
|
||||
def get_count(self):
|
||||
return len(self.ttweets_dict)
|
||||
|
||||
## Call when the TalentTweet retrieved from get_next_ttweet() was
|
||||
# posted successfully.
|
||||
def good(self):
|
||||
with open(self.current_ttweet_path, 'r') as f:
|
||||
ttweet = tt.TalentTweet.deserialize(f.readline())
|
||||
|
||||
self.add_finished_tweet(ttweet.tweet_id)
|
||||
os.remove(self.current_ttweet_path)
|
||||
self.save_file()
|
||||
self.is_good = True
|
||||
|
||||
# overwrite queue.txt
|
||||
def save_file(self):
|
||||
shutil.copyfile(self.queue_path, self.queue_backup_path)
|
||||
self.__sort_ttweets_dict()
|
||||
with open(self.queue_path, 'w') as f:
|
||||
# write timestamps
|
||||
for (id, timestamp) in self.finished_user_timestamps.items():
|
||||
f.write(f'# {id} {timestamp}\n')
|
||||
|
||||
f.write('\n')
|
||||
|
||||
# write sorted ttweets
|
||||
for ttweet in self.ttweets_dict.values():
|
||||
f.write(ttweet.serialize() + '\n')
|
||||
|
||||
def add_finished_tweet(self, id):
|
||||
self.finished_ttweets.append(id)
|
||||
with open(self.finished_ttweets_path, 'a') as f:
|
||||
f.write(f'{id}\n')
|
||||
|
||||
def __sort_ttweets_dict(self):
|
||||
self.ttweets_dict = dict(sorted(self.ttweets_dict.items()))
|
||||
if not self.__sorted:
|
||||
self.ttweets_dict = dict(sorted(self.ttweets_dict.items()))
|
||||
self.__sorted = True
|
||||
|
||||
# destructor
|
||||
def __del__(self):
|
||||
if self.good:
|
||||
if self.is_good:
|
||||
print('Ended in good state, deleting backup queue...')
|
||||
os.remove(self.queue_backup_path)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user