This repository has been archived by the owner on May 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstates.py
90 lines (65 loc) · 2.76 KB
/
states.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import enum
from subreddit_archiver import db
class DB(enum.Enum):
"""Keys that the table archive_metadata keeps track of"""
# stores a value of the Progress enum, represents the state of archival
PROGRESS = "archival_progress"
# the name of the subreddit being archivedd
SUBREDDIT = "subreddit"
# when the subreddit was created, as a unix timestamp
CREATED_UTC = "subreddit_created_utc"
# when the newest post in the archive was created, as a unix timestamp
MOST_RECENT_POST_UTC = "most_recent_saved_post_utc"
# when the least post in the archive was created, as a unix timestamp
LEAST_RECENT_POST_UTC = "least_recent_saved_post_utc"
class Progress(enum.Enum):
"""Values the DB.PROGRESS key can take in the db"""
# conveys that archival has not yet begun
IDLE = 1
# conveys that archival has begun and is in progress
SAVING_POSTS = 2
# conveys that archival is complete
COMPLETED = 3
class State:
def __init__(self, db_connection):
self.db_connection = db_connection
def set_key(self, key, value):
"""Record a value under a key
Args:
key: The key to record the value under. An attribute of DB.
value: The value to store.
"""
db.set_kv(self.db_connection, key.value, value)
def get_key(self, key):
"""Get a key from the database
Args:
key: The key under which the required value is stored. An attribute of DB.
Returns:
The string stored under the key.
"""
return db.get_kv(self.db_connection, key.value)
def get_progress(self):
try:
progress = Progress(int(self.get_key(DB.PROGRESS)))
except KeyError:
progress = Progress.IDLE
self.set_key(DB.PROGRESS, Progress.IDLE.value)
return progress
def set_progress(self, progress):
self.set_key(DB.PROGRESS, progress.value)
def get_subreddit(self):
return self.get_key(DB.SUBREDDIT)
def set_subreddit(self, subreddit):
self.set_key(DB.SUBREDDIT, subreddit)
def get_subreddit_created_utc(self):
return int(float(self.get_key(DB.CREATED_UTC)))
def set_subreddit_created_utc(self, created_utc):
self.set_key(DB.CREATED_UTC, created_utc)
def get_most_recent_post_utc(self):
return int(float(self.get_key(DB.MOST_RECENT_POST_UTC)))
def set_most_recent_post_utc(self, most_recent_post_id_utc):
self.set_key(DB.MOST_RECENT_POST_UTC, most_recent_post_id_utc)
def get_least_recent_post_utc(self):
return int(float(self.get_key(DB.LEAST_RECENT_POST_UTC)))
def set_least_recent_post_utc(self, least_recent_post_id_utc):
self.set_key(DB.LEAST_RECENT_POST_UTC, least_recent_post_id_utc)