Exemplo n.º 1
0
def _insert_new_photo(token):
    creds = get_credentials()
    headers = {"Authorization": f"Bearer {creds.token}"}
    insert_new_media_item = {
        "newMediaItems": [{
            "simpleMediaItem": {
                "uploadToken": token
            }
        }]
    }
    url = r"https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate"
    r = requests.post(url=url,
                      headers=headers,
                      data=json.dumps(insert_new_media_item))
    response = r.json()
    status = response["newMediaItemResults"][0]["status"]["message"]
    if status != "OK":
        logger.info(f"NewMediaItem insertion failed. {pformat(response)}")
        success = False
    else:
        logger.info(f"Insertion successful. {pformat(response)}")
        success = True
    elapsed = r.elapsed.microseconds / 1_000_000
    print(
        f"Media insertion elapsed time: {r.elapsed.microseconds/1_000_000} seconds"
    )
    return success, elapsed
Exemplo n.º 2
0
async def fetch(session, url):
    """
    Use session object to perform 'get' request on url
    """
    creds = get_credentials()

    headers = {"Authorization": f"Bearer {creds.token}"}
    async with session.get(url, headers=headers) as result:
        return await result.json()
Exemplo n.º 3
0
async def fetch(session, url):
    """
    Use session object to perform 'get' request on url
    """
    creds = get_credentials()

    headers = {
        "Authorization": f"Bearer {creds.token}"
    }
    async with session.get(url, headers=headers) as result:
        return await result.json()
Exemplo n.º 4
0
def _upload_binary_media(filepath, filename):
    creds = get_credentials()
    with open(filepath, "rb") as photo_fp:
        binary_file = photo_fp.read()
    url = r"https://photoslibrary.googleapis.com/v1/uploads"
    headers = {
        "Content-type": "application/octet-stream",
        "Authorization": f"Bearer {creds.token}",
        "X-Goog-Upload-File-Name": f"{filename}",
        "X-Goog-Upload-Protocol": "raw",
    }
    r = requests.post(url, headers=headers, data=binary_file)
    if r.ok:
        log_status = "Upload successful"
    else:
        log_status = "Upload failed"
    logger.info(f"{log_status}: Upload elapsed time: {r.elapsed}")
    return r
Exemplo n.º 5
0
def _upload_binary_media(filepath, filename):
    creds = get_credentials()
    with open(filepath, "rb") as photo_fp:
        binary_file = photo_fp.read()
    url = r"https://photoslibrary.googleapis.com/v1/uploads"
    headers = {
        "Content-type": "application/octet-stream",
        "Authorization": f"Bearer {creds.token}",
        "X-Goog-Upload-File-Name": f"{filename}",
        "X-Goog-Upload-Protocol": "raw",
    }
    r = requests.post(url, headers=headers, data=binary_file)
    if r.ok:
        log_status = "Upload successful"
    else:
        log_status = "Upload failed"
    logger.info(f"{log_status}: Upload elapsed time: {r.elapsed}")
    return r
Exemplo n.º 6
0
def _insert_new_photo(token):
    creds = get_credentials()
    headers = {"Authorization": f"Bearer {creds.token}"}
    insert_new_media_item = {
        "newMediaItems": [{"simpleMediaItem": {"uploadToken": token}}]
    }
    url = r"https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate"
    r = requests.post(url=url, headers=headers, data=json.dumps(insert_new_media_item))
    response = r.json()
    status = response["newMediaItemResults"][0]["status"]["message"]
    if status != "OK":
        logger.info(f"NewMediaItem insertion failed. {pformat(response)}")
        success = False
    else:
        logger.info(f"Insertion successful. {pformat(response)}")
        success = True
    elapsed = r.elapsed.microseconds/1_000_000
    print(f"Media insertion elapsed time: {r.elapsed.microseconds/1_000_000} seconds")
    return success, elapsed
Exemplo n.º 7
0
from sanic import Sanic
from sanic import response
import aiohttp
import sys

from oauth2creds import get_credentials

credentials = get_credentials()

app = Sanic(__name__)


async def fetch(session, url):
    """
    Use session object to perform 'get' request on url
    """
    creds = get_credentials()

    headers = {"Authorization": f"Bearer {creds.token}"}
    async with session.get(url, headers=headers) as result:
        return await result.json()


@app.route('/')
async def handle_request(request):
    url = r"https://www.googleapis.com/drive/v3/files/root"
    async with aiohttp.ClientSession() as session:
        result = await fetch(session, url)
        return response.json(result)

Exemplo n.º 8
0
from sanic import Sanic
from sanic import response
import aiohttp
import sys


from oauth2creds import get_credentials

credentials=get_credentials()

app = Sanic(__name__)


async def fetch(session, url):
    """
    Use session object to perform 'get' request on url
    """
    creds = get_credentials()

    headers = {
        "Authorization": f"Bearer {creds.token}"
    }
    async with session.get(url, headers=headers) as result:
        return await result.json()


@app.route('/')
async def handle_request(request):
    url = r"https://www.googleapis.com/drive/v3/files/root"
    async with aiohttp.ClientSession() as session:
        result = await fetch(session, url)
Exemplo n.º 9
0
from dataclasses import dataclass
import oauth2creds
from googleapiclient.discovery import build
from loguru import logger

import utils
from me_models import Gphoto, Gphoto_state

FOLDER = "application/vnd.google-apps.folder"
FILE_FIELDS = "id,imageMediaMetadata/time,md5Checksum,mimeType,name,originalFilename,ownedByMe,parents,size,trashed"
INIT_FIELDS = f"files({FILE_FIELDS}), nextPageToken"
UPDATE_FIELDS = f"changes(file({FILE_FIELDS}),fileId,removed),nextPageToken"
MIME_FILTER = ["image", "video", "application/vnd.google-apps.folder"]

cfg = utils.config()
service = build("drive", "v3", credentials=oauth2creds.get_credentials())
me.connect(
    db=cfg.gphotos.database, host=cfg.gphotos.host, alias=cfg.gphotos.gphoto_db_alias
)


def main():
    gsync = GphotoSync()
    gsync.sync()


class GphotoSync:
    def __init__(self):
        self.root = self.get_root()

    def sync(self):
Exemplo n.º 10
0
from dataclasses import dataclass
import oauth2creds
from googleapiclient.discovery import build
from loguru import logger

import utils
from me_models import Gphoto, Gphoto_state

FOLDER = "application/vnd.google-apps.folder"
FILE_FIELDS = "id,imageMediaMetadata/time,md5Checksum,mimeType,name,originalFilename,ownedByMe,parents,size,trashed"
INIT_FIELDS = f"files({FILE_FIELDS}), nextPageToken"
UPDATE_FIELDS = f"changes(file({FILE_FIELDS}),fileId,removed),nextPageToken"
MIME_FILTER = ["image", "video", "application/vnd.google-apps.folder"]

cfg = utils.config()
service = build("drive", "v3", credentials=oauth2creds.get_credentials())
me.connect(db=cfg.gphotos.database,
           host=cfg.gphotos.host,
           alias=cfg.gphotos.gphoto_db_alias)


def main():
    gsync = GphotoSync()
    gsync.sync()


class GphotoSync:
    def __init__(self):
        self.root = self.get_root()

    def sync(self):