-
Notifications
You must be signed in to change notification settings - Fork 23
/
dlanimotes.py
executable file
·110 lines (96 loc) · 3.67 KB
/
dlanimotes.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
#!/usr/bin/env python3
# -*- coding: utf8 -*-
################################################################################
##
## This file is part of BetterPonymotes.
## Copyright (c) 2012-2015 Typhos.
##
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU Affero General Public License as published by
## the Free Software Foundation, either version 3 of the License, or (at your
## option) any later version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
## for more details.
##
## You should have received a copy of the GNU Affero General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
################################################################################
import argparse
import hashlib
import os
import subprocess
import time
import urllib.request
import bplib
import bplib.objects
import bplib.resolve
AutogenHeader = """
/*
* This file is AUTOMATICALLY GENERATED. DO NOT EDIT.
* Generated at %s.
*/
""" % (time.strftime("%c"))
TempFilename = "animote-temp.png"
AnimoteUrlPrefix = "https://ponymotes.net/"
def find_animotes(emotes):
images = {}
for (name, emote) in emotes.items():
if emote.source.variant_matches is None:
emote.source.match_variants()
root = emote.source.variant_matches[emote]
#if root is None:
# print("ERROR:", emote)
if "+animote" in root.tags:
images.setdefault(emote.base_variant().image_url, []).append(emote)
return images
def image_path(url):
clean = bplib.clean_image_url(url)
assert clean.endswith(".png")
filename = "animotes/" + clean[:-4] + ".gif"
return filename
def update_cache(images):
for (i, url) in enumerate(images):
gif_filename = image_path(url)
if os.path.exists(gif_filename):
continue
download_url = bplib.image_download_url(url)
print("[%s/%s] Original URL: %s" % (i + 1, len(images), url))
print("[%s/%s] Download URL: %s" % (i + 1, len(images), download_url))
print("[%s/%s] Target file: %s" % (i + 1, len(images), gif_filename))
print()
req = urllib.request.Request(download_url)
with urllib.request.urlopen(req) as stream:
data = stream.read()
open(TempFilename, "wb").write(data)
subprocess.call(["apng2gif", TempFilename, gif_filename])
os.remove(TempFilename)
def dump_css(file, images):
file.write(AutogenHeader)
for (url, emotes) in images.items():
selectors = []
for emote in emotes:
for variant in emote.variants.values():
if hasattr(variant, "image_url") and variant.image_url == url:
selectors.append(".bpm-emote" + variant.selector())
selector = ",".join(selectors)
new_url = AnimoteUrlPrefix + image_path(url)
s = "%s{background-image:url(%s)!important}\n" % (selector, new_url)
file.write(s)
def main():
parser = argparse.ArgumentParser(description="Download and convert APNG animotes to GIF")
parser.add_argument("-c", "--css", help="Output CSS file", default="build/gif-animotes.css")
args = parser.parse_args()
context = bplib.objects.Context()
context.load_config()
context.load_sources()
emotes, all_emotes = bplib.resolve.resolve_emotes(context)
images = find_animotes(emotes)
update_cache(images)
with open(args.css, "w") as file:
dump_css(file, images)
if __name__ == "__main__":
main()