def test_compare():
    results = {url: [[None, None, None], [None, None, None]] for url in SAMPLE_URLS}
    tries = 0
    for url in SAMPLE_URLS:
        tries = tries + 1
        if tries > 1:
            sleep(5)

        try:
            tstart0 = time()
            art0 = browser.fetch(url, CONFIGS_0)
            tend0 = time()
            results[url][0][0] = art0.text
            results[url][0][1] = len(art0.html)
            results[url][0][2] = tend0 - tstart0
        except Exception as e:
            results[url][0][0] = e
            continue

        sleep(5)

        try:
            tstart1 = time()
            art1 = browser.fetch(url, CONFIGS_1)
            tend1 = time()
            results[url][1][0] = art1.text
            results[url][1][1] = len(art1.html)
            results[url][1][2] = tend1 - tstart1
        except Exception as e:
            results[url][1][0] = e
            continue

    for (k, v) in results.items():
        netloc = urlparse(k).netloc
        r0, r1 = v
        print(
            "%s\n    0: %s | %s | %s\n    1: %s | %s | %s"
            % (
                netloc.ljust(22)[:20],
                "-" if r0[2] is None else str(r0[2]),
                "-" if r0[1] is None else str(r0[1]),
                str(r0[0]).replace("\n", " ").ljust(42)[:40],
                "-" if r1[2] is None else str(r1[2]),
                "-" if r1[1] is None else str(r1[1]),
                str(r1[0])
            )
        )

    errs = [
        v
        for v in results.values()
        if isinstance(v[0][0], Exception) or isinstance(v[1][0], Exception)
    ]

    if len(errs) > 0:
        raise ValueError("%d errors fetching" % (len(errs),))
Exemple #2
0
from browser import fetch


def fetch_handler(res):
    print(f"headers: {res['headers']}")


fetch(
    "https://httpbin.org/get",
    response_format="json",
    headers={
        "X-Header-Thing": "rustpython is neat!"
    },
).then(fetch_handler, lambda err: print(f"error: {err}"))
Exemple #3
0
def _fetch_article(url: str) -> FetchedArticle:
    configs = browser.configs_for_url(url)
    return browser.fetch(url, configs)
Exemple #4
0
from browser import fetch, alert

def fetch_handler(repos):
    star_sum = 0
    for repo in repos:
        star_sum += repo['stars']
    alert(f'Average github trending star count: {star_sum / len(repos)}')

fetch(
    'https://github-trending-api.now.sh/repositories',
    response_format='json',
).then(fetch_handler, lambda err: alert(f"Error: {err}"))