-
Notifications
You must be signed in to change notification settings - Fork 9
/
get_gphoto.py
executable file
·86 lines (71 loc) · 2.56 KB
/
get_gphoto.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
#!/usr/bin/env python3
# coding: utf8
from pathlib import Path
import requests
import json
import logging
from random import randrange
from gphotos.authorize import Authorize
from gphotos.restclient import RestClient
log = logging.getLogger()
logging.basicConfig(level=logging.WARNING)
#log.setLevel(logging.NOTSET)
### Main app class
class KindleGphotos:
def __init__(self):
self.auth: Authorize = None
def setup(self):
credentials_file = Path(".gphotos.token")
secret_file = Path("client_secret.json")
scope = [
"https://www.googleapis.com/auth/photoslibrary.readonly",
"https://www.googleapis.com/auth/photoslibrary.sharing",
]
photos_api_url = (
"https://photoslibrary.googleapis.com/$discovery" "/rest?version=v1"
)
self.auth = Authorize(
scope, credentials_file, secret_file, 3)
self.auth.authorize()
self.google_photos_client = RestClient(photos_api_url, self.auth.session)
def start(self):
log.debug("Starting up...")
### Get album list
mylist = self.google_photos_client.sharedAlbums.list.execute(pageSize=50).json()
### Get album ID
items_count = 0
for album in mylist['sharedAlbums']:
if 'title' in album.keys():
log.debug('"'+album['title']+'"')
if album['title'] == 'kindle':
print(album['title'], album['mediaItemsCount'])
items_count = int(album['mediaItemsCount'])
album_id = album['id']
if not items_count:
quit()
### get list of images
body = {
"pageSize": 50,
"albumId": album_id,
# "filters": {
# "mediaTypeFilter": {"mediaTypes": ["PHOTO"]},
# },
}
photo_list = self.google_photos_client.mediaItems.search.execute(body).json()
notfound = 1
while(notfound):
idx = randrange(items_count)
log.debug(idx)
if "image/jpeg" in photo_list['mediaItems'][idx]['mimeType']:
notfound = 0
media_item = photo_list['mediaItems'][idx]
print(media_item['filename'], media_item['mimeType'])
### download photo
url = str(media_item['baseUrl'])+'=w2048-h1024'
photo = requests.get(url)
open('photo.jpg', 'wb').write(photo.content)
def main(self):
self.setup()
self.start()
if __name__ == '__main__':
KindleGphotos().main()