-
Notifications
You must be signed in to change notification settings - Fork 0
/
poem.py
42 lines (29 loc) · 1003 Bytes
/
poem.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
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
import random
import json
import urllib.request
import urllib.parse
def main():
try:
contents = urllib.request.urlopen("http://poetrydb.org/title").read()
readable = contents.decode('utf-8')
data = json.loads(readable)
poemUrl = "http://poetrydb.org/title/" + urllib.parse.quote((data['titles'][random.randint(0,2971)]), safe='-\"\\,.:;[]/!’()É_`?*=\'')
contents = urllib.request.urlopen(poemUrl).read()
readable = contents.decode('utf-8')
data = json.loads(readable)
title = data[0]['title']
author = data[0]['author']
lines = data[0]['lines']
poem = "\n"
print ("Author: " + author + "\n")
print ("Title: " + title)
for i in range(0,len(lines)):
poem = poem + (lines[i]) + " \n"
print (poem)
return data
except:
print ("Something went wrong!")
if __name__ == '__main__':
main()