-
Notifications
You must be signed in to change notification settings - Fork 1
/
hackdisrupt.py
303 lines (269 loc) · 10.4 KB
/
hackdisrupt.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# -*- coding: utf-8 -*-
from flask import Flask, jsonify, Response
import requests
import os
from ConcurOAuthPython import getbasic, getTokenGivenUsernamePasswordAndConsumerKey
from BeautifulSoup import BeautifulSoup
from datetime import datetime, timedelta
import hashlib
import binascii
import time
from evernote.edam.notestore.ttypes import NoteFilter
import evernote.edam.type.ttypes as Types
from evernote.api.client import EvernoteClient
# Sandbox: https://sandbox.evernote.com/api/DeveloperToken.action
# Production: https://www.evernote.com/api/DeveloperToken.action
auth_token = "S=s1:U=8f65c:E=14fa7113473:C=1484f600570:P=1cd:A=en-devtoken:V=2:H=484744fd0ffa906797416ae02ce5cd9c"
#Constants
USER = "[email protected]"
PWD = "disrupt"
KEY = 'mbGLfEhU1rUUG8IOImdmVt'
#TOKEN = 'NPuB/Wr+a9OkYNWz9Mw/kWNWvyk='
SAMPLE_REPORT_NAME = 'Disrupt Hackathon NYC Trip'
SAMPLE_REPORT_ID = 'D7E30A5A1E1F4ACF82FE'
city_name = "NYC"
TripID = 'nHyQT$pcgFtPlchwTlyhTG$pjyel9xI$sVJ9OTTrUw'
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
#Initializing FLASK app
app = Flask(__name__)
#Getting the Current TOKEN
TOKEN = getTokenGivenUsernamePasswordAndConsumerKey('[email protected]', 'disrupt', KEY)['Access_Token']['Token']
#oauth
auth = {
'Authorization': "OAuth %s" % TOKEN,
'Accept':'application/xml',
'lastModifiedDate': '2001-01-01' #This will get older itinerariesk
}
client = EvernoteClient(token=auth_token, sandbox=True)
note_store = client.get_note_store()
# Listar todos notebooks:
notebooks = note_store.listNotebooks()
# print "Achei ", len(notebooks), " notebooks:"
# for notebook in notebooks:
# print " * ", notebook.name
# Criar uma nova nota:
# print "\nCriando uma nova nota no notebook principal\n"
note = Types.Note()
note.title = "Business Travel to %s" % (city_name)
# Anexando uma imagem:
image = open( os.path.join(SITE_ROOT, 'NY.png'), 'rb').read()
md5 = hashlib.md5()
md5.update(image)
hash = md5.digest()
data = Types.Data()
data.size = len(image)
data.bodyHash = hash
data.body = image
resource = Types.Resource()
resource.mime = 'image/png'
resource.data = data
# Adicionando o novo Resource na lista de resources dessa nota
note.resources = [resource]
# Para exibir a imagem no meio da nota, soh precisamos do hash MD5 dela
hash_hex = binascii.hexlify(hash)
# ENML = Evernote Markup Language. It's an HTML subset with some extra tags
# note.content = '<?xml version="1.0" encoding="UTF-8"?>'
# note.content += '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'
# note.content += '<en-note>'
# note.content += 'Esta eh uma nova nota, inserida direto no notebook principal :)<br/>'
# note.content += 'Olha aqui o logo do Evernote:<br/>'
# note.content += '<en-media type="image/png" hash="' + hash_hex + '"/>'
# note.content += '</en-note>'
flight_bool, history_bool, taxi_bool, hotel_bool, weather_bool, tip_bool = True, True, True, True, True, True
@app.route('/')
def home():
pass
@app.route('/itineraries/')
def itineraries():
"""
Returns all itineraries
"""
resp = requests.get('https://www.concursolutions.com/api/travel/trip/v1.1',headers = auth)
return Response(resp.text, mimetype='application/xml')
@app.route('/itinerary/info')
def itinerary_info(TripID = 'nHyQT$pcgFtPlchwnqtoRjd1MgMLqRPunn4pQShg'):
"""
Returns all itineraries
"""
TripID = 'nHyQT$pcgFtPlchwTlyhTG$pjyel9xI$sVJ9OTTrUw'
resp = requests.get('https://www.concursolutions.com/api/travel/trip/v1.1/%s' % TripID,headers = auth)
root = BeautifulSoup(resp.text)
note=""
for booking in root.bookings.findAll('booking'):
for air in booking.findAll('air'):
note += """
<div>
<ul><li><strong>Flight</strong></li></ul>
</div>
<div>%s -> %s</div>
<div><en-todo/> arrive on %s by %s</div>
<div><en-todo/> takeoff is at %s</div>
<hr></hr>
""" % (
air.startcitycode.text,
air.endcitycode.text,
air.startcitycode.text,
(datetime.strptime(air.startdatelocal.text, '%Y-%m-%dT%H:%M:%S') - timedelta(hours=3)).strftime("%m/%d - %H:%M"),
datetime.strptime(air.startdatelocal.text, '%Y-%m-%dT%H:%M:%S').strftime("%H:%M"),
)
note += """
<div>
<ul><li><strong>Hotel</strong></li></ul>
</div>
<div>You're staying at:</div>
<div><a href="https://www.google.com/maps/preview?q=Times+Square+Hilton+New+York">%s</div>
<hr></hr>
""" % (
root.bookings.findAll('booking')[0].hotel.find('name').text,
)
#Counting number of Taxi Cabs
resp = requests.get('https://www.concursolutions.com/api/v3.0/expense/entries/',headers = auth)
root = BeautifulSoup(resp.text)
cont_cabs = 0
for t in root.findAll('expensetypecode'):
if t.text == 'TAXIX':
cont_cabs = cont_cabs + 1
note += """
<div>
<ul><li><strong>Taxi</strong></li></ul>
</div>
<div>The last time you've been there you used %s taxi cabs.</div>
<div><en-todo/> Download <a href="http://www.uber.com/">Uber</a> (it works the best in %s)</div>
<hr></hr>
"""% (
cont_cabs,
'NYC',
)
return note
#return Response(resp.text, mimetype='application/xml')
@app.route('/itinerary/all')
def all_itinerary_info():
resp = requests.get('https://www.concursolutions.com/api/travel/trip/v1.1',headers = auth)
root = BeautifulSoup(resp.text)
note = ""
for i in root.findAll("itineraryinfo"):
note += itinerary_info(i.tripid.text)
return note
@app.route('/entries/')
def entries():
"""
Returns all expense entries
"""
#Expense Entries
resp = requests.get('https://www.concursolutions.com/api/v3.0/expense/entries/',headers = auth)
return Response(resp.text, mimetype='application/xml')
@app.route('/integrate/')
def integrate():
note.content = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note>
<div>
"""
if flight_bool:
resp = requests.get('https://www.concursolutions.com/api/travel/trip/v1.1/%s' % TripID,headers = auth)
root = BeautifulSoup(resp.text)
for booking in root.bookings.findAll('booking'):
for air in booking.findAll('air'):
note.content += """
<div>
<ul><li><strong>Flight</strong></li></ul>
</div>
<div>%s -> %s</div>
<div><en-todo/> arrive on %s by %s</div>
<div><en-todo/> takeoff is at %s</div>
<hr></hr>
""" % (
air.startcitycode.text,
air.endcitycode.text,
air.startcitycode.text,
(datetime.strptime(air.startdatelocal.text, '%Y-%m-%dT%H:%M:%S') - timedelta(hours=3)).strftime("%m/%d - %H:%M"),
datetime.strptime(air.startdatelocal.text, '%Y-%m-%dT%H:%M:%S').strftime("%H:%M"),
)
if hotel_bool:
note.content += """
<div>
<ul><li><strong>Hotel</strong></li></ul>
</div>
<div>You're staying at:</div>
<div>%s</div>
<hr></hr>
""" % (
root.bookings.findAll('booking')[0].hotel.find('name').text,
)
if taxi_bool:
#Counting number of Taxi Cabs
resp = requests.get('https://www.concursolutions.com/api/v3.0/expense/entries/',headers = auth)
root = BeautifulSoup(resp.text)
cont_cabs = 0
for t in root.findAll('expensetypecode'):
if t.text == 'TAXIX':
cont_cabs = cont_cabs + 1
note.content += """
<div>
<ul><li><strong>Taxi</strong></li></ul>
</div>
<div>The last time you've been there you used %s taxi cabs.</div>
<div><en-todo/> Download <a href="http://www.uber.com/">Uber</a> (it works the best in %s)</div>
<hr></hr>
"""% (
cont_cabs,
'NYC',
)
if weather_bool:
note.content += """
<div>
<ul><li><strong>Weather</strong></li></ul>
</div>
<div>It'll be summertime in """+ city_name +"""</div>
<div><en-todo/> Grab a coat, it'll be rainy</div>
<hr></hr>
"""
def get_tip_notes(start_date, end_date):
tip_notes = []
noteFilter = NoteFilter()
noteFilter.words = "tag:tip created:%s -created:%s" % (start_date, end_date) # notes with tag #tip created between 2012-01-01 and 2014-09-08 (flight dates)
note_list = note_store.findNotes(auth_token, noteFilter, 0, 10)
for note in note_list.notes:
guid = note.guid
title = note.title
user_id = "587356"
shard_id = "s1"
url = "evernote:///view/%s/%s/%s/%s/" % (user_id, shard_id, guid, guid)
tip_notes.append( '<div><en-todo/> %s (<a href="%s">view full note</a>)</div>' % (title, url) )
return tip_notes
if tip_bool:
note.content += """
<div>
<ul><li><strong>#tip</strong></li></ul>
</div>
<div>When you last visited there, you wrote the following #tip:</div>"""
tip_notes = get_tip_notes("20120101", "20140908")
for tip_note in tip_notes:
note.content += tip_note
note.content += """
<hr></hr>
"""
note.content += """
</div>
"""
note.content += """
<div>
<br clear="none"/>
<en-media type="image/png" hash="%s"/>
</div>
</en-note>""" % (hash_hex)
# Set our beloved bizmem tag :)
note.tagNames = ["bizmem"]
# Set the reminder
# for demo purposes, we'll ignore the actual useful reminder date (two days before the flight) and set it to today
now = int(round(time.time() * 1000))
then = now + 60*60*1000 # one hour after `now`
# init NoteAttributes instance
note.attributes = Types.NoteAttributes()
note.attributes.reminderOrder = now
note.attributes.reminderTime = then
# Finally, save the note
created_note = note_store.createNote(note)
return "Note created successfully! GUID: %s" % (created_note.guid)
if __name__ == '__main__':
app.run(debug = True)