-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvote_monitor.py
68 lines (52 loc) · 1.84 KB
/
vote_monitor.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
61
62
63
64
65
66
67
68
import csv
import json
from datetime import datetime
import os
import praw
from config import config
from reddit import reddit
class DefaultEncoder(json.JSONEncoder):
def default(self, o):
return ""
class MonitoredSubmission:
def __init__(self, id) -> None:
self.id = id
self.log_file = open('monitored/' + id + '.csv', 'a+')
self.csv_writer = csv.writer(self.log_file)
self.submission: praw.reddit.Submission = None
self.last_log: datetime = None
self.load_submission()
self.write_description()
def load_submission(self):
self.submission = reddit.submission(self.id)
self.submission.title # needed to load submission data
def write_description(self):
filename = 'monitored/' + self.id + '.json'
if os.path.exists(filename):
return
with open(filename, 'w+', encoding='utf8') as description_file:
self.load_submission()
json.dump(vars(self.submission),
description_file,
cls=DefaultEncoder,
ensure_ascii=False,
sort_keys=True, indent=4, separators=(',', ': '))
def log_score(self):
self.load_submission()
now = datetime.now()
self.last_log = now
self.csv_writer.writerow((
now.astimezone().replace(microsecond=0).isoformat(),
str(self.submission.score),
str(self.submission.upvote_ratio)
))
self.log_file.flush()
def last_log_old(self) -> bool:
if self.last_log is None:
return True
if ((datetime.now() - self.last_log).total_seconds() > config['submission_log_minimum_period_s']):
return True
return False
def log_score_if_needed(self):
if (self.last_log_old()):
self.log_score()