Skip to content

Commit aaf20b0

Browse files
committed
Day 45. TalkPython 100 days of code
1 parent aec31b1 commit aaf20b0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from pprint import pprint
2+
3+
from tabulate import tabulate
4+
import requests
5+
import click
6+
7+
8+
def search_talkpython_api(keyword):
9+
link = 'http://search.talkpython.fm/api/search?q={}'
10+
resp = requests.get(link.format(keyword.replace(' ', '-')))
11+
resp.raise_for_status()
12+
13+
results = resp.json()
14+
episodes = {int(ep['id']): ep['title']
15+
for ep in results['results'] if ep['category'] == 'Episode'}
16+
return episodes
17+
18+
19+
@click.command()
20+
@click.argument('keyword')
21+
@click.option('--table', is_flag=True)
22+
def show_result(keyword, table):
23+
result = search_talkpython_api(keyword)
24+
print(f'There are {len(result)} matching episodes:')
25+
if table:
26+
print(tabulate(result.items(),
27+
showindex=range(1, len(result)+1),
28+
headers=('Index', 'Show number', 'Title')))
29+
else:
30+
for idx, (number, title) in enumerate(result.items(), 1):
31+
print(f'{idx}. {title}')
32+
33+
34+
if __name__ == "__main__":
35+
show_result()

0 commit comments

Comments
 (0)