Last active
March 26, 2018 16:11
-
-
Save amitnabarro/0222b462a8334ff37928998768d441e6 to your computer and use it in GitHub Desktop.
Fetch multiple requests from GitHub jobs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import aiohttp | |
import json | |
import itertools | |
SEARCH_URL = 'https://jobs.github.com/positions.json?' | |
async def fetch_job_opportunities(session, url): | |
async with session.get(url) as response: | |
return json.loads(await response.text()) | |
async def main(): | |
futures = [] | |
async with aiohttp.ClientSession() as session: | |
futures.append(fetch_job_opportunities(session, SEARCH_URL + 'description=Python')) | |
futures.append(fetch_job_opportunities(session, SEARCH_URL + 'description=Javascript')) | |
futures.append(fetch_job_opportunities(session, SEARCH_URL + 'description=Clojure')) | |
results = await asyncio.gather(*futures) | |
jobs = list(itertools.chain.from_iterable(results)) | |
print('Total number of jobs: %s' % len(jobs)) | |
try: | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) | |
finally: | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment