forked from nelsonjchen/cowboystyle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
80 lines (67 loc) · 2.56 KB
/
Copy pathdeploy.py
File metadata and controls
80 lines (67 loc) · 2.56 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
import sys
import praw
import datetime
import difflib
from pprint import pprint
import hashlib
import os
import subprocess
import astral
from jinja2 import Environment, PackageLoader, FileSystemLoader
env = Environment(loader=FileSystemLoader( 'templates'))
print("Beginning uploading CSS to Subreddit")
username = os.environ[ "REDDIT_USER" ]
password = os.environ[ "REDDIT_PASSWORD" ]
subreddit = os.environ[ "REDDIT_SUBREDDIT" ]
mode = os.environ[ "REDDIT_STYLESHEET_MODE" ]
compile_scss = os.environ[ "REDDIT_STYLESHEET_COMPILE" ] == 'true'
manage_sidebar = os.environ[ "REDDIT_SIDEBAR_MANAGE" ] == 'true'
sidebar_filename = os.environ[ "REDDIT_SIDEBAR_FILENAME" ]
if (mode == "day"):
filename = os.environ[ "REDDIT_STYLESHEET_DAY_FILENAME" ]
elif (mode == "night"):
filename = os.environ[ "REDDIT_STYLESHEET_NIGHT_FILENAME" ]
elif (mode == "auto"):
ucsb = astral.City(('University of California, Santa Barbara Main Campus',
'USA', 34.41254, -119.84813, 'US/Pacific'))
if (ucsb.solar_elevation() > 0):
print("AUTO: Day")
filename = os.environ[ "REDDIT_STYLESHEET_DAY_FILENAME" ]
else:
print("AUTO: Night")
filename = os.environ[ "REDDIT_STYLESHEET_NIGHT_FILENAME" ]
else:
filename = os.environ[ "REDDIT_STYLESHEET_DAY_FILENAME" ]
if (compile_scss):
subprocess.call("compass compile", shell=True)
USER_AGENT = "Subreddit SCSS Update Bot for /r/%s" % subreddit
print(USER_AGENT)
print("Uploading %s to %s" % (filename, subreddit))
with open(filename, 'r') as file:
style = file.read()
print("Going to Reddit %s" % subreddit)
r = praw.Reddit(user_agent = USER_AGENT)
r.login(username, password)
print("Logged in as %s" % username)
sr = r.get_subreddit(subreddit)
print("Got Subreddit %s" % subreddit)
if (manage_sidebar):
print("Rendering sidebar markdown")
sidebar = env.get_template(sidebar_filename).render(subreddit=subreddit)
print("Setting sidebar's markdown")
sr.update_settings(description = sidebar)
print("Hashing Stylesheet")
style_hash = hashlib.md5(style.encode('UTF-8'))
style = style + "/*" + style_hash.hexdigest() + "*/"
print("Setting Stylesheet")
sr.set_stylesheet(style)
style_set = sr.get_stylesheet()['stylesheet']
print("Checking Stylesheet")
if (style_set.find(style_hash.hexdigest()) == -1):
print("Style that was uploaded was invalid. Upload manually for errors")
d = difflib.Differ()
result = list(d.compare(style_set.splitlines(1),style.splitlines(1)))
pprint(result)
else:
print("Sucessfully uploaded CSS to Subreddit %s" % subreddit)
print(datetime.datetime.now().ctime())