Example #1
0
def __get_session() -> requests.Session:
    """Get or create a requests session for scryfall."""
    if mtgjson4.USE_CACHE.get():
        requests_cache.install_cache(
            "scryfall_cache",
            backend="sqlite",
            expire_after=mtgjson4.SESSION_CACHE_EXPIRE_SCRYFALL,
        )

    session: Optional[requests.Session] = SESSION.get(None)
    if session is None:
        session = requests.Session()

        if mtgjson4.CONFIG_PATH.is_file():
            # Open and read MTGJSON secret properties
            config = configparser.RawConfigParser()
            config.read(mtgjson4.CONFIG_PATH)
            header_auth = {
                "Authorization":
                "Bearer " + config.get("Scryfall", "client_secret")
            }
            session.headers.update(header_auth)
            LOGGER.info("Fetching from Scryfall with authentication")
        else:
            LOGGER.warning("Fetching from Scryfall WITHOUT authentication")

        session = util.retryable_session(session)
        SESSION.set(session)
    return session
Example #2
0
def __get_session() -> requests.Session:
    """
    Get or create a requests session for MTGStocks.
    :return Session data
    """
    if mtgjson4.USE_CACHE.get():
        requests_cache.install_cache(
            "stocks_cache",
            backend="sqlite",
            expire_after=mtgjson4.SESSION_CACHE_EXPIRE_STOCKS,
        )

    session: Optional[requests.Session] = SESSION.get(None)
    if session is None:
        session = requests.Session()

        if mtgjson4.CONFIG_PATH.is_file():
            # Open and read MTGJSON secret properties
            config = configparser.RawConfigParser()
            config.read(mtgjson4.CONFIG_PATH)
            SESSION_TOKEN.set(config.get("MTGStocks", "token"))

        session = util.retryable_session(session)
        SESSION.set(session)
    return session
Example #3
0
def __get_session() -> requests.Session:
    """Get or create a requests session for TCGPlayer."""
    global GH_DB_URL, GH_DB_KEY, GH_API_KEY, GH_API_USER, GH_DB_FILE

    if mtgjson4.USE_CACHE.get(False):
        requests_cache.install_cache(
            str(mtgjson4.PROJECT_CACHE_PATH.joinpath("tcgplayer_cache")),
            expire_after=mtgjson4.SESSION_CACHE_EXPIRE_TCG,
        )

    session: Optional[requests.Session] = SESSION.get(None)
    if session is None:
        session = requests.Session()
        header_auth = {"Authorization": "Bearer " + _request_tcgplayer_bearer()}

        # Open and read MTGJSON secret properties
        config = configparser.RawConfigParser()
        config.read(mtgjson4.CONFIG_PATH)
        GH_API_USER = config.get("CardHoarder", "gh_api_user")
        GH_API_KEY = config.get("CardHoarder", "gh_api_key")
        GH_DB_KEY = config.get("CardHoarder", "gh_db_key")
        GH_DB_FILE = config.get("CardHoarder", "gh_db_file")
        GH_DB_URL = f"https://gist.github.com/{GH_DB_KEY}"

        session.headers.update(header_auth)
        session = util.retryable_session(session)
        SESSION.set(session)
    return session
Example #4
0
def __get_session() -> requests.Session:
    """
    Get or create a requests session for CardHoarder.
    :return Session data
    """
    global GH_DB_URL, GH_DB_KEY, GH_API_KEY, GH_API_USER, GH_DB_FILE

    session: Optional[requests.Session] = SESSION.get(None)
    if session is None:
        session = requests.Session()

        if mtgjson4.CONFIG_PATH.is_file():
            # Open and read MTGJSON secret properties
            config = configparser.RawConfigParser()
            config.read(mtgjson4.CONFIG_PATH)
            SESSION_TOKEN.set(config.get("CardHoarder", "token"))

            GH_API_USER = config.get("CardHoarder", "gh_api_user")
            GH_API_KEY = config.get("CardHoarder", "gh_api_key")
            GH_DB_KEY = config.get("CardHoarder", "gh_db_key")
            GH_DB_FILE = config.get("CardHoarder", "gh_db_file")
            GH_DB_URL = f"https://gist.github.com/{GH_DB_KEY}"

        session = util.retryable_session(session)
        SESSION.set(session)
    return session
Example #5
0
def __get_session() -> requests.Session:
    """
    Get or create a requests session for MTGStocks.
    :return Session data
    """
    if mtgjson4.USE_CACHE.get():
        requests_cache.install_cache(
            "stocks_cache",
            backend="sqlite",
            expire_after=mtgjson4.SESSION_CACHE_EXPIRE_STOCKS,
        )

    session: Optional[requests.Session] = SESSION.get(None)
    if session is None:
        session = requests.Session()

        if mtgjson4.CONFIG_PATH.is_file():
            # Open and read MTGJSON secret properties
            config = configparser.RawConfigParser()
            config.read(mtgjson4.CONFIG_PATH)
            SESSION_TOKEN.set(config.get("MTGStocks", "token"))

        session = util.retryable_session(session)
        SESSION.set(session)
    return session
Example #6
0
def __get_session() -> requests.Session:
    """Get or create a requests session for scryfall."""
    if mtgjson4.USE_CACHE.get():
        requests_cache.install_cache(
            "scryfall_cache",
            backend="sqlite",
            expire_after=mtgjson4.SESSION_CACHE_EXPIRE_SCRYFALL,
        )

    session: Optional[requests.Session] = SESSION.get(None)
    if session is None:
        session = requests.Session()

        if mtgjson4.CONFIG_PATH.is_file():
            # Open and read MTGJSON secret properties
            config = configparser.RawConfigParser()
            config.read(mtgjson4.CONFIG_PATH)
            header_auth = {
                "Authorization": "Bearer " + config.get("Scryfall", "client_secret")
            }
            session.headers.update(header_auth)
            LOGGER.info("Fetching from Scryfall with authentication")
        else:
            LOGGER.warning("Fetching from Scryfall WITHOUT authentication")

        session = util.retryable_session(session)
        SESSION.set(session)
    return session
Example #7
0
def _get_session() -> requests.Session:
    """Get or create a requests session for gatherer."""
    session: Optional[requests.Session] = SESSION.get(None)
    if session is None:
        session = requests.Session()
        session = util.retryable_session(session)
        SESSION.set(session)
    return session
Example #8
0
def __get_session() -> requests.Session:
    """Get or create a requests session for TCGPlayer."""
    session: Optional[requests.Session] = SESSION.get(None)
    if session is None:
        session = requests.Session()
        header_auth = {
            "Authorization": "Bearer " + _request_tcgplayer_bearer()
        }
        session.headers.update(header_auth)
        session = util.retryable_session(session)
        SESSION.set(session)
    return session
Example #9
0
def download(scryfall_url: str) -> Dict[str, Any]:
    """
    Get the data from Scryfall in JSON format using our secret keys
    :param scryfall_url: URL to download JSON data from
    :return: JSON object of the Scryfall data
    """
    session = util.retryable_session(SESSION.get())
    response = session.get(url=scryfall_url, timeout=5.0)
    request_api_json: Dict[str, Any] = response.json()

    LOGGER.info("Downloaded URL: {0}".format(scryfall_url))

    return request_api_json
Example #10
0
def __get_session() -> requests.Session:
    """Get or create a requests session for TCGPlayer."""
    if mtgjson4.USE_CACHE.get():
        requests_cache.install_cache(
            "tcg_cache",
            backend="sqlite",
            expire_after=mtgjson4.SESSION_CACHE_EXPIRE_TCG,
        )

    session: Optional[requests.Session] = SESSION.get(None)
    if session is None:
        session = requests.Session()
        header_auth = {"Authorization": "Bearer " + _request_tcgplayer_bearer()}
        session.headers.update(header_auth)
        session = util.retryable_session(session)
        SESSION.set(session)
    return session
Example #11
0
def __get_session() -> requests.Session:
    """Get or create a requests session for TCGPlayer."""
    if mtgjson4.USE_CACHE.get():
        requests_cache.install_cache(
            str(mtgjson4.PROJECT_CACHE_PATH.joinpath("tcgplayer_cache")),
            expire_after=mtgjson4.SESSION_CACHE_EXPIRE_TCG,
        )

    session: Optional[requests.Session] = SESSION.get(None)
    if session is None:
        session = requests.Session()
        header_auth = {
            "Authorization": "Bearer " + _request_tcgplayer_bearer()
        }
        session.headers.update(header_auth)
        session = util.retryable_session(session)
        SESSION.set(session)
    return session
Example #12
0
def download(card_mid: str) -> bs4.BeautifulSoup:
    """
    Download a specific card from gatherer
    :param card_mid: card id to download
    :return: HTML soup parser of the resulting page
    """
    session = util.retryable_session(SESSION.get())
    response = session.get(
        url=GATHERER_CARD,
        params={
            "multiverseid": str(card_mid),
            "printed": "true"
        },
        timeout=5.0,
    )

    soup: bs4.BeautifulSoup = bs4.BeautifulSoup(response.text, "html.parser")
    LOGGER.info("Downloaded URL {}".format(response.url))
    return soup
Example #13
0
def get_session() -> requests.Session:
    """Get or create a requests session for scryfall."""
    session: Optional[requests.Session] = SESSION.get(None)
    if session is None:
        session = requests.Session()

        if pathlib.Path(mtgjson4.CONFIG_PATH).is_file():
            # Open and read MTGJSON secret properties
            config = configparser.RawConfigParser()
            config.read(mtgjson4.CONFIG_PATH)
            header_auth = {
                "Authorization":
                "Bearer " + config.get("Scryfall", "client_secret")
            }
            session.headers.update(header_auth)
            LOGGER.info("Fetching from Scryfall with authentication")
        else:
            LOGGER.warning("Fetching from Scryfall WITHOUT authentication")

        session = util.retryable_session(session)
        SESSION.set(session)
    return session