#!/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)