Skip to content

Instantly share code, notes, and snippets.

@eirenik0
Forked from madjar/scrapper.py
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save eirenik0/e13fbed9e71b5434ae66 to your computer and use it in GitHub Desktop.

Select an option

Save eirenik0/e13fbed9e71b5434ae66 to your computer and use it in GitHub Desktop.

Revisions

  1. @madjar madjar revised this gist Mar 2, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion scrapper.py
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ def get(*args, **kwargs):

    @asyncio.coroutine
    def wait_with_progress(coros):
    for f in tqdm(asyncio.as_completed(coros), total=len(coros)):
    for f in tqdm.tqdm(asyncio.as_completed(coros), total=len(coros)):
    yield from f


  2. @madjar madjar created this gist Mar 2, 2014.
    38 changes: 38 additions & 0 deletions scrapper.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    import asyncio
    import aiohttp
    import bs4
    import tqdm


    @asyncio.coroutine
    def get(*args, **kwargs):
    response = yield from aiohttp.request('GET', *args, **kwargs)
    return (yield from response.read_and_close(decode=True))


    @asyncio.coroutine
    def wait_with_progress(coros):
    for f in tqdm(asyncio.as_completed(coros), total=len(coros)):
    yield from f


    def first_magnet(page):
    soup = bs4.BeautifulSoup(page)
    a = soup.find('a', title='Download this torrent using magnet')
    return a['href']


    @asyncio.coroutine
    def print_magnet(query):
    url = 'http://thepiratebay.se/search/{}/0/7/0'.format(query)
    with (yield from sem):
    page = yield from get(url, compress=True)
    magnet = first_magnet(page)
    print('{}: {}'.format(query, magnet))


    distros = ['archlinux', 'ubuntu', 'debian']
    sem = asyncio.Semaphore(5)
    loop = asyncio.get_event_loop()
    f = asyncio.wait([print_magnet(d) for d in distros])
    loop.run_until_complete(f)