-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (63 loc) · 1.98 KB
/
main.py
File metadata and controls
81 lines (63 loc) · 1.98 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
81
import cherrypy
import os, os.path
from bs4 import BeautifulSoup
import requests
import random
from hydrator import Hydrator, Route
session = requests.Session()
session.headers.update({
"Accept": "application/json",
"User-Agent": "hydrator/0.1"
})
class Root(object):
@cherrypy.expose
def default(self, *args, **kwargs):
# loading index.html into memory
raw_index = ""
with open("./www/index.html") as t:
raw_index = str(t.read())
# initializing hydrator object
h = Hydrator(raw_index, [
["/", Route],
["/testing", TestingRoute],
["default", DefaultRoute]
])
# gets path from request object
path = cherrypy.request.path_info
# generates meta and puts into DOM
h.hydrate_dom(path)
# gets new DOM with meta
dom = h.get_dom()
return dom
class TestingRoute(Route):
def generate(paths):
r = session.get("https://www.reddit.com/.json")
data = r.json()["data"]["children"]
index = random.randint(0, len(data)-1)
print(data[index])
return {
"og:title": data[index]["data"]["title"],
"og:image": data[index]["data"]["thumbnail"],
"og:description": data[index]["data"]["subreddit"]
}
class DefaultRoute(Route):
def generate(paths):
return {
"og:title": "throwback to default",
"og:video": "https://us.videos.epicio.net/public/full30/videos/MDI3MDY5/transforms/854x480.mp4",
"og:video:type": "video/mp4"
}
conf = {
'/': {
'tools.staticdir.on': True,
'tools.staticdir.root': os.path.abspath(os.getcwd()),
'tools.staticdir.dir': './www'
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './www/static'
}
}
if __name__ == "__main__":
cherrypy.quickstart(Root(), '/', conf)
wsgi_app = cherrypy.Application(Root(), '/', config=conf)