-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
170 lines (142 loc) · 4.54 KB
/
utils.py
File metadata and controls
170 lines (142 loc) · 4.54 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import errno
import os
import sys
from contextlib import asynccontextmanager, contextmanager
from pathlib import Path
import aiomysql
import pymysql
import pymysql.cursors
from dotenv import load_dotenv
MIN_BACKOFF = 120
MAX_BACKOFF = 3600
THUMBNAIL_SIZE = 256
dotenv_path = Path(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".env"))
if not load_dotenv(dotenv_path=dotenv_path):
print("Couldn't load the configuration file. "
"Please ensure the file `.env` is in the same directory as the executable. "
"You may need to complete and rename the example file `.env-example`.",
file=sys.stderr)
sys.exit(errno.ENOENT)
def get_reddit_auth():
try:
return {
"client_id": os.environ['AWB_CLIENT_ID'],
"client_secret": os.environ['AWB_CLIENT_SECRET'],
"user_agent": f"python:awb:v2.0b (by u/AnimewallpaperBot)",
"refresh_token": os.environ['AWB_TOKEN'],
}
except KeyError as e:
_raise_env_missing(e)
def get_test_reddit_auth():
try:
return {
"client_id": os.environ['TEST_CLIENT_ID'],
"client_secret": os.environ['TEST_CLIENT_SECRET'],
"user_agent": f"python:awb:test (by u/{os.environ['TEST_USERNAME']})",
"username": os.environ['TEST_USERNAME'],
"password": os.environ['TEST_PASSWORD']
}
except KeyError as e:
_raise_env_missing(e)
def get_imgur_auth():
try:
return {
"Authorization": f"Client-ID {os.environ['IMGUR_AUTH']}"
}
except KeyError as e:
_raise_env_missing(e)
def get_mysql_auth(docker=False, as_root=False):
try:
return {
"host": "awb_mysql" if docker else "localhost",
"user": "root" if as_root else "animewallpaperbot",
"password": os.environ['MYSQL_ROOT_PASS'] if as_root else os.environ['MYSQL_PASS'],
"db": "awb",
}
except KeyError as e:
_raise_env_missing(e)
def get_rabbitmq_auth(docker=False):
try:
return {
"host": "awb_rabbitmq" if docker else "localhost",
"login": "animewallpaperbot",
"password": os.environ['RABBITMQ_PASS'],
}
except KeyError as e:
_raise_env_missing(e)
def get_default_settings():
return {
'enabled': False,
'flairs': {},
'except_authors': [],
'ResolutionMismatch': {'enabled': False},
'ResolutionBad': {
'enabled': False,
'horizontal': None,
'vertical': None,
'square': None
},
'AspectRatioBad': {
'enabled': False,
'horizontal': None,
'vertical': None
},
'RateLimitAny': {
'enabled': False,
'interval_hours': None,
'frequency': None,
'incl_deleted': False
},
'SourceCommentAny': {
'enabled': False,
'timeout_hrs': None,
},
'RepostAny': {
'enabled': False,
'report_only': True,
'similarity_pct': .75,
'threshold_months': 6,
}
}
# https://stackoverflow.com/a/54847238
# https://rednafi.github.io/digressions/python/2020/03/26/python-contextmanager.html
@asynccontextmanager
async def async_database_ctx(auth):
# TODO: logging + error handling
con = await aiomysql.connect(**auth)
cur = await con.cursor(aiomysql.cursors.DictCursor)
try:
yield cur
finally:
await con.commit()
await cur.close()
con.close()
@contextmanager
def database_ctx(auth):
# TODO: logging + error handling
con = pymysql.connect(**auth)
cur = con.cursor(pymysql.cursors.DictCursor)
try:
yield cur
finally:
con.commit()
cur.close()
con.close()
def _raise_env_missing(e: KeyError):
print(f"Value {e.args[0]} is not set. "
"Please ensure all values are set in the file `.env`. "
"You may need to complete and rename the example file `.env-example`.",
file=sys.stderr)
sys.exit(errno.ENOENT)
# https://medium.com/thefloatingpoint/pythons-round-function-doesn-t-do-what-you-think-71765cfa86a8
def normal_round(num, ndigits=0):
"""
Rounds a float to the specified number of decimal places.
num: the value to round
ndigits: the number of digits to round to
"""
if ndigits == 0:
return int(num + 0.5)
else:
digit_value = 10 ** ndigits
return int(num * digit_value + 0.5) / digit_value