-
Notifications
You must be signed in to change notification settings - Fork 0
/
001-telex.40m.py
69 lines (58 loc) · 1.85 KB
/
001-telex.40m.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
import xml.etree.ElementTree as ET
import datetime
import time
MAX_ARTICLES=20
print("📮")
print("---")
print("ʇ legfontosabb | href=https://telex.hu/legfontosabb")
print("Refresh... | refresh=true")
print("---")
CACHE_FILE="not/telex.cache"
def fallback():
with open(CACHE_FILE, "r") as f:
stored_feed = f.read()
if(len(stored_feed) > 0):
print(stored_feed)
else:
print ("No cache...")
telexFeed = []
url = 'https://telex.hu/rss'
headers = {'accept': 'application/xml;q=0.9, */*;q=0.8'}
# The timeout value will be applied to both the connect and the read timeouts. Specify a tuple if you would like to set the values separately:
try:
response = requests.get(url, headers=headers, timeout=(3.05, 27))
telexXML = ET.fromstring(response.text)
except:
print("Couldn't parse response. 💀")
fallback()
exit(0)
count = 0
for x in telexXML.iter('item'):
if (count >= MAX_ARTICLES):
break
# Tue, 26 Apr 2022 08:14:10 +0200
pubDate = x.find('pubDate').text
feedTime = datetime.datetime.strptime(pubDate, '%a, %d %b %Y %H:%M:%S +%f').strftime('%a %H:%M')
# pubDate = "zolo"
title = x.find('title').text.replace('\n', ' ').replace('\r', '')
category = x.find('category').text.replace('\n', ' ').replace('\r', '')
description = x.find('description').text.replace('\n', ' ').replace('\r', '')
link = x.find('link').text
telexFeed.append("%s" % (f"{feedTime} ~ {category} :: {title}"))
telexFeed.append("%s" % (f"--{description} | href={link}"))
count = count + 1
content = '\n'.join(telexFeed)
print(content)
# print (response.text)
if not telexXML:
print("connection error")
fallback()
with open(CACHE_FILE, "w+") as f:
f.write(content)
f.write("\n---\n")
f.write("cached\n")
f.write(time.ctime())
f.close()