-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesktop_ponymotes.py
360 lines (333 loc) · 13.4 KB
/
desktop_ponymotes.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python3
import sqlite3
import os
import logging
import argparse
import sys
import configparser
import concurrent.futures
import coloredlogs
import requests
__version__ = "1.0.4"
# Should auto set itself up on first run.
l = logging.getLogger(__name__)
coloredlogs.install(show_hostname=False)
# Some silly globals
remote_host = "http://dinsfire.com"
db_file = "emote_db.db"
last_modify_prefix = ".lastmodify_"
config_dir_path = os.path.expanduser("~/.desktop_ponymotes")
config_file = "config.ini"
session = requests.Session()
user_agent = "jibodeah.desktop_ponymotes"
# Also config, which is created by main.
_debug_levels = {
"critical": logging.CRITICAL,
"error": logging.ERROR,
"warning": logging.WARNING,
"info": logging.INFO,
"debug": logging.DEBUG,
}
_CONFIG_SECTION_BASIC = "Basic Config"
_CONFIG_SECTION_FILTERS = "Basic Filters"
_CONFIG_SECTION_EMOTES = "Emote Blacklist"
_CONFIG_SECTION_TAGS = "Tag Blacklist"
_CONFIG_SECTION_SUBREDDITS = "Subreddit Blacklist"
_config_file_default = r"""# BPM Updater config
# If you delete this it'll be regenerated with default values.
[{}]
# Directory to save to.
# '~' be used as a shortcut to your home directory on posix systems
# ...and tends to point to C:\users\yourusername on Windows.
# Must point to an existing directory.
# Recommended: Use the default, and link to it wherever you want.
emote_dir = ~/.desktop_ponymotes/emotes/
# Values to change blacklists into whitelists, if you want to be really
# exclusive for whatever reason.
emote_blacklist_is_whitelist = false
tag_blacklist_is_whitelist = false
subreddit_blacklist_is_whitelist = false
[{}]
# Basic emote filters.
# Also contains shortcuts for filtering of the most likely tags.
allow_nsfw = false
# Blacklists the 'questionable' tag
allow_questionable = true
# Blacklists the 'nonpony' tag
allow_nonpony = true
[{}]
# Case sensitive names of emotes to blacklist, one per line
# Note that you'll want to blacklist all name variants aswell!
# A leading + or - is permitted, but unnecessary.
# Examples: (But without the preceeding # and space)
# somedumbemote
# twisquint
[{}]
# Case insensitive names of tags to blacklist.
# Same format as the Emote Blacklist.
# 'Cept variant tags are not needed.
[{}]
# Case insensitive names of subreddits for blacklist.
# A leading /r/ is permitted, but unnecessary.
# Example: (If you hate bananas)
# mylittlenanners
# (Although in that case you'd be better off blacklisting the 'banana' tag)
""".format(
_CONFIG_SECTION_BASIC,
_CONFIG_SECTION_FILTERS,
_CONFIG_SECTION_EMOTES,
_CONFIG_SECTION_TAGS,
_CONFIG_SECTION_SUBREDDITS
)
def first_run_setup():
os.mkdir(config_dir_path)
os.chdir(config_dir_path)
os.mkdir("emotes") # This serves only as the default emote save location
os.mkdir("emotes/archive") # Deleted emotes are moved here
with open("emotes/archive/readme.txt", "w") as f:
f.write("Emotes removed from BPM go here.\n")
make_default_config()
def make_default_config():
with open(config_file, "w") as f:
f.write(_config_file_default)
def get_remote_file(path, local_filename, force=False, save_last_modified=True):
"""Fetches a remote file from dinsfire.com
path: Path to remote file, relative to dinsfire.com.
Ex.: for dinsfire.com/emoteCache/emote.png, pass /emoteCache/emote.png
local_filename: The local filename to save to.
force: Do not skip download if file not modified. (Default: False)
save_last_modified: Save a file containing the date the remote file
was last modified. To use when fetching the same file again.
(Default: True)
"""
h = {"user-agent": user_agent + "/{}".format(__version__)}
timestamp_filename = ''.join([
os.path.dirname(local_filename),
last_modify_prefix,
os.path.basename(local_filename)
])
if os.path.isfile(timestamp_filename) and not force:
with open(timestamp_filename, "r") as f:
h["If-Modified-Since"] = f.read().strip()
l.debug("Downloading remote: {}".format(path))
r = session.get("{}{}".format(remote_host, path), headers=h, stream=True)
if r.status_code == 304:
return r.status_code
elif r.status_code == 404:
l.warning("Got 404 for {}!".format(path))
return r.status_code
elif not r.ok:
l.error("Error fetching remote: {}".format(path))
r.raise_for_status()
raise RuntimeError("Response not ok!") # Should never get here.
with open(local_filename, "wb") as f:
for chunk in r.iter_content(1024):
f.write(chunk)
if "Last-Modified" in r.headers.keys() and save_last_modified:
with open(timestamp_filename, "w") as f:
f.write(r.headers['Last-Modified'] + "\n") # Gotta have that newline at end of file.
return r.status_code
def update_database(force=False):
l.info("Forcing database update!" if force else "Updating database...")
if os.path.isfile(db_file):
old_db_exists = True
l.debug("Database already exists, renaming to .old")
os.rename(db_file, db_file + '.old')
try:
i = get_remote_file("/emoteCache/mobileDatabase.db", db_file, force)
except Exception as e:
if old_db_exists:
os.rename(db_file + '.old', db_file)
raise e
if i == 304:
l.info("Database already up to date!")
if old_db_exists:
os.rename(db_file + '.old', db_file)
return False
elif i == 200:
l.info("Database updated!")
return True
def get_emotes(db, old_db=None, skip_already_downloaded=False):
"""Return a list of emote names that pass the filter.
db: A sqlite3.connection to the emote database.
old_db: A sqlite3.connection to the old version of the database.
When supplied, the function will return only updated emotes.
(Default: None)
skip_already_downloaded: If true, emotes that have already been downloaded
will not be returned.
NOTE: This means that updated emotes will not get updated locally
"""
q = "SELECT emoteName, dateModified FROM mobileEmotes" # Base query
w = [] # WHERE causes, with params replaced with `?`
p = [] # list corresponding to param variables (Cast to tuple later)
if not config[_CONFIG_SECTION_FILTERS].getboolean('allow_nsfw'):
w.append("isNSFW = ?")
p.append(0)
if not config[_CONFIG_SECTION_FILTERS].getboolean('allow_nonpony'):
w.append("tags NOT LIKE ?")
p.append("%nonpony %")
if not config[_CONFIG_SECTION_FILTERS].getboolean('allow_questionable'):
w.append("tags NOT LIKE ?")
p.append("%questionable %")
for key in config[_CONFIG_SECTION_EMOTES]:
w.append("emoteName {} ?".format("=" if config[_CONFIG_SECTION_BASIC].getboolean(
"emote_blacklist_is_whitelist") else "!="))
p.append(key)
for key in config[_CONFIG_SECTION_TAGS]:
w.append("tags{} LIKE ?".format("" if config[_CONFIG_SECTION_BASIC].getboolean(
"tag_blacklist_is_whitelist") else " NOT"))
if key.startswith("+") or key.startswith("-"):
key = key[1:]
p.append("%{} %".format(key.lower()))
for key in config[_CONFIG_SECTION_SUBREDDITS]:
w.append("source {} ?".format("=" if config[_CONFIG_SECTION_BASIC].getboolean(
"subreddit_blacklist_is_whitelist") else "!="))
key = key.lower()
if key.startswith("/r/"):
key = key[3:]
elif key.startswith("r/"):
key = key[2:]
p.append(key)
if w: # Not empty
q += " WHERE {}".format(' AND '.join(w))
q += " ORDER BY emoteName"
l.debug("SQL Query: {}".format(q))
l.debug("SQL Params: {}".format(p))
c = db.cursor()
c.execute(q, tuple(p))
ret = c.fetchall()
if skip_already_downloaded:
for e in ret[:]:
if os.path.isfile(''.join([config[_CONFIG_SECTION_BASIC]['emote_dir'], e[0], '.png'])):
ret.remove(e)
if old_db is not None:
old_c = old_db.cursor()
# Remove non-updated emotes.
for e in ret[:]:
old_c.execute("SELECT dateModified FROM mobileEmotes WHERE emoteName = ?", (e[0], ))
old_e = old_c.fetchone()
if old_e is not None and e[1] <= old_e[0]:
ret.remove(e)
return ret
def get_removed_emotes(emotes):
ret = []
for f in os.listdir(config[_CONFIG_SECTION_BASIC]['emote_dir']):
if not f.endswith(".png"):
l.debug("get_removed_emotes: '{}' not an image!".format(f))
continue
f = f[:-4] # strip the .png
if f not in emotes:
ret.append(f)
return ret
if __name__ == "__main__":
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument(
"--log-level",
help="Logging level to use.",
choices=_debug_levels.keys(),
default='info',
)
argument_parser.add_argument(
"--force",
"-f",
help="Force fresh download of all emotes.",
action="store_true",
)
argument_parser.add_argument(
"--skip-already-downloaded",
help="Skip emotes that already exist on disk. "
"(This is useful if you accidentally deleted an emote or two)",
action="store_true",
)
args = argument_parser.parse_args()
coloredlogs.set_level(_debug_levels[args.log_level])
l.info("Desktop PonyMotes {} running!".format(__version__))
if not os.path.isdir(config_dir_path):
l.warning("Config directory missing, performing first time setup...")
first_run_setup()
print("Just performed some first time setup!")
print("Look in {} and edit the config to your heart's desire...".format(
os.path.abspath(config_dir_path)))
print("Then run this again!")
sys.exit(0)
os.chdir(config_dir_path)
global config
config = configparser.ConfigParser(allow_no_value=True, empty_lines_in_values=False)
if not config.read(config_file): # Returns [] on failure
l.warning("Config file not found! Remaking from default...")
make_default_config()
if not config.read(config_file):
l.error("Error remaking and reading config file!")
raise RuntimeError("Could not load nor remake and read config.")
try:
config.items(_CONFIG_SECTION_BASIC)
config.items(_CONFIG_SECTION_FILTERS)
config.items(_CONFIG_SECTION_EMOTES)
config.items(_CONFIG_SECTION_TAGS)
config.items(_CONFIG_SECTION_SUBREDDITS)
except configparser.Error as e:
l.exception(
"Config file appears to be corrupt! You could delete it to refresh it to defaults."
)
sys.exit(-1)
# Config sanity checking:
if not os.path.isdir(os.path.expanduser(config[_CONFIG_SECTION_BASIC]['emote_dir'])):
l.error("Error parsing config: {} is not a directory!".format(
config[_CONFIG_SECTION_BASIC]['emote_dir']))
sys.exit(-1)
emote_dir = config[_CONFIG_SECTION_BASIC]['emote_dir']
if not emote_dir.endswith("/") and not emote_dir.endswith("\\"):
config[_CONFIG_SECTION_BASIC]['emote_dir'] += "/"
config[_CONFIG_SECTION_BASIC]['emote_dir'] = os.path.expanduser(
config[_CONFIG_SECTION_BASIC]['emote_dir'])
# Check archive directory exists.
if not os.path.isdir(config[_CONFIG_SECTION_BASIC]['emote_dir'] + 'archive/'):
l.warning("archive directory does not exist, creating...")
os.mkdir(config[_CONFIG_SECTION_BASIC]['emote_dir'] + 'archive/')
if update_database(force=args.force):
db = sqlite3.connect(db_file)
old_db = sqlite3.connect(db_file + '.old') if os.path.isfile(db_file + '.old') else None
else:
l.info("No updates available.")
sys.exit(0)
try:
emotes = get_emotes(sqlite3.connect(db_file),
old_db=None if args.force else old_db,
skip_already_downloaded=args.skip_already_downloaded)
l.info("Found {} new/updated emotes!".format(len(emotes)))
total_emotes = len(emotes)
digits = len(str(total_emotes))
def download_emote(emote):
"""A single job to download an emote
emote:The emote name"""
get_remote_file(
"/emoteCache/{}.png".format(emote[0]),
"".join([config[_CONFIG_SECTION_BASIC]["emote_dir"], emote[0], ".png"]),
save_last_modified=False,
)
return emote
with concurrent.futures.ThreadPoolExecutor() as executor:
for i, emote in enumerate(executor.map(download_emote, emotes)):
l.info(
"[{}/{}] Downloaded {}.png".format(
str(i + 1).rjust(digits), total_emotes, emote[0]
)
)
finally:
if os.path.isfile(db_file + '.old'):
os.remove(db_file + '.old')
l.info("Checking for removed emotes...")
removed = get_removed_emotes(list(e[0] for e in get_emotes(db)))
if removed: # Not empty
for e in removed:
os.rename(
"{}{}.png".format(config[_CONFIG_SECTION_BASIC]['emote_dir'], e),
"{}archive/{}.png".format(config[_CONFIG_SECTION_BASIC]['emote_dir'], e),
)
l.info("{} emote{} moved to archive.".format(
len(removed),
'' if len(removed) == 1 else 's')
)
else:
l.info("...none!")
l.info("All done!")