-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
134 lines (104 loc) · 4.57 KB
/
server.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
from flask import Flask, render_template, request
import requests
from evernote.api.client import EvernoteClient
import hashlib
import evernote.edam.type.ttypes as Types
import evernote.edam.notestore.ttypes as NoteStoreTypes
import evernote.edam.notestore.NoteStore as NoteStore
from evernote.edam.notestore.ttypes import NotesMetadataResultSpec
import binascii
giphy_api_key="dc6zaTOxFJmzC" #public beta key
evernote_auth_token = "put evernote dev token here"
EN_URL="https://sandbox.evernote.com"
app=Flask(__name__)
@app.route("/", methods=['POST','GET'])
def main():
""" GET: gets random gif from giphy and displays it along with the option to see another gif and to
save the gif to their evernote account"""
if request.method == "GET":
#get random gif from giphy api
response=requests.get("http://api.giphy.com/v1/gifs/random?api_key="+giphy_api_key).json()
if not response['meta']['msg']=='OK':
return "error with connection to giphy"
#get random image url and id from giphy api response
giphy_url=response['data']['image_url']
giphy_id=response['data']['id']
#get tags and pass them to the page because the giphy api only show tags for random images
giphy_tags=''
for tag in response['data']['tags']:
giphy_tags+=tag+', '
giphy_tags=giphy_tags[:-2]
return render_template("index.html", giphy_url=giphy_url, giphy_id=giphy_id, giphy_tags=giphy_tags)
"""POST: shows confirmation of evernote gif save and presents option
to return to main page or see the note in evernote"""
if request.method == 'POST':
if request.form['giphy_id'] and request.form['giphy_tags']:
giphy_id=request.form['giphy_id']
giphy_tags=request.form['giphy_tags']
response=requests.get("http://api.giphy.com/v1/gifs/"+giphy_id+"?api_key="+giphy_api_key).json()
giphy_url=response['data']['images']['original']['url']
client = EvernoteClient(token=evernote_auth_token, sandbox=True)
user_store = client.get_user_store()
note_store = client.get_note_store()
notebooks = note_store.listNotebooks()
#check if giphy notebook exists
for notebook in notebooks:
if notebook.name=="Giphy":
giphyNotebookGuid=notebook.guid
break
#if not create it
try:
giphyNotebookGuid
except NameError:
notebook=Types.Notebook()
notebook.name="Giphy"
notebook=note_store.createNotebook(notebook)
giphyNotebookGuid=notebook.guid
#create note title with user name + giphy id for unique identifier
note_title=response['data']['username']+"-"+response['data']['id']
#check to see if note exists already
notebook_filter=NoteStoreTypes.NoteFilter()
notebook_filter.guid=giphyNotebookGuid
result_spec = NotesMetadataResultSpec(includeTitle=True)
noteList = note_store.findNotesMetadata(evernote_auth_token, notebook_filter,0 , 40000, result_spec)
for note in noteList.notes:
if note.title==note_title:
shardId=user_store.getUser(evernote_auth_token).shardId
shareKey=note_store.shareNote(evernote_auth_token, note.guid)
evernote_url="%s/shard/%s/sh/%s/%s" % (EN_URL,shardId,note.guid,shareKey)
return render_template("already_there.html", giphy_url=giphy_url, evernote_url=evernote_url)
#get image
image= requests.get(giphy_url, stream=True).content
md5 = hashlib.md5()
md5.update(image)
gif_hash = md5.digest()
data = Types.Data()
data.size = len(image)
data.bodyHash = gif_hash
data.body = image
resource = Types.Resource()
resource.mime = 'image/gif'
resource.data = data
hash_hex = binascii.hexlify(gif_hash)
note = Types.Note()
note.notebookGuid=giphyNotebookGuid #create note for our Giphy notebook
note.title=note_title #name based on Giphy username and id
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>'+giphy_tags+'<br/>' #add tags to note contents
note.content += '<en-media type="image/png" hash="' + hash_hex + '"/>'
note.content += '</en-note>'
note.resources = [resource] # Now, add the new Resource to the note's list of resources
note=note_store.createNote(note) # create the note
user=user_store.getUser(evernote_auth_token)
shardId=user.shardId
shareKey=note_store.shareNote(evernote_auth_token, note.guid)
evernote_url="%s/shard/%s/sh/%s/%s" % (EN_URL,shardId,note.guid,shareKey)
return render_template("saved.html", giphy_url=giphy_url, evernote_url=evernote_url)
else:
return "error finding the gif"
else:
return "server error"
if __name__=="__main__":
app.run(debug=True)