forked from opengovfoundation/govify-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.py
114 lines (80 loc) · 3.77 KB
/
worker.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
import config
import time
import uuid
import subprocess
import os
import sendgrid
import pyrax
pyrax.set_setting("identity_type", "rackspace")
pyrax.set_setting("region", config.rackspace['API_REGION'])
def do_main_program() :
pyrax.set_credentials(config.rackspace['API_USERNAME'], config.rackspace['API_KEY'])
my_client_id = str(uuid.uuid4())
pq = pyrax.queues
pq.client_id = my_client_id
cf = pyrax.cloudfiles
in_container = cf.get_container(config.rackspace['API_FILES_IN'])
out_container = cf.get_container(config.rackspace['API_FILES_OUT'])
# We set the ttl and grace period to their minimum, 60 seconds.
# Get 1 at a time.
claim = pq.claim_messages(config.rackspace['API_QUEUE'], 60, 60, 1)
if claim and len(claim.messages) :
for msg in claim.messages:
#print 'Claimed {0}'.format([msg.body])
in_obj = in_container.get_object(msg.body['Tempname'])
# Generate a safe filename.
new_filename = '/tmp/' + msg.body['Tempname']
# Insert our data into that file.
f = open(new_filename, 'w')
f.write(in_obj.get())
f.close()
# If we successfully govify'd the document
try:
subprocess.check_call(["/usr/bin/govify", new_filename])
f = open(new_filename + '.pdf', 'r')
# Upload the new file
obj = out_container.store_object(msg.body['Tempname'] + '.pdf', f.read(),
content_type='application/pdf', ttl=config.rackspace['API_FILE_LIFETIME'])
f.close()
os.remove(new_filename + '.pdf')
# Remove the item from the inbox
# Do this via the container so the cache is cleared!
in_container.delete_object(msg.body['Tempname'])
# Remove the item from the queue
pq.delete_message(config.rackspace['API_QUEUE'], msg.id, claim.id)
# Notify the user
do_mail(msg.body['Author'], obj.get_temp_url(config.rackspace['API_FILE_LIFETIME']));
except subprocess.CalledProcessError:
print 'Something went wrong!'
# Remove our temp files.
os.remove(new_filename)
def do_mail(email, link) :
msg_txt = """Congratulations! Your .gov.ify-ed file is now available for download,
printing and sharing at the following URL:
{0}
Nice job! And while the file is real, this whole thing is really just an April Fool's
joke from the merry pranksters at the OpenGov Foundation. Hope you enjoyed it. Now,
come check out our authentic open data work and our serious open source projects on Github.
http://opengovfoundation.org/
""".format(link)
msg_html = """Congratulations! Your .gov.ify-ed file is now available for download, printing and
sharing at the following URL:<br/>
<br/>
<a href="{0}">{0}</a><br/>
<br/>
Nice job! And while the file is real, this whole thing is really just an April Fool's
joke from the merry pranksters at the <a href="http://opengovfoundation.org/">OpenGov Foundation</a>.
Hope you enjoyed it. Now, come check out our authentic <a href="http://americadecoded.org/">open
data work</a> and our serious <a href="https://github.com/opengovfoundation">open source projects</a> on Github.
""".format(link)
sg = sendgrid.SendGridClient(config.sendgrid['USERNAME'], config.sendgrid['PASSWORD'])
message = sendgrid.Mail(to=email, subject='.gov.ify : Your Government-Ready PDF Is Ready for Download!', html=msg_html, text=msg_txt, from_email=config.sendgrid['FROM'])
status, msg = sg.send(message)
def run():
while True:
do_main_program()
time.sleep(config.loop_sleep)
run()
# with daemon.DaemonContext():
# do_main_program()
# time.sleep(15)