-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygdd.py
83 lines (62 loc) · 2.48 KB
/
pygdd.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
import re, urllib, urllib2, urlparse
import csv
#**********************************************************************
# PyGDD - Python Google Drive Downloader
# Class that connects to Google Drive and downloads spreadsheets
#
# 2014, Miguel Carranza miguel@miguelcarranza.es
# www.github.com/MiguelCarranza
#**********************************************************************
class PyGDD(object):
def __init__(self, gmail_login, gmail_password):
self.gmail_login = gmail_login
self.gmail_password = gmail_password
self.auth_token = self.__get_auth_token()
#**********************************************************************
# Private methods
#**********************************************************************
def __get_auth_token(self):
url = 'https://www.google.com/accounts/ClientLogin'
params = {
"Email": self.gmail_login,
"Passwd": self.gmail_password,
"service": 'wise',
"accountType": "HOSTED_OR_GOOGLE",
"source": 'ms-prometheus'
}
req = urllib2.Request(url, urllib.urlencode(params))
return re.findall(r"Auth=(.*)", urllib2.urlopen(req).read())[0]
def __get_headers(self):
return {
"Authorization": "GoogleLogin auth=" + self.auth_token,
"GData-Version": "3.0"
}
def __get_key_and_gid(self, url):
try:
parsed_url = urlparse.urlparse(url)
key = urlparse.parse_qs(parsed_url.query)['key'][0]
gid = int(urlparse.parse_qs(parsed_url.fragment)['gid'][0])
except:
raise PyGDDException('Wrong Google Drive URL.')
return (key, gid)
#**********************************************************************
# Public methods
#**********************************************************************
def get_csv_file(self, url):
key, gid = self.__get_key_and_gid(url)
try:
url_format = "https://docs.google.com/feeds/download/spreadsheets/Export?key=%s&exportFormat=csv&gid=%i"
headers = self.__get_headers()
req = urllib2.Request(url_format % (key, gid), headers=headers)
return urllib2.urlopen(req)
except:
raise PyGDDException('Impossible to download %s.' % url)
def get_csv_dict_reader(self, url):
return csv.DictReader(self.get_csv_file(url))
#**********************************************************************
# PyGDDException
#
# Exception. Used if something went wrong.
#**********************************************************************
class PyGDDException(Exception):
pass