Initial commit
This commit is contained in:
commit
ad39827784
6 changed files with 2259 additions and 0 deletions
63
bin/rcd-api-langs
Executable file
63
bin/rcd-api-langs
Executable file
|
|
@ -0,0 +1,63 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
from: https://codereview.stackexchange.com/q/213026
|
||||
"""
|
||||
from functools import partial
|
||||
from operator import itemgetter
|
||||
from typing import (
|
||||
Dict,
|
||||
Iterator,
|
||||
Union)
|
||||
|
||||
import requests
|
||||
|
||||
URL = 'http://www.rosettacode.org/w/api.php'
|
||||
REQUEST_PARAMETERS = dict(
|
||||
action='query',
|
||||
list='categorymembers',
|
||||
cmlimit=1000,
|
||||
cmsort='timestamp',
|
||||
rawcontinue=True,
|
||||
format='json')
|
||||
|
||||
|
||||
def all_langs(
|
||||
*,
|
||||
url: str,
|
||||
request_params: Dict[str, Union[str, int, bool]]
|
||||
) -> Iterator[str]:
|
||||
|
||||
with requests.Session() as session:
|
||||
langs = partial(
|
||||
langs_titles,
|
||||
session=session,
|
||||
url=url,
|
||||
**request_params)
|
||||
|
||||
all_langs = langs(cmtitle='Category:Programming_Languages')
|
||||
for lang in all_langs:
|
||||
print(lang)
|
||||
|
||||
|
||||
def langs_titles(
|
||||
*,
|
||||
session: requests.Session,
|
||||
url: str,
|
||||
**params: Union[str, int, bool]) -> Iterator[str]:
|
||||
|
||||
"""Yields langs names for a specified category"""
|
||||
while True:
|
||||
request = session.get(url, params=params)
|
||||
data = request.json()
|
||||
yield from map(itemgetter('title'), data['query']['categorymembers'])
|
||||
query_continue = data.get('query-continue')
|
||||
if query_continue is None:
|
||||
return
|
||||
params.update(query_continue['categorymembers'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
langs = all_langs(
|
||||
url=URL,
|
||||
request_params=REQUEST_PARAMETERS)
|
||||
63
bin/rcd-api-tasks
Executable file
63
bin/rcd-api-tasks
Executable file
|
|
@ -0,0 +1,63 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
from: https://codereview.stackexchange.com/q/213026
|
||||
"""
|
||||
from functools import partial
|
||||
from operator import itemgetter
|
||||
from typing import (
|
||||
Dict,
|
||||
Iterator,
|
||||
Union)
|
||||
|
||||
import requests
|
||||
|
||||
URL = 'http://www.rosettacode.org/w/api.php'
|
||||
REQUEST_PARAMETERS = dict(
|
||||
action='query',
|
||||
list='categorymembers',
|
||||
cmlimit=1000,
|
||||
cmsort='timestamp',
|
||||
rawcontinue=True,
|
||||
format='json')
|
||||
|
||||
|
||||
def all_tasks(
|
||||
*,
|
||||
url: str,
|
||||
request_params: Dict[str, Union[str, int, bool]]
|
||||
) -> Iterator[str]:
|
||||
|
||||
with requests.Session() as session:
|
||||
tasks = partial(
|
||||
tasks_titles,
|
||||
session=session,
|
||||
url=url,
|
||||
**request_params)
|
||||
|
||||
all_tasks = tasks(cmtitle='Category:Programming_Tasks')
|
||||
for task in all_tasks:
|
||||
print(task)
|
||||
|
||||
|
||||
def tasks_titles(
|
||||
*,
|
||||
session: requests.Session,
|
||||
url: str,
|
||||
**params: Union[str, int, bool]) -> Iterator[str]:
|
||||
|
||||
"""Yields tasks names for a specified category"""
|
||||
while True:
|
||||
request = session.get(url, params=params)
|
||||
data = request.json()
|
||||
yield from map(itemgetter('title'), data['query']['categorymembers'])
|
||||
query_continue = data.get('query-continue')
|
||||
if query_continue is None:
|
||||
return
|
||||
params.update(query_continue['categorymembers'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
tasks = all_tasks(
|
||||
url=URL,
|
||||
request_params=REQUEST_PARAMETERS)
|
||||
Loading…
Add table
Add a link
Reference in a new issue