From 443d6faa5627d9d732458b32a71acbf0ab0b534d Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Mon, 18 Feb 2019 15:17:41 +0530 Subject: [PATCH 1/6] wip --- google_cl/cli.py | 22 +++++++++++++--------- setup.py | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/google_cl/cli.py b/google_cl/cli.py index 1cb7b99..29687f4 100644 --- a/google_cl/cli.py +++ b/google_cl/cli.py @@ -4,15 +4,19 @@ import sys import click +@click.group() +def cli(): + click.echo("Sunday Monday") -@click.command() -def main(args=None): - """Console script for google_cl.""" - click.echo("Replace this message by putting your code into " - "google_cl.cli.main") - click.echo("See click documentation at http://click.pocoo.org/") - return 0 +@cli.group() +def picasa(): + click.echo("This is a list") -if __name__ == "__main__": - sys.exit(main()) # pragma: no cover +@cli.group() +def contacts(): + click.echo("contacts placeholder") + +@picasa.command() +def list(): + click.echo("This is coming from picasa list") diff --git a/setup.py b/setup.py index 9f8ba98..16533ca 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@ description="Pythonic interface to interact with google services", entry_points={ 'console_scripts': [ - 'google_cl=google_cl.cli:main', + 'google=google_cl.cli:cli', ], }, install_requires=requirements, From ee0ffecca7d9d2db674d09d44233f2621b42a83c Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Tue, 27 Aug 2019 16:08:56 +0530 Subject: [PATCH 2/6] cleanup: pylint fixes --- google_cl/cli.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) mode change 100644 => 100755 google_cl/cli.py diff --git a/google_cl/cli.py b/google_cl/cli.py old mode 100644 new mode 100755 index 29687f4..d3d7203 --- a/google_cl/cli.py +++ b/google_cl/cli.py @@ -17,6 +17,7 @@ def picasa(): def contacts(): click.echo("contacts placeholder") + @picasa.command() -def list(): +def photolist(): click.echo("This is coming from picasa list") From 7bfcac95392132f466f57e71f6305016ccca3ce8 Mon Sep 17 00:00:00 2001 From: Vinit Date: Sun, 28 Jun 2020 14:15:58 +0530 Subject: [PATCH 3/6] feat: update readme and add some dependecies --- README.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.rst | 41 --------------------------------------- setup.py | 2 +- 3 files changed, 57 insertions(+), 42 deletions(-) create mode 100644 README.md delete mode 100644 README.rst diff --git a/README.md b/README.md new file mode 100644 index 0000000..4f519b2 --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +google\_cl +========== + +[![image](https://img.shields.io/pypi/v/google_cl.svg)](https://pypi.python.org/pypi/google_cl) + +[![image](https://img.shields.io/travis/vinitkumar/google_cl.svg)](https://travis-ci.org/vinitkumar/google_cl) + +[![Documentation Status](https://readthedocs.org/projects/google-cl/badge/?version=latest)](https://google-cl.readthedocs.io/en/latest/?badge=latest) + +[![Updates](https://pyup.io/repos/github/vinitkumar/google_cl/shield.svg)](https://pyup.io/repos/github/vinitkumar/google_cl/) + +Pythonic interface to interact with google services + +- Free software: Apache Software License 2.0 +- Documentation: . + +Features +-------- + +- TODO + +Contributors +++++++ + +Code Contributors +----------------- + +This project exists thanks to all the people who contribute. +\[\[Contribute\](CONTRIBUTING.md)\]. .. image:: + + +Financial Contributors \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-- + +Become a financial contributor and help us sustain our community. +[Contribute](https://opencollective.com/googlecl) + +### Individuals + +[![image](https://opencollective.com/googlecl/individuals.svg?width=890)](https://opencollective.com/googlecl) + +### Organizations + +Support this project with your organization. Your logo will show up here +with a link to your website. +[Contribute](https://opencollective.com/googlecl) + +[![image](https://opencollective.com/googlecl/organization/0/avatar.svg)](https://opencollective.com/googlecl/organization/0/website) + +- TODO + +Credits +------- + +This package was created with +[Cookiecutter](https://github.com/audreyr/cookiecutter) and the +[audreyr/cookiecutter-pypackage](https://github.com/audreyr/cookiecutter-pypackage) +project template. diff --git a/README.rst b/README.rst deleted file mode 100644 index 8d0a198..0000000 --- a/README.rst +++ /dev/null @@ -1,41 +0,0 @@ -========= -google_cl -========= - - -.. image:: https://img.shields.io/pypi/v/google_cl.svg - :target: https://pypi.python.org/pypi/google_cl - -.. image:: https://img.shields.io/travis/vinitkumar/google_cl.svg - :target: https://travis-ci.org/vinitkumar/google_cl - -.. image:: https://readthedocs.org/projects/google-cl/badge/?version=latest - :target: https://google-cl.readthedocs.io/en/latest/?badge=latest - :alt: Documentation Status - - -.. image:: https://pyup.io/repos/github/vinitkumar/google_cl/shield.svg - :target: https://pyup.io/repos/github/vinitkumar/google_cl/ - :alt: Updates - - - -Pythonic interface to interact with google services - - -* Free software: Apache Software License 2.0 -* Documentation: https://google-cl.readthedocs.io. - - -Features --------- - -* TODO - -Credits -------- - -This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template. - -.. _Cookiecutter: https://github.com/audreyr/cookiecutter -.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage diff --git a/setup.py b/setup.py index 16533ca..39bdaeb 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ requirements = ['Click>=6.0', ] -setup_requirements = ['pytest-runner', ] +setup_requirements = ['google-auth==1.6.3', 'pytest-runner', ] test_requirements = ['pytest', ] From 25d09a0abacfe6f5a8314b4db810ea4ea98ceb9e Mon Sep 17 00:00:00 2001 From: Vinit Date: Sun, 28 Jun 2020 14:18:54 +0530 Subject: [PATCH 4/6] update readme --- README.md | 37 ++----------------------------------- 1 file changed, 2 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 4f519b2..220c04a 100644 --- a/README.md +++ b/README.md @@ -17,40 +17,7 @@ Pythonic interface to interact with google services Features -------- -- TODO +TODO: -Contributors +++++++ +- Implement core Google services -Code Contributors ------------------ - -This project exists thanks to all the people who contribute. -\[\[Contribute\](CONTRIBUTING.md)\]. .. image:: - - -Financial Contributors \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-- - -Become a financial contributor and help us sustain our community. -[Contribute](https://opencollective.com/googlecl) - -### Individuals - -[![image](https://opencollective.com/googlecl/individuals.svg?width=890)](https://opencollective.com/googlecl) - -### Organizations - -Support this project with your organization. Your logo will show up here -with a link to your website. -[Contribute](https://opencollective.com/googlecl) - -[![image](https://opencollective.com/googlecl/organization/0/avatar.svg)](https://opencollective.com/googlecl/organization/0/website) - -- TODO - -Credits -------- - -This package was created with -[Cookiecutter](https://github.com/audreyr/cookiecutter) and the -[audreyr/cookiecutter-pypackage](https://github.com/audreyr/cookiecutter-pypackage) -project template. From e128eb3abdb3c52cee2c3f65afbd12acaceac9dc Mon Sep 17 00:00:00 2001 From: Vinit Date: Sun, 28 Jun 2020 15:01:36 +0530 Subject: [PATCH 5/6] feat: add file for authorize and add reqired deps --- setup.py | 8 +-- {google_cl => src}/__init__.py | 0 src/authorize.py | 114 ++++++++++++++++++++++++++++++++ {google_cl => src}/cli.py | 0 {google_cl => src}/google_cl.py | 0 5 files changed, 118 insertions(+), 4 deletions(-) rename {google_cl => src}/__init__.py (100%) create mode 100644 src/authorize.py rename {google_cl => src}/cli.py (100%) rename {google_cl => src}/google_cl.py (100%) diff --git a/setup.py b/setup.py index 39bdaeb..175f628 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ from setuptools import setup, find_packages -with open('README.rst') as readme_file: +with open('README.md') as readme_file: readme = readme_file.read() with open('HISTORY.rst') as history_file: @@ -13,7 +13,7 @@ requirements = ['Click>=6.0', ] -setup_requirements = ['google-auth==1.6.3', 'pytest-runner', ] +setup_requirements = ['google-auth==1.6.3', 'pytest-runner', "requests_oauthlib", ] test_requirements = ['pytest', ] @@ -36,7 +36,7 @@ description="Pythonic interface to interact with google services", entry_points={ 'console_scripts': [ - 'google=google_cl.cli:cli', + 'google=src.cli:cli', ], }, install_requires=requirements, @@ -45,7 +45,7 @@ include_package_data=True, keywords='google_cl', name='google_cl', - packages=find_packages(include=['google_cl']), + packages=find_packages(include=['src']), setup_requires=setup_requirements, test_suite='tests', tests_require=test_requirements, diff --git a/google_cl/__init__.py b/src/__init__.py similarity index 100% rename from google_cl/__init__.py rename to src/__init__.py diff --git a/src/authorize.py b/src/authorize.py new file mode 100644 index 0000000..734de71 --- /dev/null +++ b/src/authorize.py @@ -0,0 +1,114 @@ +# Source of this file: https://github.com/gilesknap/gphotos-sync/blob/master/gphotos/authorize.py + +import logging +from typing import List, Optional + +from requests.adapters import HTTPAdapter +from requests_oauthlib import OAuth2Session + +from pathlib import Path +from urllib3.util.retry import Retry + + + +from json import load, dump, JSONDecodeError + +logger = logging.getLogger(__name__) + +# OAuth endpoints given in the Google API documentation +authorization_base_url = "https://accounts.google.com/o/oauth2/v2/auth" +token_uri = "https://www.googleapis.com/oauth2/v4/token" + + + +class Authorize: + def __init__( + self, + scope: List[str], + token_file: Path, + secrets_file: Path, + max_retries: int = 5, + ): + self.max_retries = max_retries + self.scope: List[str] = scope + self.token_file: Path = token_file + self.session = None + self.token = None + + + try: + with secrets_file.open("r") as stream: + all_json = load(stream) + + secrets = all_json["installed"] + sef.client_id = secrets["client_id"] + self.client_secret = secrets["client_secret"] + self.redirect_uri = secrets["redirect_uris"][0] + self.token_uri = secrets["token_uri"] + self.extra = { + "client_id": self.client_id, + "client_secret": self.client_secret, + } + except (JSONDecodeError, IOError): + print(f"missing or bad secrets file: {secrets_file}") + exit(1) + + def load_token(self) -> Optional[str]: + try: + with self.token_file.open("r") as stream: + token = load(stream) + except (JSONDecodeError, IOError): + return None + return token + + def save_token(self, token: str): + with self.token_file.open("w") as stream: + dump(token, stream) + self.token_file.chmod(0o600) + + def authorize(self): + token = self.load_token() + + if token: + self.session = OAuth2Session( + self.client_id, + token=token, + auto_refresh_url=self.token_uri, + auto_refresh_kwargs=self.extra, + token_updater=self.save_token, + ) + else: + self.session = OAuth2Session( + self.client_id, + scope=self.scope, + redirect_uri=self.redirect_uri, + auto_refresh_url=self.token_uri, + auto_refresh_kwargs=self.extra, + token_updater=self.save_token, + ) + + authorization_url, _ = self.session.authorization_url( + authorization_base_url, access_type="offline", prompt="select_account" + ) + print("Please go here and authorize,", authorization_url) + + response_code = input("Paste the response token here:") + self.token = self.session.fetch_token( + self.token_uri, client_secret=self.client_secret, code=response_code) + self.save_token(self.token) + + # set up retry behaviour for authorize session + + retries = Retry( + total=self.max_retries, + backoff_factor=0.5, + status_forcelist=[500, 502, 503, 504], + method_whitelist=frozenset(["GET", "POST"]), + raise_on_status=False, + respect_retry_after_header=True, + ) + + self.session.mount("https://", HTTPAdapter(max_retries=retries)) + + + diff --git a/google_cl/cli.py b/src/cli.py similarity index 100% rename from google_cl/cli.py rename to src/cli.py diff --git a/google_cl/google_cl.py b/src/google_cl.py similarity index 100% rename from google_cl/google_cl.py rename to src/google_cl.py From b1fa23523a2ae2770d0780a025f1a0de787d4720 Mon Sep 17 00:00:00 2001 From: Vinit Date: Sun, 28 Jun 2020 16:23:11 +0530 Subject: [PATCH 6/6] feat: some more code --- setup.py | 5 ++--- src/cli.py | 5 +++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 175f628..995c4c2 100644 --- a/setup.py +++ b/setup.py @@ -11,9 +11,8 @@ with open('HISTORY.rst') as history_file: history = history_file.read() -requirements = ['Click>=6.0', ] - -setup_requirements = ['google-auth==1.6.3', 'pytest-runner', "requests_oauthlib", ] +setup_requirements = ['google-auth==1.6.3', 'pytest-runner', 'requests_oauthlib', 'requests' ] +requirements = ['google-auth==1.6.3', 'pytest-runner', 'requests_oauthlib', 'requests' ] test_requirements = ['pytest', ] diff --git a/src/cli.py b/src/cli.py index d3d7203..54de1e1 100755 --- a/src/cli.py +++ b/src/cli.py @@ -3,11 +3,16 @@ """Console script for google_cl.""" import sys import click +from src.authorize import Authorize @click.group() def cli(): click.echo("Sunday Monday") +@cli.group() +def login(): + Authorize() + @cli.group() def picasa(): click.echo("This is a list")