Created
December 31, 2011 00:48
-
-
Save dequegorg/1542262 to your computer and use it in GitHub Desktop.
Retrieve StackExchange Inbox with Google Accounts and Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Copyright 2011 benjamin <jesuisbenjamin@gmail.com> | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
# MA 02110-1301, USA. | |
import urllib | |
import urllib2 | |
import cookielib | |
from BeautifulSoup import BeautifulSoup | |
from getpass import getpass | |
# Define URLs | |
google_accounts_url = 'http://accounts.google.com' | |
authentication_url = 'https://accounts.google.com/ServiceLoginAuth' | |
stack_overflow_url = 'http://stackoverflow.com/users/authenticate' | |
genuwine_url = 'http://stackoverflow.com/inbox/genuwine' | |
# Set counter for requests: | |
counter = 0 | |
# Build opener | |
jar = cookielib.CookieJar() | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar)) | |
class Parser(HTMLParser): | |
''' | |
Parser to handle response and retrieve dhs value. | |
''' | |
def handle_starttag(self, tag, attrs): | |
if tag == 'input': | |
for item in attrs: | |
if item[0] == 'name' and item[1] == 'dsh': | |
for item in attrs: | |
if item[0] == 'value': | |
self.results = {} | |
self.results['dsh'] = item[1] | |
def expound_request(request): | |
''' | |
Prints out a verbose of request before sending it. | |
''' | |
global counter | |
counter += 1 | |
print '_____________________________________________________________________' | |
print '\n REQUEST N#', counter | |
print '\n URL: \n', request._Request__original | |
print '\n HEADERS: \n', request.headers | |
print '\n DATA: \n', request.data | |
print '\n' | |
def expound_response(response): | |
''' | |
Prints out a verbose of response. | |
''' | |
print '_____________________________________________________________________' | |
print '\n RESPONSE N#', counter | |
print '\n URL:\n', response.url | |
print '\n CODE:\n', response.code | |
print '\n HEADERS:\n', response.headers | |
print '\n COOKIE JAR:\n', jar, '\n' | |
def request_url(request): | |
''' | |
Requests given URL. | |
''' | |
expound_request(request) | |
try: | |
response = opener.open(request) | |
expound_response(response) | |
except: | |
raise | |
return response | |
def authenticate(username='', password=''): | |
''' | |
Authenticates to Google Accounts using user-provided username and password | |
''' | |
# Build up headers | |
user_agent = 'Mozilla/5.0 (Ubuntu; X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0' | |
headers = {'User-Agent' : user_agent} | |
# Set Data to None | |
data = None | |
# Build up URL request with headers and data | |
request = urllib2.Request(google_accounts_url, data, headers) | |
response = request_url(request) | |
# Build up POST data for authentication | |
html = response.read() | |
dsh = BeautifulSoup(html).findAll(attrs={'name' : 'dsh'})[0].get('value').encode() | |
auto = response.headers.getheader('X-Auto-Login') | |
follow_up = urllib.unquote(urllib.unquote(auto)).split('continue=')[-1] | |
galx = jar._cookies['accounts.google.com']['/']['GALX'].value | |
values = {'continue' : follow_up, | |
'followup' : follow_up, | |
'dsh' : dsh, | |
'GALX' : galx, | |
'pstMsg' : 1, | |
'dnConn' : 'https://accounts.youtube.com', | |
'checkConnection' : '', | |
'checkedDomains' : '', | |
'timeStmp' : '', | |
'secTok' : '', | |
'Email' : username, | |
'Passwd' : password, | |
'signIn' : 'Sign in', | |
'PersistentCookie' : 'yes', | |
'rmShown' : 1} | |
data = urllib.urlencode(values) | |
# Build up URL for authentication | |
request = urllib2.Request(authentication_url, data, headers) | |
response = request_url(request) | |
# Check if logged in | |
if response.url != request._Request__original: | |
print '\n Logged in :)\n' | |
else: | |
print '\n Log in failed :(\n' | |
# Build OpenID Data | |
values = {'oauth_version' : '', | |
'oauth_server' : '', | |
'openid_username' : '', | |
'openid_identifier' : 'https://www.google.com/accounts/o8/id'} | |
data = urllib.urlencode(values) | |
# Build up URL for OpenID authetication | |
request = urllib2.Request(stack_overflow_url, data, headers) | |
response = request_url(request) | |
# Retrieve Genuwine | |
data = None | |
request = urllib2.Request(genuwine_url, data, headers) | |
response = request_url(request) | |
print response.read() | |
if __name__ == '__main__': | |
username = raw_input('Enter your Gmail address: ') | |
password = getpass('Enter your password: ') | |
authenticate(username, password) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment