# Copyright (C) 2012 FinalsClub Foundation
import datetime
+import magic
import mimetypes
import os
+import re
import time
import httplib2
'secret/client_secrets.json')
#from credentials import GOOGLE_USER # FIXME
try:
- from secret.drive import GOOGLE_USER
+ from secrets.drive import GOOGLE_USER
except:
GOOGLE_USER = 'admin@karmanotes.org' # FIXME
PPT_MIMETYPES = ['application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation']
+def extract_file_details(fileobj):
+ details = None
+ year = None
+
+ fileobj.open()
+ filebuf = fileobj.read()
+ with magic.Magic() as m:
+ details = m.id_buffer(filebuf)
+ fileobj.close()
+
+ result = re.search(r'Create Time/Date:[^,]+(?P<year>\d{4})', details)
+ if result:
+ if 'year' in result.groupdict():
+ year = result.groupdict()['year']
+
+ return {'year': year}
+
def build_flow():
""" Create an oauth2 autentication object with our preferred details """
scopes = [
note.file_type = 'ppt'
note.pdf_file.save(filename + '.pdf', ContentFile(content_dict['pdf']))
- else:
- # PPT files do not have this export ability
- note.gdrive_url = file_dict[u'exportLinks']['application/vnd.oasis.opendocument.text']
+ elif 'html' in content_dict and content_dict['html']:
note.html = content_dict['html']
+ # before we save new html, sanitize a tags in note.html
+ #note.sanitize_html(save=False)
+ #FIXME: ^^^ disabled
note.text = content_dict['text']
- # before we save new html, sanitize a tags in note.html
- #note.sanitize_html(save=False)
- #FIXME: ^^^ disabled until we can get html out of an Etree html element
+ note_details = extract_file_details(fp_file)
+ if 'year' in note_details and note_details['year']:
+ note.year = note_details['year']
+
# Finally, save whatever data we got back from google
note.save()