Menu

[r138]: / trunk / apps / drive.py  Maximize  Restore  History

Download this file

163 lines (143 with data), 5.9 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
#!/usr/bin/env python
########################################################################
#
# Project: Cloud Export
# URL: http://www.nabber.org/projects/
# E-mail: neil@nabber.org
#
# Copyright: (C) 2012-2014, Neil McNab
# License: GNU General Public License Version 2
# (http://www.gnu.org/copyleft/gpl.html)
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Filename: $URL: https://cloudexport.svn.sourceforge.net/svnroot/cloudexport/trunk/apps/cal.py $
# Last Updated: $Date: 2013-03-16 21:07:16 -0400 (Sat, 16 Mar 2013) $
# Version: $Rev: 127 $
# Author(s): Neil McNab
#
########################################################################
import urllib2
import os
import base64
import sys
import codecs
import time
import calendar
import mimetypes
import re
import apiclient.discovery
import google
import core
# mimetypes doesn't have everything we need, so we have another lookup here
MIME_MAP = {
'text/html': ".html",
'application/pdf': ".pdf",
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': ".docx",
'application/vnd.oasis.opendocument.text': ".odt",
'application/x-vnd.oasis.opendocument.spreadsheet': ".ods",
'application/vnd.oasis.opendocument.spreadsheet': ".ods",
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': '.xlsx',
'application/vnd.openxmlformats-officedocument.presentationml.presentation': '.pptx',
'application/rtf': ".rtf",
'text/plain': ".txt",
'image/png': '.png',
'image/jpeg': '.jpg',
'image/svg+xml': '.svg',
'image/tiff': '.tif',
'application/zip': '.zip',
'audio/mpeg': '.mp3',
'image/x-ms-bmp': '.bmp',
'text/csv': '.csv',
}
RETRIES=3
TIMEOUT=5
class backup(core.ServiceCore):
def __init__(self, *args):
core.ServiceCore.__init__(self, 'drive', *args)
options = self.options
print "Downloading Google Drive Files..."
http = google.login(options["path"], options['browser'])
# Build a service object for interacting with the API. Visit
# the Google APIs Console
# to get a developerKey for your own application.
service = apiclient.discovery.build(serviceName='drive', version='v2', http=http, developerKey=apiclient.keys.developer_key)
file_list = service.files().list().execute()
#print len(file_list["items"])
#sys.exit()
count = 0
for fileobj in file_list["items"]:
if self.cancel():
self.messages('Backup Canceled.')
return
self.progress(float(count)/len(file_list["items"]))
count +=1
if "downloadUrl" not in fileobj and "exportLinks" not in fileobj:
# skip everything we can't download, should just be folders
continue
title = fileobj["title"]
#print title,
urls = {}
ext = None
if "downloadUrl" in fileobj:
urls[fileobj["mimeType"]] = fileobj["downloadUrl"]
if "exportLinks" in fileobj:
urls = fileobj["exportLinks"]
if not bool(options['dryrun']):
for mimetype in urls.keys():
url = urls[mimetype]
filename = self.add_ext(self._path_cleanup(title), mimetype)
self.messages('Downloading ' + filename)
dl_count = 0
dl_success = False
while (dl_count < RETRIES and not dl_success):
try:
dl_success = self.try_once(service, url, filename)
except AssertionError:
self.messages("Error with download, trying again")
dl_count += 1
time.sleep(TIMEOUT)
if dl_count == RETRIES:
raise AssertionError, "Download failed for %s" % filename
print ""
self.end()
def try_once(self, service, url, filename):
resp, content = service._http.request(url)
if resp.status == 200:
#print "OK",
handle = open(os.path.join(self.path, filename), "wb")
handle.write(content)
handle.close()
return True
raise AssertionError, "Download failed for %s, (%s: %s)" % (filename, resp.status, content)
return False
def add_ext(self, filename, mimetype):
ext = self.guess_extension(mimetype)
if filename.lower().endswith(ext.lower()):
return filename
return filename + ext
def guess_extension(self, mimetype):
ext = None
try:
ext = MIME_MAP[mimetype]
except KeyError:
print "Missing mimetype %s, falling back to system defaults" % mimetype
if ext is None:
ext = mimetypes.guess_extension(mimetype)
if ext is None:
print "Failed to find extension for mimetype", mimetype
return ext
def _path_cleanup(self, path):
return re.sub("[\\/]+", "-", path)