Skip to content

Instantly share code, notes, and snippets.

@bbelderbos
Created July 11, 2018 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbelderbos/90d2a4fdbac6ae4a7581be9b2d4f054b to your computer and use it in GitHub Desktop.
Save bbelderbos/90d2a4fdbac6ae4a7581be9b2d4f054b to your computer and use it in GitHub Desktop.
has a topic been discussed on python bytes?
# caches python bytes podcast links to a file and searches links on first cli
# arg string (case insensitive)
import os
import re
import sys
import time
import feedparser
from cfonts import render, say
say('Discussed at Python Bytes?', font='chrome', size=(122, 32))
RSS = 'https://pythonbytes.fm/episodes/rss'
CACHE = 'links.out'
ONE_DAY = 24*60*60
A_HREF = re.compile(r'^<a href="([^"]+)">(?:<strong>)?(.*?)(?:</strong>)?</a>$') # noqa E 501
def _expired_cache():
return (time.time() - os.stat(CACHE).st_mtime) > ONE_DAY
def cache_feed():
if os.path.isfile(CACHE) and not _expired_cache():
return
data = feedparser.parse(RSS)
output = []
for entry in data['entries']:
episode = entry['itunes_episode']
date = entry['published']
summary = entry['summary']
links = re.findall(r'<a.*?</a>', summary)
for url in links:
m = A_HREF.match(url)
if m:
url, title = m.groups()
else:
title = ''
output.append(f'{episode:<4} | {date} | {title} | {url}')
with open(CACHE, 'w') as f:
f.write('\n'.join(output))
def ireplace(old, new, text):
"""https://stackoverflow.com/a/4759732"""
index_l = text.lower().index(old.lower())
return text[:index_l] + new + text[index_l + len(old):]
def search_episode_links(grep):
"""searches through href and names"""
grep = grep.lower()
with open(CACHE) as f:
matching_lines = [line.strip() for line in f.readlines()
if grep in line]
if matching_lines:
for line in matching_lines:
match_out = render(grep.upper(), font="console",
colors=['candy'], space=False)
print(ireplace(grep, match_out, line))
else:
print(f'No matches for "{grep}"')
if __name__ == '__main__':
if len(sys.argv) < 2:
print(f'Usage: {sys.argv[0]} search-term')
sys.exit(1)
cache_feed()
grep = sys.argv[1]
search_episode_links(grep)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment