forked from blacktwin/JBOPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grab_gdrive_media.py
109 lines (88 loc) · 3.74 KB
/
grab_gdrive_media.py
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
# -*- encoding: UTF-8 -*-
'''
https://gist.github.com/blacktwin/f435aa0ccd498b0840d2407d599bf31d
'''
import os
import httplib2
# pip install --upgrade google-api-python-client
from oauth2client.file import Storage
from googleapiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
import time, shutil, sys
# Copy your credentials from the console
# https://console.developers.google.com
CLIENT_ID = ''
CLIENT_SECRET = ''
OUT_PATH = '' # Output Path
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
CREDS_FILE = os.path.join(os.path.dirname(__file__), 'credentials.json')
if not os.path.exists(OUT_PATH):
os.makedirs(OUT_PATH)
storage = Storage(CREDS_FILE)
credentials = storage.get()
if credentials is None:
# Run through the OAuth flow and retrieve credentials
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print('Go to the following link in your browser: ' + authorize_url)
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)
storage.put(credentials)
# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)
def list_files(service):
page_token = None
while True:
param = {}
if page_token:
param['pageToken'] = page_token
files = service.files().list(**param).execute()
for item in files['items']:
yield item
page_token = files.get('nextPageToken')
if not page_token:
break
for item in list_files(drive_service):
if (item.get('mimeType') == 'image/jpeg' or item.get('mimeType') == 'video/mp4') \
and (item.get('originalFilename').endswith(('.jpg', '.mp4'))):
try:
video_path = OUT_PATH + "\\" + "Video"
if not os.path.isdir(video_path):
os.mkdir(video_path)
picture_path = OUT_PATH + "\\" + "Pictures"
if not os.path.isdir(picture_path):
os.mkdir(picture_path)
if item.get('mimeType') == 'image/jpeg' and item.get('originalFilename').endswith('.jpg'):
year_date = picture_path + "\\" + item['createdDate'][:4]
elif item.get('mimeType') == 'video/mp4' and item.get('originalFilename').endswith('.mp4'):
year_date = video_path + "\\" + item['createdDate'][:4]
md_date = year_date + "\\" + item['createdDate'][5:10]
if not os.path.isdir(year_date):
os.mkdir(year_date)
if not os.path.isdir(md_date):
os.mkdir(md_date)
outfile = os.path.join(md_date, '%s' % item['title'])
download_url = None
if 'mimeType' in item and 'image/jpeg' in item['mimeType'] or 'video/mp4' in item['mimeType']:
download_url = item['downloadUrl']
else:
print 'ERROR getting %s' % item.get('title')
print item
print dir(item)
if download_url:
print "downloading %s" % item.get('title')
resp, content = drive_service._http.request(download_url)
if resp.status == 200:
if os.path.isfile(outfile):
print("ERROR, %s already exist" % outfile)
else:
with open(outfile, 'wb') as f:
f.write(content)
print("OK.")
else:
print('ERROR downloading %s' % item.get('title'))
except Exception as e:
print(e)