-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
109 lines (97 loc) · 3.35 KB
/
main.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from flask import Flask, request, redirect
import sys, os, json
from YQL import YQL
from StocksQParser import *
from xml.etree import ElementTree
import XMLGenerator
import sentry_sdk
sentry_sdk.init(
dsn="https://4c85f15fca6c5ba16a905745f395a5eb@o4506249211281408.ingest.sentry.io/4506249213181952",
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
traces_sample_rate=1.0,
# Set profiles_sample_rate to 1.0 to profile 100%
# of sampled transactions.
# We recommend adjusting this value in production.
profiles_sample_rate=1.0,
)
app = Flask(__name__)
sys.stdout.reconfigure(encoding='utf-8')
yql = YQL()
# Ignore these, testing :P
@app.route('/')
def hello_world():
return "Sup"
@app.route("/errort")
def errort():
1/0 # raises an error
return "<p>Hello, World!</p>"
# Stocks
@app.route('/dgw', methods=["POST", "GET"])
def dgw():
print("Legacy app found!")
if request.method == 'GET':
return "ok", 200
sentXML = request.data.decode()
root = ElementTree.fromstring(sentXML)
type = root[0].attrib['type']
api = root.attrib['api']
print(sentXML)
if api == "finance":
print("Using finance")
q = parseQuery(sentXML)
return XMLGenerator.getStocksXMLWithQandType(q, type)
elif api == 'weather':
print("Using Weather")
return legacyWeatherDGW()
# Weather
# iOS 5 seems to use this endpoint, let's redirect to the regular function
@app.route('/v1/yql')
def legacyWeatherYQL():
print("Legacy app found!")
return weatherEndpoint()
@app.route('/yql/weather/dgw', methods=["POST"])
def legacyWeatherDGW():
print("Pre iOS 5 app found!")
if request.data:
sentXML = request.data.decode()
reqType = sentXML[sentXML.index("y id=")+5:sentXML.index(" time")].replace('"', "")
print(reqType)
if reqType == "3":
q = sentXML[sentXML.index("ase>")+4:sentXML.index("</phr")]
if "|" in q:
q = q.split("|")[1]
return XMLGenerator.getLegacyWeatherSearchXMLWithYQLandQ(yql, q)
if reqType == "30":
q = sentXML[sentXML.index("id>")+3:sentXML.index("</id")]
if "|" in q:
q = q.split("|")[1]
print("q = " + q)
return XMLGenerator.getLegacyWeatherXMLWithYQLandQ(yql, q)
return ""
# iOS 6 contacts this endpoint for all things weather
@app.route('/yql/weather')
def weatherEndpoint():
# Get the request and set the request for the yql handling object
q = request.args.get('q')
if q:
print(q)
if 'partner.weather.locations' and not 'yql.query.multi' in q:
# Search Request
q = q[q.index('query="')+7:q.index('" a')]
return searchReq(q)
elif 'partner.weather.forecasts' in q:
# Weather Request
return weatherReq(q)
def searchReq(q):
return XMLGenerator.getWeatherSearchXMLWithYQLandQ(yql, q)
def weatherReq(q):
if "lat=" in q:
return XMLGenerator.getWeatherXMLWithYQLandLatLonginQ(yql, q)
return XMLGenerator.getWeatherXMLWithYQLandQ(yql, q)
if __name__ == '__main__':
if not os.path.exists("generatedWoeids.json"):
with open("generatedWoeids.json", "w") as database:
database.write(json.dumps({}))
database.close()
app.run(host="0.0.0.0", port=5002)