Last active
July 22, 2020 00:34
-
-
Save ap-Codkelden/c85ef9ba2a6c7ddd1cc6 to your computer and use it in GitHub Desktop.
Check a date of last news at archlinux.org and compare it with saved
This file contains hidden or 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 re | |
import sys | |
import os.path | |
import datetime | |
import urllib.request | |
from os.path import expanduser | |
def get_news_page(): | |
try: | |
f = urllib.request.urlopen('http://www.archlinux.org') | |
except urllib.error.URLError: | |
print('Cannot fetch page content, terminate.') | |
sys.exit() | |
else: | |
return f.read() | |
def check_date(news_file): | |
try: | |
if os.path.isfile(news_file): | |
with open(news_file) as f: | |
saved_date = f.read() | |
print('Last date is ' + saved_date) | |
return datetime.datetime.strptime(saved_date, '%Y-%m-%d') | |
else: | |
print('Last date not found.') | |
return | |
except ValueError: | |
print('WARNING: News file does not contain a valid date value!') | |
return | |
def main(): | |
news_file = os.path.join(expanduser("~"), '.archnews') | |
time_pattern = b'<p class="timestamp">(?P<timestamp>\d{4}-\d{2}-\d{2})<\/p>' | |
saved_date = check_date(news_file) | |
html = get_news_page() | |
dt = re.search(time_pattern,html) | |
last_site_date = datetime.datetime.strptime(dt.group(1).decode('utf-8'), '%Y-%m-%d') | |
if not saved_date or (saved_date and saved_date < last_site_date): | |
print('Check news at <http://www.archlinux.org>, please.') | |
with open(news_file, 'w') as f: | |
f.write(datetime.date.strftime(last_site_date, '%Y-%m-%d')) | |
else: | |
print('All right, you can safely update.') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment