-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplied_to.py
72 lines (54 loc) · 1.67 KB
/
replied_to.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
69
70
71
72
# Python
import json
import logging
import time
# 3rd Party
import praw
from atomicwrites import atomic_write
# Self
import util
from praw_wrapper import praw_object_wrapper_t
class replied_t:
def __init__(self, file_path):
self.path = file_path
try:
with open(file_path, 'r') as f:
self.dict = json.load(f)
except IOError:
self.dict = {}
pass
logging.debug("Initialized replied to list.")
# Takes an id or an object and returns whether that comment/sub in the list
def contains(self, obj):
id = None
if isinstance(obj, str):
id = obj
elif isinstance(obj, (praw_object_wrapper_t, praw.models.Comment, praw.models.Submission)):
id = obj.id
else:
raise ValueError("contains passed bad obj: {}".format(type(obj)))
return id in self.dict
def flush(self):
with atomic_write(self.path, overwrite=True) as f:
json.dump(self.dict, f, sort_keys=True, indent=4)
def add(self, wo):
if not isinstance(wo, praw_object_wrapper_t):
raise ValueError("add passed bad wo: {}".format(type(wo)))
if wo.id in self.dict:
logging.warning("add was passed {} whose ID is already listed".format(wo))
self.dict[wo.id] = {
"id": wo.id,
"type": "comments" if wo.is_comment() else "submissions",
"time": time.time(),
}
logging.debug(self.dict[wo.id])
logging.debug("Added {} to replied to list.".format(wo))
self.flush()
def remove(self, wo):
if not isinstance(wo, praw_object_wrapper_t):
raise ValueError("remove passed bad wo: {}".format(type(wo)))
if wo.id not in self.dict:
raise KeyError()
del self.dict[wo.id]
logging.debug("Removed {} from replied to list.".format(wo))
self.flush()