-
Notifications
You must be signed in to change notification settings - Fork 11
/
migration_util.py
74 lines (53 loc) · 2.08 KB
/
migration_util.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
from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import requests
import urllib
from io import BytesIO
from pathlib import Path
import redis
r = redis.Redis(host='0.0.0.0', port=6379, db=0, decode_responses=True)
def authorize_with_google():
store = file.Storage((Path().parent / "auth/google_token.json").resolve().as_posix())
return store.get()
def get_google_photos_service(google_creds):
return build('photoslibrary', 'v1',
http=google_creds.authorize(Http()),
cache_discovery=False)
def find_album_on_google(album_title):
return r.get(album_title)
def create_album_on_google(service, album_title):
albums = service.albums()
new_album = albums.create(body={"album": {"title": album_title}}).execute()
r.set(album_title, new_album.get('id', None))
return new_album.get("id", None)
def upload_photo_to_google(google_auth, service, album_id, photo_data,
photo_title, photo_tags):
media_items = service.mediaItems()
url = 'https://photoslibrary.googleapis.com/v1/uploads'
authorization = 'Bearer ' + google_auth.access_token
headers = {
"Authorization": authorization,
'Content-type': 'application/octet-stream',
'X-Goog-Upload-File-Name': photo_title,
'X-Goog-Upload-Protocol': 'raw',
}
upload_response = requests.post(url, headers=headers, data=photo_data)
upload_token = upload_response.text
if upload_token is not None:
payload = {
"albumId": album_id,
"newMediaItems": [{
"description": photo_tags,
"simpleMediaItem": {
"uploadToken": upload_token
}
}]
}
add_photo_req = media_items.batchCreate(body=payload)
add_photo_resp = add_photo_req.execute()
return add_photo_resp
def get_photo_from_flickr(photo_url):
photo_url_obj = urllib.request.urlopen(photo_url)
return BytesIO(photo_url_obj.read())