-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht2f.py
More file actions
181 lines (147 loc) · 5.4 KB
/
t2f.py
File metadata and controls
181 lines (147 loc) · 5.4 KB
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
import cgi
import flickrUpload
import flickr
import logging
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp.util import run_wsgi_app
# extend db to store info relating to the photos
class Photo(db.Model):
message = db.StringProperty()
tags = db.StringProperty()
date = db.DateTimeProperty(auto_now_add=True)
data = db.BlobProperty(required=True)
mimetype = db.StringProperty(required=True)
photo_id = db.StringProperty()
shortURL = db.StringProperty()
class Tag(db.Model):
tag_id = db.Key()
post_id = int()
name = db.StringProperty()
url = db.Link()
class Auth(db.Model):
api_key = db.StringProperty()
api_secret = db.StringProperty()
token = db.StringProperty()
# the main page of the webapp
class MainPage(webapp.RequestHandler):
def get(self):
#token = db.GqlQuery("SELECT token FROM Auth")
photos = db.GqlQuery("SELECT * FROM Photo ORDER BY date DESC LIMIT 10")
TODO:
for photo in photos:
date_str = photo.date.isoformat(' ')[0:16]
tags = photo.tags.split(' ')
self.response.out.write("""
<tr class="rowdiv" onMouseOver="this.className='highlight'" onMouseOut="this.className='rowdiv'">
<td class="celldiv">%s</td>
<td class="celldiv">%s</td>
<td class="celldiv">
""" % (date_str, photo.message) )
for tag in tags:
self.response.out.write('<a href="http://flickr.com/photos/xosrow/tags/' + tag + '">' + tag + '</a> ')
self.response.out.write("</td></tr>")
self.response.out.write("""</table>
</body>
</html>""")
# PicPoster page presents a form to user to post pics. For debug only
class PicPoster(webapp.RequestHandler):
def get(self):
self.response.out.write("""
<html>
<head><title>Pic Poster test</title></head>
<body>
<h2 style="font-family: Sans-Serif;">Select your photo</h2>
<div style="font-family: Sans-Serif; display:block; background:#eee; border:1px solid #ccc; width:80%; border-bottom-left-radius: 5px 5px; border-bottom-right-radius: 5px 5px; border-top-left-radius: 5px 5px; border-top-right-radius: 5px 5px;padding: 10px 20px; margin-left: auto; margin-right: auto;">
<form action="/uploader" method="post" enctype="multipart/form-data">
message: <input type="text" name="message"><br>
File: <input type="file" name="media"><br>
<input type="submit">
</form>""")
self.response.out.write('</body></html>')
# uploader page is what gets called by Twitter for iPhone
class Uploader(webapp.RequestHandler):
def post(self):
tweet = self.request.get('message')
words = tweet.split(' ')
message = ''
myTags = ''
# setting API key and secret manually for now
flickr.API_KEY = 'd6d991956d33e88d1114ac377f4b0467'
flickr.API_SECRET = 'e227ded1f7148092'
# break up the tweet. Remove hashtags and send them separately as tags.
for word in words:
if word.rfind('#') == 0:
junk1, junk2, tag = word.partition('#')
myTags += tag + ' '
logging.debug("tag: %s" % word)
else:
message += word + ' '
# add the twitter tag by default
myTags += 'twitter'
# get the uploaded file and it's mimetype
file = self.request.POST['media']
# record the info in the db table
photo = Photo(data=file.value, mimetype=file.type)
photo.message = message
photo.tags = myTags
photo.put()
# some debug info to know where the post came from
headers = self.request.headers
logging.debug(headers)
logging.debug(self.request.arguments())
logging.debug("mitmetype: %s" % file.type)
# upload the image - - debug mode for now
photo = flickrUpload.upload(filedata=file.value, mimetype=file.type, title=photo.message, tags=myTags)
photoid = photo.__getattr__('id')
encodedid = encode58(photoid)
# if we are here the flickrError exception hasn't been thrown
# and we can continue to send the reponse with stat=ok
myResponse = """<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok">
<mediaid>%s</mediaid>
<mediaurl>http://flic.kr/p/%s</mediaurl>
</rsp>""" % (photoid, encodedid)
# test debug message
#myResponse = """<?xml version="1.0" encoding="UTF-8"?>
#<rsp stat="ok">
#<mediaid>abc123</mediaid>
#<mediaurl>http://yfrog.com/835fzcj</mediaurl>
#</rsp>"""
logging.debug("writing response %s" % myResponse)
# send a the response back to caller
self.response.out.write(myResponse)
# next line is useful for later
# redirect back to main page
#self.redirect('/')
application = webapp.WSGIApplication([('/', MainPage),
('/picposter', PicPoster),
('/uploader', Uploader)
],
debug=True)
def encode58(number):
# define the alphabet
# includes 1 to 9
# includes 'a' to 'z' except for 'l'
# includes 'A' to 'Z' except for 'O' and 'I'
alphabet = range(1,10)
alphabet.extend(map(chr,range(97,108)))
alphabet.extend(map(chr,range(109,123)))
alphabet.extend(map(chr,range(65, 73)))
alphabet.extend(map(chr,range(74,79)))
alphabet.extend(map(chr,range(80,91)))
num = int(number)
encoded = ''
while num >= 58:
remainder = num % 58
encoded = str(alphabet[remainder]) + encoded
temp = int(num/58)
num = temp
encoded = str(alphabet[num]) + encoded
return encoded
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()