-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathapp.py
50 lines (41 loc) · 1.61 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
import os
import requests
from lxml import html
from flask import request
from flask import Flask
from flask import Response
app = Flask(__name__)
@app.route('/')
def home():
usage = 'Pass a properly encoded url parameter e.g. /https/www.google.com'
return usage
@app.route('/https/<url>')
def root(url):
url = 'https://' + url
r = requests.get(url)
rr = Response(response=r.content, status=r.status_code)
rr.headers["Content-Type"] = r.headers['Content-Type']
return rr
@app.route('/g/<keyword>')
def gkeyword(keyword):
url = 'https://www.google.com/search'
payload = {'q':keyword, 'num':1, 'start':1, 'sourceid':'chrome', 'ie':'UTF-8', 'cr':'cr=countryUS'}
r = requests.get(url, params=payload)
rr = Response(response=r.content, status=r.status_code)
rr.headers["Content-Type"] = r.headers['Content-Type']
return rr
@app.route('/r/<subreddit>/subscribers')
def gsubreddit(subreddit):
url = 'https://old.reddit.com/r/' + subreddit
xpath ="//span[@class='subscribers']/span[@class='number']/text()"
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
r = requests.get(url, headers=headers)
tree = html.fromstring(r.content)
subscribers = tree.xpath(xpath)
rr = Response(response=subscribers, status=r.status_code)
rr.headers["Content-Type"] = r.headers['Content-Type']
return rr
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 8000.
port = int(os.environ.get('PORT', 8000))
app.run(host='0.0.0.0', port=port)