Last active
September 14, 2022 04:02
-
-
Save Ferdi265/3587406c1035021a1707 to your computer and use it in GitHub Desktop.
anews - Arch Linux news python script
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
#!/usr/bin/env python3 | |
import os | |
import sys | |
import time | |
import termcolor | |
import feedparser | |
# Usage: | |
# anews [number of headlines (optional)] | |
# e.g. "anews 6" or "anews" | |
# Dependencies: | |
# python | |
# python-feedparser | |
# python-termcolor | |
# Files: | |
# $HOME/.anews.last - stores the timestamp of last news retrieval | |
def last_io(): | |
global last | |
try: | |
with open(os.environ['HOME'] + '/.anews.last', 'r') as f: | |
last = time.strptime(f.readline(), '%Y-%m-%d %H:%M:%S\n') | |
except: | |
print('Unable to read ~/.anews.last, assuming unix epoch') | |
last = time.strptime('1970-01-01 00:00:00', '%Y-%m-%d %H:%M:%S') | |
try: | |
with open(os.environ['HOME'] + '/.anews.last', 'w') as f: | |
f.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n') | |
except: | |
print('Unable to write ~/.anews.last, skipping') | |
def feed_io(): | |
global feed | |
feed = feedparser.parse('https://www.archlinux.org/feeds/news') | |
def titles(n): | |
last_io() | |
feed_io() | |
for i in range(n): | |
entry = feed['entries'][i] | |
print(termcolor.colored('#' + str(i + 1), 'yellow', attrs = ['bold']), termcolor.colored(entry['title'], 'blue' if entry['published_parsed'] < last else 'green', attrs = ['bold']), termcolor.colored('by', 'white'), termcolor.colored(entry['author'], 'white', attrs = ['bold'])) | |
print(' ', time.strftime(termcolor.colored('[%Y-%m-%d %H:%M:%S]', 'white', attrs = ['bold']), entry['published_parsed']), entry['link']) | |
def shift_args(): | |
if len(sys.argv) < 1: | |
raise ValueError('Invalid arguments') | |
tmp, *sys.argv = sys.argv | |
return tmp | |
def parse_args(): | |
shift_args() # discard script name | |
num = 10 | |
if len(sys.argv) != 0: | |
num = int(shift_args()) | |
titles(num) | |
parse_args() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment