<reponame>Gitznik/aiogoogle<gh_stars>100-1000
#!/usr/bin/python3.7
from pathlib import Path
import json
import asyncio
import pprint

from aiohttp import ClientSession
from aiogoogle import Aiogoogle
from aiogoogle import HTTPError


def _pop_unstable_apis(all_apis: list):
    return [api for api in all_apis if len(api[1]) <= 3]


async def refresh_disc_docs_json():
    file_errors = []

    # Create new .data/ dir if one doesn't exists
    this_folder = Path(__file__).parent
    data_dir = this_folder / "data"
    data_dir.mkdir(exist_ok=True)

    async with ClientSession() as sess:
        apis_pref = await sess.get(
            "https://www.googleapis.com/discovery/v1/apis?preferred=true"
        )
        apis_pref = await apis_pref.json()

    # Refresh all_apis in tests/tests_globals.py
    all_apis = [(api["name"], api["version"]) for api in apis_pref["items"]]
    all_apis = _pop_unstable_apis(all_apis)
    final_all_apis = all_apis

    async with Aiogoogle() as google:
        tasks = [google.discover(name, version) for (name, version) in all_apis]
        print('Requesting all APIs, this might take a while')
        all_discovery_documents = await asyncio.gather(*tasks, return_exceptions=True)

    # Refresh discovery files in tests/data
    for google_api, (name, version) in zip(all_discovery_documents, all_apis):
        if isinstance(google_api, HTTPError):
            e = google_api
            if e.res.status_code != 404:
                print('Non 404 error')
                print('\033[91m\n' + e + '\n\033[0m')
            else:
                # only ignore if it's a 404 error. Should raise an error otherwise
                final_all_apis = list(filter(lambda api: (api[0] != name), final_all_apis))

            file_errors.append({f"{name}-{version}": str(e)})
            print(f'\033[91mError: Failed to download {name} {version}\033[0m')
            continue

        # Save discovery docuemnt as .json file to the newly created data dir
        json_file = data_dir / f"{name}_{version}_discovery_doc.json"
        with open(str(json_file), "w") as discovery_file:
            json.dump(google_api.discovery_document, discovery_file)
        print(f"saved {name}-{version} to {json_file}")

    print("Done")
    if file_errors:
        print(f"Errors found: {str(file_errors)}")

    with this_folder.joinpath("ALL_APIS.py").open("w") as f:
        f.write("""### This file is autogenerated ###\n""")
        f.write(f"ALL_APIS = {pprint.pformat(final_all_apis)}")
        print("SUCCESS!")


if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(refresh_disc_docs_json())

#!/usr/bin/python3.7
from pathlib import Path
import json
import asyncio
import pprint

from aiohttp import ClientSession
from aiogoogle import Aiogoogle
from aiogoogle import HTTPError


def _pop_unstable_apis(all_apis: list):
    return [api for api in all_apis if len(api[1]) <= 3]


async def refresh_disc_docs_json():
    file_errors = []

    # Create new .data/ dir if one doesn't exists
    this_folder = Path(__file__).parent
    data_dir = this_folder / "data"
    data_dir.mkdir(exist_ok=True)

    async with ClientSession() as sess:
        apis_pref = await sess.get(
            "https://www.googleapis.com/discovery/v1/apis?preferred=true"
        )
        apis_pref = await apis_pref.json()

    # Refresh all_apis in tests/tests_globals.py
    all_apis = [(api["name"], api["version"]) for api in apis_pref["items"]]
    all_apis = _pop_unstable_apis(all_apis)
    final_all_apis = all_apis

    async with Aiogoogle() as google:
        tasks = [google.discover(name, version) for (name, version) in all_apis]
        print('Requesting all APIs, this might take a while')
        all_discovery_documents = await asyncio.gather(*tasks, return_exceptions=True)

    # Refresh discovery files in tests/data
    for google_api, (name, version) in zip(all_discovery_documents, all_apis):
        if isinstance(google_api, HTTPError):
            e = google_api
            if e.res.status_code != 404:
                print('Non 404 error')
                print('\033[91m\n' + e + '\n\033[0m')
            else:
                # only ignore if it's a 404 error. Should raise an error otherwise
                final_all_apis = list(filter(lambda api: (api[0] != name), final_all_apis))

            file_errors.append({f"{name}-{version}": str(e)})
            print(f'\033[91mError: Failed to download {name} {version}\033[0m')
            continue

        # Save discovery docuemnt as .json file to the newly created data dir
        json_file = data_dir / f"{name}_{version}_discovery_doc.json"
        with open(str(json_file), "w") as discovery_file:
            json.dump(google_api.discovery_document, discovery_file)
        print(f"saved {name}-{version} to {json_file}")

    print("Done")
    if file_errors:
        print(f"Errors found: {str(file_errors)}")

    with this_folder.joinpath("ALL_APIS.py").open("w") as f:
        f.write("""### This file is autogenerated ###\n""")
        f.write(f"ALL_APIS = {pprint.pformat(final_all_apis)}")
        print("SUCCESS!")


if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(refresh_disc_docs_json())
#!/usr/bin/python3.7 # Create new .data/ dir if one doesn't exists # Refresh all_apis in tests/tests_globals.py # Refresh discovery files in tests/data # only ignore if it's a 404 error. Should raise an error otherwise # Save discovery docuemnt as .json file to the newly created data dir ### This file is autogenerated ###\n