Skip to content
This repository was archived by the owner on Jan 25, 2019. It is now read-only.

it displays available mime types for the given document and added ext… #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,37 @@
- `--text`, `-T` -- fetch plain text content (Look out for BOM)
- `--html`, `-H` -- fetch HTML content
- `--mime-type` -- specify arbitrary mime type

- `--ext EXT` -- find the default mime-type according to this ext
- `--repository-name REPOSITORY_NAME`
- `--file-name FILE_NAME`

## Example usage:

$ python gitdriver.py 1j6Ygv0_this_is_a_fake_document_id_a8Q66mvt4
$ python gitdriver.py --text 1j6Ygv0_this_is_a_fake_document_id_a8Q66mvt4
mime types available: ['text/html', 'text/plain', 'application/pdf']
exts available: ['html', 'txt', 'pdf']
Create repository "Untitled"
Initialized empty Git repository in /home/lars/projects/gitdriver/Untitled/.git/
[master (root-commit) 27baec9] revision from 2013-01-08T21:57:38.837Z
1 file changed, 1 insertion(+)
create mode 100644 content
create mode 100644 Untitled
[master 132175a] revision from 2013-01-08T21:57:45.800Z
1 file changed, 1 insertion(+), 1 deletion(-)
[master eb2302c] revision from 2013-01-09T01:47:29.593Z
1 file changed, 5 insertions(+), 1 deletion(-)
$ ls Untiled
content
Untitled.txt
$ cd Untitled
$ git log --oneline
d41ad6e revision from 2013-01-09T01:47:29.593Z
8d3e3ec revision from 2013-01-08T21:57:45.800Z
ccc0bdd revision from 2013-01-08T21:57:38.837Z

$ python gitdriver.py --ext pdf 1j6Ygv0_this_is_a_fake_document_id_a8Q66mvt4
$ ls Untiled
Untitled.pdf


## Google setup

You will need to create an OAuth client id and secret for use with
Expand Down
42 changes: 36 additions & 6 deletions gitdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import argparse
import subprocess
import yaml
import mimetypes

from drive import GoogleDrive, DRIVE_RW_SCOPE

Expand All @@ -16,14 +17,36 @@ def parse_args():
p.add_argument('--html', '-H', action='store_const', const='text/html',
dest='mime_type')
p.add_argument('--mime-type', dest='mime_type')
p.add_argument('--ext', dest='ext',
help='find the default mime-type according to this ext')
p.add_argument('--raw', '-R', action='store_true',
help='Download original document if possible.')
p.add_argument('--repository-name', dest='repository_name')
p.add_argument('--file-name', dest='file_name')

p.add_argument('docid')

return p.parse_args()

def mapIfDefined(fn, value):
return fn(value) if value is not None else None

def mime_type_ext(mime_type):
if (mime_type == 'text/plain'):
return 'txt'
else:
return mapIfDefined(lambda str:str[1:], mimetypes.guess_extension(mime_type))

def main():
opts = parse_args()
mimetypes.init()

if opts.ext:
if opts.mime_type:
print "Specify either ext or mime-type, but not both"
exit(1)
opts.mime_type = mimetypes.types_map['.' + opts.ext]

if not opts.mime_type:
print "Exactly one mime-type must be given!"
exit(1)
Expand All @@ -41,14 +64,22 @@ def main():
# an exception if the file does not exist.
md = gd.get_file_metadata(opts.docid)

mime_types = md['exportLinks'].keys()
print 'mime types available: %s' % map(str, mime_types)
print 'exts available: %s' % map(mime_type_ext, mime_types)

repository_name = opts.repository_name or md['title']
ext = opts.ext or mime_type_ext(opts.mime_type)
file_name = opts.file_name or (md['title'] + ('.' + ext if ext is not None else ""))

# Initialize the git repository.
print 'Create repository "%(title)s"' % md
subprocess.call(['git','init',md['title']])
os.chdir(md['title'])
print 'Create repository "%s"' % repository_name
subprocess.call(['git','init',repository_name])
os.chdir(repository_name)

# Iterate over the revisions (from oldest to newest).
for rev in gd.revisions(opts.docid):
with open('content', 'w') as fd:
with open(file_name, 'w') as fd:
if 'exportLinks' in rev and not opts.raw:
# If the file provides an 'exportLinks' dictionary,
# download the requested MIME type.
Expand All @@ -64,10 +95,9 @@ def main():
fd.write(chunk)

# Commit changes to repository.
subprocess.call(['git', 'add', 'content'])
subprocess.call(['git', 'add', file_name])
subprocess.call(['git', 'commit', '-m',
'revision from %s' % rev['modifiedDate']])

if __name__ == '__main__':
main()