-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
231 lines (190 loc) · 6.38 KB
/
controller.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import logging
from datetime import datetime
import evernote.edam.type.ttypes as Types
import evernote.edam.error.ttypes as Errors
from evernote.api.client import EvernoteClient
import models as m
import config as conf
def update_user(access_token):
"update or save user"
client = get_evernote_client(access_token)
user_store = client.get_user_store()
user = user_store.getUser()
if not user:
return None
if not m.update_user_evernote(user.id, user.username, access_token):
return None
return user
def get_notebook(id, access_token):
"get notebook, if not exsits create it."
client = get_evernote_client(access_token)
note_store = client.get_note_store()
guid = m.get_user_guid(id)
if guid:
notebook = note_store.getNotebook(guid)
else:
notebook = create_notebook(id, note_store)
if not notebook:
return None
return notebook
def create_notebook(id, note_store):
"create notebook and svaing to db"
notebook = _create_notebook(conf.notebook_name, note_store)
if not notebook:
return None
if not m.update_user_guid(id, notebook.guid):
return None
return notebook
def _create_notebook(name, note_store):
try:
notebook = Types.Notebook()
notebook.name = name
created_notebook = note_store.createNotebook(notebook)
except Errors.EDAMUserException, e:
logging.error("CreateNoteBook Error",e)
return None
return created_notebook
def makeNote(authToken, noteStore, noteTitle, noteBody, parentNotebook=None):
nBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
nBody += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">"
nBody += "<en-note>%s</en-note>" % noteBody
## Create note object
ourNote = Types.Note()
ourNote.title = noteTitle
ourNote.content = nBody
## parentNotebook is optional; if omitted, default notebook is used
if parentNotebook and hasattr(parentNotebook, 'guid'):
ourNote.notebookGuid = parentNotebook.guid
## Attempt to create note in Evernote account
try:
note = noteStore.createNote(authToken, ourNote)
except Errors.EDAMUserException, edue:
## Something was wrong with the note data
## See EDAMErrorCode enumeration for error code explanation
## http://dev.evernote.com/documentation/reference/Errors.html#Enum_EDAMErrorCode
print "EDAMUserException:", edue
return None
except Errors.EDAMNotFoundException, ednfe:
## Parent Notebook GUID doesn't correspond to an actual notebook
print "EDAMNotFoundException: Invalid parent notebook GUID", ednfe
return None
## Return created note object
return note
def get_evernote_client_via_id(id):
"using user.id get EvernoteClient"
access_token = m.get_user_token(int(id))
return get_evernote_client(access_token)
def get_evernote_client(access_token = None):
"return EvernoteClient"
if access_token:
return EvernoteClient(
token=access_token,
sandbox=True)
else:
client = EvernoteClient(
consumer_key=conf.consumer_key,
consumer_secret=conf.consumer_secret,
sandbox=True # Default: True
)
return client
def connect_facebook(id, user):
return m.update_user_sns(id, user, "facebook")
def connect_twitter(id, user):
return m.update_user_sns(id, user, "twitter")
def connect_foursquare(id, user):
return m.update_user_sns(id, user, "foursquare")
def get_facebook_token(enid):
return m.get_facebook_token(enid)
def get_twitter_token(enid):
return m.get_twitter_token(enid)
def get_foursquare_token(enid):
return m.get_sns_token(enid, "foursquare")
def update_daily_facebook(enid, fbid, data):
daily=[]
for d in data["data"]:
try:
_daily = dict(
ddid = d["id"],
message = d["message"],
created_time = datetime.strptime(
d["created_time"],
"%Y-%m-%dT%H:%M:%S+0000") )
except:
continue
else:
_daily.update(
id = enid,
sid = fbid,
source = "facebook",
picture = [d.get("full_picture")],
description = d.get("description")
)
daily.append(_daily)
return m.save_daily(daily)
def get_since_id(enid, twid):
return m.get_since_id(enid, twid)
def get_media(data):
"return media url list from entities"
medias = []
if not data:
return None
if not data.get("media"):
return None
for m in data.get("media"):
if m.get("media_url"):
medias.append(m.get("media_url"))
return medias
def update_daily_twitter(enid, twid, data):
daily = []
for d in data:
try:
_daily = dict(
ddid = d["id"],
message = d["text"],
created_time = datetime.strptime(
d["created_at"],
"%a %b %d %H:%M:%S +0000 %Y") )
except:
continue
else:
_daily.update(
id = enid,
sid = twid,
source = "twitter",
picture = get_media(d.get("entities") ),
description = None
)
daily.append(_daily)
return m.save_daily(daily)
def update_daily_foursuare(enid, foursquare_id, data):
daily = []
for d in data.get("response").get("checkins"):
try:
_daily = dict(ddid = d["id"],
created_at = datetime.datetime.fromtimestamp(
d["createdAt"]),
message = d["shot"],
venue = d["venue"])
except:
continue
else:
_daily.update(
id = enid,
sid = foursquare_id,
picture = get_picture(d.get("photos").get("items")),
source = "foursquare",
description = None
)
daily.append(_daily)
return m.save_daily(daily)
def get_picture(data):
photos = []
for p in data:
photos.append("%swidth%s%s" %
( p["prefix"], p["width"], p["suffix"] )
)
return photos
def get_user(enid):
return m.get_user(enid)