adding some test changes to ppt handling for testing on beta
[oweals/karmaworld.git] / karmaworld / apps / notes / gdrive.py
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 # Copyright (C) 2012  FinalsClub Foundation
4
5 import datetime
6 import mimetypes
7 import os
8 import time
9
10 import httplib2
11 from apiclient.discovery import build
12 from apiclient.http import MediaFileUpload
13 from django.conf import settings
14 from django.core.files import File
15 from oauth2client.client import flow_from_clientsecrets
16
17 from karmaworld.apps.notes.models import DriveAuth, Note
18
19 CLIENT_SECRET = os.path.join(settings.DJANGO_ROOT, \
20                     'secret/client_secrets.json')
21 #from credentials import GOOGLE_USER # FIXME
22 try:
23     from secrets.drive import GOOGLE_USER
24 except:
25     GOOGLE_USER = 'admin@karmanotes.org' # FIXME
26
27 EXT_TO_MIME = {'.docx': 'application/msword'}
28
29 def build_flow():
30     """ Create an oauth2 autentication object with our preferred details """
31     scopes = [
32         'https://www.googleapis.com/auth/drive',
33         'https://www.googleapis.com/auth/drive.file',
34         'https://www.googleapis.com/auth/userinfo.email',
35         'https://www.googleapis.com/auth/userinfo.profile',
36     ]
37
38     flow = flow_from_clientsecrets(CLIENT_SECRET, ' '.join(scopes), \
39             redirect_uri='http://localhost:8000/oauth2callback')
40     flow.params['access_type'] = 'offline'
41     flow.params['approval_prompt'] = 'force'
42     flow.params['user_id'] = GOOGLE_USER
43     return flow
44
45
46 def authorize():
47     """ Use an oauth2client flow object to generate the web url to create a new
48         auth that can be then stored """
49     flow = build_flow()
50     print flow.step1_get_authorize_url()
51
52
53 def accept_auth(code):
54     """ Callback endpoint for accepting the post `authorize()` google drive
55         response, and generate a credentials object
56         :code:  An authentication token from a WEB oauth dialog
57         returns a oauth2client credentials object """
58     flow = build_flow()
59     creds = flow.step2_exchange(code)
60     return creds
61
62
63 def build_api_service(creds):
64     http = httplib2.Http()
65     http = creds.authorize(http)
66     return build('drive', 'v2', http=http), http
67
68
69 def check_and_refresh(creds, auth):
70     """ Check a Credentials object's expiration token
71         if it is out of date, refresh the token and save
72         :creds: a Credentials object
73         :auth:  a DriveAuth that backs the cred object
74         :returns: updated creds and auth objects
75     """
76     if creds.token_expiry < datetime.datetime.utcnow():
77         # if we are passed the token expiry,
78         # refresh the creds and store them
79         http = httplib2.Http()
80         http = creds.authorize(http)
81         creds.refresh(http)
82         auth.credentials = creds.to_json()
83         auth.save()
84     return creds, auth
85
86
87 def convert_with_google_drive(note):
88     """ Upload a local note and download HTML
89         using Google Drive
90         :note: a File model instance # FIXME
91     """
92     # TODO: set the permission of the file to permissive so we can use the
93     #       gdrive_url to serve files directly to users
94
95     # Get file_type and encoding of uploaded file
96     # i.e: file_type = 'text/plain', encoding = None
97     (file_type, encoding) = mimetypes.guess_type(note.note_file.path)
98
99     resource = {
100                 'title':    note.name,
101             }
102
103
104     if file_type != None:
105         media = MediaFileUpload(note.note_file.path, mimetype=file_type,
106                     chunksize=1024*1024, resumable=True)
107
108     else:
109         media = MediaFileUpload(note.note_file.path,
110                     chunksize=1024*1024, resumable=True)
111
112     auth = DriveAuth.objects.filter(email=GOOGLE_USER).all()[0]
113     creds = auth.transform_to_cred()
114
115
116     creds, auth = check_and_refresh(creds, auth)
117
118     service, http = build_api_service(creds)
119
120     # get the file extension
121     filename, extension = os.path.splitext(note.note_file.path)
122     # Upload the file
123     if extension.lower() in ['.pdf', '.jpeg', '.jpg', '.png']:
124         # include OCR on ocr-able files
125         file_dict = service.files().insert(body=resource, media_body=media, convert=True, ocr=True).execute()
126
127     elif extension.lower() in ['.ppt', 'pptx']:
128         # FIXME VVVV
129         file_dict = service.files().insert(body=resource, media_body=media, convert=True, ocr=True).execute()
130
131     else:
132         file_dict = service.files().insert(body=resource, media_body=media, convert=True).execute()
133
134     if u'exportLinks' not in file_dict:
135         # wait some seconds
136         # get the doc from gdrive
137         time.sleep(30)
138         file_dict = service.files().get(fileId=file_dict[u'id']).execute()
139
140     # get the converted filetype urls
141     download_urls = {}
142     download_urls['html'] = file_dict[u'exportLinks']['text/html']
143     download_urls['text'] = file_dict[u'exportLinks']['text/plain']
144     if extension.lower() in ['.ppt', 'pptx']:
145
146         download_urls['pdf'] = file_dict[u'exportLinks']['application/pdf']
147
148
149     content_dict = {}
150     for download_type, download_url in download_urls.items():
151         print "\n%s -- %s" % (download_type, download_urls)
152         resp, content = http.request(download_url, "GET")
153
154
155         if resp.status in [200]:
156             print "\t downloaded!"
157             # save to the File.property resulting field
158             content_dict[download_type] = content
159         else:
160             print "\t Download failed: %s" % resp.status
161
162     # Get a new copy of the file from the database with the new metadata from filemeta
163     new_note = Note.objects.get(id=note.id)
164     if extension.lower() == '.pdf':
165         new_note.file_type = 'pdf'
166         new_note.pdf_file = File(content_dict['pdf'])
167
168     # set the .odt as the download from google link
169     new_note.gdrive_url = file_dict[u'exportLinks']['application/vnd.oasis.opendocument.text']
170     new_note.html = content_dict['html']
171     new_note.text = content_dict['text']
172
173     # before we save new html, sanitize a tags in note.html
174     new_note.sanitize_html(save=False)
175
176     # Finally, save whatever data we got back from google
177     new_note.save()