Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 160 lines (141 sloc) 5.916 kB
6fb4989 @asldevi javascript to add petition URL
asldevi authored
1 import time
2 import md5
3 import urllib, urllib2
ce522cd @asldevi petitions- more form validations; names for contacts imported from gm…
asldevi authored
4 from xml.dom import minidom
6fb4989 @asldevi javascript to add petition URL
asldevi authored
5 from BeautifulSoup import BeautifulSoup
6 import string
7 import demjson
8
9 import web
10 from settings import db, render, session
898cbd2 @asldevi share petitions functionality using the imported contacts
asldevi authored
11 from utils import helpers, forms
6fb4989 @asldevi javascript to add petition URL
asldevi authored
12
13 def yahooLoginURL(email, url, token=None):
14 email = urllib.quote(email)
15 lines = open('/home/watchdog/certs/yauth', 'r').readlines()
16 appid = lines[0].rstrip()
17 secret = lines[1].rstrip()
18 ts = time.time()
19 appdata = email
20 yurl = 'https://api.login.yahoo.com'
21 purl = '%s?appid=%s&appdata=%s&ts=%s' % (url,appid, appdata, ts)
22 surl ='%s%s' % (purl, secret)
23 sig = md5.new(surl).hexdigest()
24 furl = '%s%s&sig=%s' % (yurl, purl, sig)
25 if token: furl = '%s&token=%s' % ( furl, token)
26 return furl
27
28 def gmailLoginURL(email):
29 url = 'https://www.google.com/accounts/AuthSubRequest?'
30 scope = urllib2.quote('http://www.google.com/m8/feeds/')
31 next = urllib2.quote('http://watchdog.net/authsub')
32 url += 'scope='+scope+'&session=1&secure=0&next='+ next
33 return url
34
35 class importcontacts:
36
37 def GET(self):
4bd272f @asldevi petitions - delete and unsign; msg_type to differentiate error msgs; …
asldevi authored
38 msg, msg_type = helpers.get_delete_msg()
6fb4989 @asldevi javascript to add petition URL
asldevi authored
39 return render.import_contacts(msg)
40
41 def POST(self):
42 i = web.input()
43 email = i.get('email')
ce522cd @asldevi petitions- more form validations; names for contacts imported from gm…
asldevi authored
44 form = forms.loadcontactsform()
45 if form.validates(i):
46 session.email = email
47 session.pid = i.pid
48 if i.provider == 'Yahoo':
49 ylogin_url = yahooLoginURL(email, '/WSLogin/V1/wslogin')
50 raise web.seeother(ylogin_url)
51 elif i.provider == 'Google':
52 glogin_url = gmailLoginURL(email)
53 raise web.seeother(glogin_url)
6fb4989 @asldevi javascript to add petition URL
asldevi authored
54 else:
ce522cd @asldevi petitions- more form validations; names for contacts imported from gm…
asldevi authored
55 import petition
56 share_obj = petition.share()
57 return share_obj.GET(form)
58
59 def save_contacts(email, contacts, provider):
60 user_id = helpers.get_loggedin_userid()
61 for c in contacts:
62 cname, cemail = c['name'], c['email']
63 vars = dict(user_id=user_id, uemail=email, cemail=cemail,
64 cname=cname, provider=provider)
65 e = db.select('contacts',
66 where='user_id=$user_id and uemail=$uemail and cemail=$cemail',
67 vars=vars)
68 if not e: n = db.insert('contacts', seqname=False, **vars)
69 else: db.update('contacts', cname=cname,
70 where='user_id=$user_id and uemail=$uemail and cemail=$cemail',
71 vars=vars)
6fb4989 @asldevi javascript to add petition URL
asldevi authored
72
73 class bbauth:
ce522cd @asldevi petitions- more form validations; names for contacts imported from gm…
asldevi authored
74 def get_contacts(self, contacts_json):
75 content = demjson.decode(contacts_json)
76
77 contacts = []
78 for c in content.get('contacts'):
6fb4989 @asldevi javascript to add petition URL
asldevi authored
79 fields = c['fields']
80 cemail = fields[0]['data']
81 cfname = ' '; clname = ' '
82
83 if len(fields) > 1:
84 cfname = fields[1].get('first', ' ')
85 clname = fields[1].get('last', ' ')
ce522cd @asldevi petitions- more form validations; names for contacts imported from gm…
asldevi authored
86
6fb4989 @asldevi javascript to add petition URL
asldevi authored
87 cname = u'%s %s' % (cfname, clname)
88 cname = cname.replace(''', ' ').strip()
ce522cd @asldevi petitions- more form validations; names for contacts imported from gm…
asldevi authored
89 contacts.append(dict(email=cemail, name=cname))
90 return contacts
6fb4989 @asldevi javascript to add petition URL
asldevi authored
91
92 def GET(self):
93 i = web.input()
94 appid = i.get('appid').rstrip()
95 appdata = i.get('appdata')
96 userhash = i.get('userhash')
97 ts = i.get('ts')
98 token = i.get('token')
99 email = session.email
100 #XXX: security verification etc..
101 url = yahooLoginURL(email, '/WSLogin/V1/wspwtoken_login', token)
102 resp = urllib2.urlopen(url)
103 content = resp.read()
104 soup = BeautifulSoup(content)
105 aurl = 'http://address.yahooapis.com/v1/searchContacts?format=json'
106 wssid = soup.findAll('wssid')[0].contents[0]
107 cookie =soup.findAll('cookie')[0].contents[0]
108 cookie = cookie.strip()
109
898cbd2 @asldevi share petitions functionality using the imported contacts
asldevi authored
110 furl = aurl + '&fields=email,name&email.present=1&appid=%s&WSSID=%s' % (appid, wssid)
111 req = urllib2.Request(furl)
112 req.add_header('Cookie', cookie)
113 req.add_header('Content-Type', 'application/json')
ce522cd @asldevi petitions- more form validations; names for contacts imported from gm…
asldevi authored
114 response = urllib2.urlopen(req).read()
115 contacts = self.get_contacts(response)
116 save_contacts(email, contacts, provider='YAHOO')
898cbd2 @asldevi share petitions functionality using the imported contacts
asldevi authored
117 raise web.seeother('/c/share?pid=%s' % (session.pid))
6fb4989 @asldevi javascript to add petition URL
asldevi authored
118
119 class authsub:
ce522cd @asldevi petitions- more form validations; names for contacts imported from gm…
asldevi authored
120 def get_contacts(self, contacts_feed):
121 ATOM_NS = 'http://www.w3.org/2005/Atom'
122 doc = minidom.parse(contacts_feed)
123 entries = doc.getElementsByTagNameNS(ATOM_NS, u'entry')
124
125 def get_text(elem):
126 text = ''
127 for node in elem.childNodes:
128 if node.nodeType == node.TEXT_NODE:
129 text += node.data
130 return text
131
132 contacts = []
133 for e in entries:
134 e_title = e.getElementsByTagNameNS(ATOM_NS, u'title')[0]
135 name = get_text(e_title)
136 email = e.getElementsByTagName('gd:email')[0].getAttribute('address')
137 contacts.append(dict(name=name, email=email))
138 return contacts
6fb4989 @asldevi javascript to add petition URL
asldevi authored
139
140 def GET(self):
141 i = web.input()
142 authToken = i.get('token')
143 email = session.email
144 emailq = urllib2.quote(email)
145 url = ("http://www.google.com/m8/feeds/contacts/%s/full?max-results=999" % emailq)
146 headers = { 'Authorization' : 'AuthSub token="%s"' % authToken.strip() }
147 request = urllib2.Request(url, None, headers)
148 response = urllib2.urlopen(request)
ce522cd @asldevi petitions- more form validations; names for contacts imported from gm…
asldevi authored
149 contacts = self.get_contacts(response)
150 save_contacts(email, contacts, provider='GOOGLE')
898cbd2 @asldevi share petitions functionality using the imported contacts
asldevi authored
151 raise web.seeother('/c/share?pid=%s' % (session.pid))
6fb4989 @asldevi javascript to add petition URL
asldevi authored
152
153 class yauth:
154 def GET(self):
155 return """
156 Phrase: "# and nation nation moved yet so ship or onwhether so now conceived any the that"
157 File: "ydnlIEWXo.html"
158 Url to Check: "http://watchdog.net/ydnlIEWXo.html"
159 """
Something went wrong with that request. Please try again.