-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
53 lines (44 loc) · 2 KB
/
app.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
from flask import Flask, request, render_template, jsonify
from flaskext.markdown import Markdown
import json
import requests
import urllib
from scrapy.selector import Selector
app = Flask(__name__)
Markdown(app)
@app.route('/')
def help():
return render_template('help.html')
@app.route('/favicon.ico')
def favicon():
return ''
@app.route('/<path:url>')
def process(url):
# Reconstitute url querystrings that flask got greedy with
querystrings = urllib.parse.urlencode(request.args)
url = url + '?' + querystrings
return jsonify(get_data(url))
def get_data(url):
r = requests.get(url)
sel = Selector(r)
if sel.xpath('//meta[@property="og:type"]/@content').extract_first() == 'events.event':
data = {
'url': sel.xpath('//link[@rel="canonical"]/@href').extract_first(),
'title': sel.xpath('//meta[@property="og:title"]/@content').extract_first(),
'description': sel.xpath('//meta[@property="og:description"]/@content').extract_first().strip(),
'start_time': sel.xpath('//meta[@property="event:start_time"]/@content').extract_first(),
'end_time': sel.xpath('//meta[@property="event:end_time"]/@content').extract_first(),
'location': sel.xpath('//meta[@name="twitter:data1"]/@value').extract_first(),
}
return data
if sel.xpath('//meta[@property="og:type"]/@content').extract_first() == 'uniiverse:listing':
data = {
'url': sel.xpath('//link[@rel="canonical"]/@href').extract_first(),
'title': sel.xpath('//meta[@property="og:title"]/@content').extract_first(),
'description': sel.xpath('//*[@itemprop="description"]/text()').extract_first().strip(),
'start_time': sel.xpath('//*[@itemprop="startDate"]/text()').extract_first(),
'end_time': sel.xpath('//*[@itemprop="endDate"]/text()').extract_first(),
'location': sel.xpath('//*[@itemprop="location"]/text()').extract_first(),
}
return data
return {}