-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindpoem.py
54 lines (38 loc) · 1.73 KB
/
findpoem.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
#!/usr/bin/env python
# coding: latin-1
import pymongo, subprocess, pyqrcode
from pymongo import MongoClient
from random import randint
from PIL import Image
client = MongoClient()
db = client.pfoundation_data
poems = db.poems
count = poems.count()
print(count)
random = randint(1, count)
poem = poems.find_one({"number": random})
file = open('poem.html', 'w')
file.truncate()
file.write("<!DOCTYPE html><head><link href='css/index.css' type='text/css' rel='stylesheet' /></head><body><img src='images/magazine-heading.png' class='logo'>")
print(poem["number"])
file.write("<div id='title'>" + poem["title"][0].encode("ascii", "xmlcharrefreplace") + "</div>")
file.write("<div id='author'>by " + poem["author"][0].encode("ascii", "xmlcharrefreplace") + "</div>")
file.write("<div id='poem'>" + poem["poem_text"] + "</div>")
file.write("<div id='credit'>" + poem["credit"] + "</div>")
url = pyqrcode.create("http://poetryfoundation.org" + poem["url"])
url.png('poemqrcode.png', scale=3)
file.write("<div class='qrcode'><img src='poemqrcode.png'><p>Scan this QR Code to visit this poem online, or visit poetryfoundation.org " + poem["url"] + "</p></div>")
url = pyqrcode.create("http://poetryfoundation.org" + poem["author_link"][0])
url.png('authorqrcode.png', scale=3)
file.write("<div class='qrcode'><img src='authorqrcode.png'><p>Scan this QR Code for more by this author, or visit poetryfoundation.org " + poem["author_link"][0] + "</p></div>")
file.write("</body></html>")
file.close()
subprocess.call(["phantomjs", "phantom.js"])
im = Image.open("poem.png")
im = im.convert('1')
width, height = im.size
if width > 512:
box = (0, 0, 512, height)
im = im.crop(box)
im.save("poem.png")
subprocess.call("png2pos -c -s 1 poem.png > /dev/usb/lp0", shell=True)