-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshorturl.py
48 lines (40 loc) · 1.53 KB
/
shorturl.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
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.api import mail
import os, logging
import json, urllib, urllib2
import traceback
# Web app
API_KEY = 'AIzaSyDkrW_qf_sLGzhFrHPjzDKZ-kXsS9SC22A'
class ShortUrlPage(webapp.RequestHandler):
def get(self):
longurl = self.request.get('longurl')
service = self.request.get('service')
results = {'short' : None}
# bit.ly, in the future ?
if service == 'goo.gl':
post_url = 'https://www.googleapis.com/urlshortener/v1/url?key=%s' % API_KEY
postdata = {'longUrl':longurl}
headers = {'Content-Type':'application/json'}
req = urllib2.Request(
post_url,
json.dumps(postdata),
headers
)
ret = urllib2.urlopen(req).read()
results['short'] = json.loads(ret)['id'].replace("http://","https://")
json_res = json.dumps(results)
self.response.headers['Access-Control-Allow-Origin'] = '*'
self.response.out.write(json_res)
application = webapp.WSGIApplication(
[
('/shorturl', ShortUrlPage),
],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()