|
|
@ -3,80 +3,99 @@ from simpledatabase import SimpleDatabase |
|
|
|
import json |
|
|
|
from datetime import date, timedelta |
|
|
|
|
|
|
|
def load(): |
|
|
|
""" Load all feeds """ |
|
|
|
def update(): |
|
|
|
""" Update all feeds """ |
|
|
|
feeds = open('feeds.txt').readlines() |
|
|
|
db = SimpleDatabase('feeds.json', 'feeds.log') |
|
|
|
|
|
|
|
tmp = {} |
|
|
|
tmp['feeds'] = {} |
|
|
|
tmp['all_posts_sorted'] = {} |
|
|
|
|
|
|
|
for x, feed in enumerate(feeds): |
|
|
|
parsed = feedparser.parse(feed) |
|
|
|
x = str(x) |
|
|
|
tmp[x] = {} |
|
|
|
tmp[x]['title'] = parsed.feed.title |
|
|
|
tmp[x]['link'] = parsed.feed.link |
|
|
|
tmp[x]['description'] = parsed.feed.description |
|
|
|
tmp[x]['entries'] = parsed.entries |
|
|
|
|
|
|
|
print(parsed) |
|
|
|
|
|
|
|
tmp['feeds'][x] = {} |
|
|
|
tmp['feeds'][x]['title'] = parsed.feed.title |
|
|
|
tmp['feeds'][x]['link'] = parsed.feed.link |
|
|
|
tmp['feeds'][x]['description'] = parsed.feed.description |
|
|
|
|
|
|
|
for post in parsed.entries: |
|
|
|
year = post['published_parsed'][0] |
|
|
|
month = post['published_parsed'][1] |
|
|
|
day = post['published_parsed'][2] |
|
|
|
post_date = date(year, month, day) |
|
|
|
|
|
|
|
if not str(post_date) in tmp['all_posts_sorted']: |
|
|
|
tmp['all_posts_sorted'][str(post_date)] = [] |
|
|
|
|
|
|
|
post['feed_details'] = {} |
|
|
|
post['feed_details']['title'] = parsed.feed.title |
|
|
|
post['feed_details']['link'] = parsed.feed.link |
|
|
|
post['feed_details']['description'] = parsed.feed.description |
|
|
|
tmp['all_posts_sorted'][str(post_date)].append(post) |
|
|
|
|
|
|
|
db.update(tmp) |
|
|
|
return db |
|
|
|
|
|
|
|
def load(): |
|
|
|
db = SimpleDatabase('feeds.json', 'feeds.log') |
|
|
|
return db |
|
|
|
|
|
|
|
def latest(num): |
|
|
|
""" Placeholder request """ |
|
|
|
request = [ |
|
|
|
{ |
|
|
|
"feedtitle" : "Varia EN", |
|
|
|
"post": "hello world", |
|
|
|
"date" : "Monday 15th of February 2021", |
|
|
|
"url" : "https://vvvvvvaria.org/en/rr-wireless-imagination-1.html" |
|
|
|
} |
|
|
|
] |
|
|
|
""" Collect the <num> latest published posts """ |
|
|
|
db = load() |
|
|
|
|
|
|
|
dates = [key for key in db['all_posts_sorted'].keys()] |
|
|
|
dates.sort(reverse=True) |
|
|
|
request = [] |
|
|
|
|
|
|
|
for date in dates: |
|
|
|
posts = db['all_posts_sorted'][date] |
|
|
|
for post in posts: |
|
|
|
if len(request) < int(num): |
|
|
|
request.append(post) |
|
|
|
else: |
|
|
|
break |
|
|
|
|
|
|
|
return request |
|
|
|
|
|
|
|
def today(): |
|
|
|
""" Collect posts from today """ |
|
|
|
db = load() |
|
|
|
today = date.today() |
|
|
|
request = [] |
|
|
|
|
|
|
|
today = str(date.today()).split('-') |
|
|
|
today_year = "{:02d}".format(int(today[0])) |
|
|
|
today_month = "{:02d}".format(int(today[1])) |
|
|
|
today_day = "{:02d}".format(int(today[2])) |
|
|
|
print('TODAY =', today_year, today_month, today_day) |
|
|
|
for date_str, posts in db['all_posts_sorted'].items(): |
|
|
|
year = int(date_str.split('-')[0]) |
|
|
|
month = int(date_str.split('-')[1]) |
|
|
|
day = int(date_str.split('-')[2]) |
|
|
|
d = date(year, month, day) |
|
|
|
|
|
|
|
# Check if any posts are published today |
|
|
|
if d == today: |
|
|
|
for post in posts: |
|
|
|
request.append(post) |
|
|
|
|
|
|
|
request = [] |
|
|
|
for x, feed in db.items(): |
|
|
|
for post in feed['entries']: |
|
|
|
if post['published_parsed']: |
|
|
|
year = "{:02d}".format(post['published_parsed'][0]) |
|
|
|
month = "{:02d}".format(post['published_parsed'][1]) |
|
|
|
day = "{:02d}".format(post['published_parsed'][2] + 1) |
|
|
|
print('POST DATE =', year, month, day) |
|
|
|
|
|
|
|
# Check if this post is published today |
|
|
|
if year == today_year: |
|
|
|
if month == today_month: |
|
|
|
if day == today_day: |
|
|
|
request.append(post) |
|
|
|
return request |
|
|
|
|
|
|
|
def past(days): |
|
|
|
""" Collect posts from this week """ |
|
|
|
db = load() |
|
|
|
|
|
|
|
point_in_the_past = date.today() - timedelta(int(days)) |
|
|
|
print(f"{ days } days in the past =", point_in_the_past) |
|
|
|
|
|
|
|
request = [] |
|
|
|
for x, feed in db.items(): |
|
|
|
for post in feed['entries']: |
|
|
|
if post['published_parsed']: |
|
|
|
year = post['published_parsed'][0] |
|
|
|
month = post['published_parsed'][1] |
|
|
|
day = post['published_parsed'][2] |
|
|
|
|
|
|
|
post_date = date(year, month, day) |
|
|
|
print("post date =",post_date) |
|
|
|
|
|
|
|
if post_date > point_in_the_past: |
|
|
|
request.append(post) |
|
|
|
|
|
|
|
for date_str, posts in db['all_posts_sorted'].items(): |
|
|
|
year = int(date_str.split('-')[0]) |
|
|
|
month = int(date_str.split('-')[1]) |
|
|
|
day = int(date_str.split('-')[2]) |
|
|
|
d = date(year, month, day) |
|
|
|
|
|
|
|
if d > point_in_the_past: |
|
|
|
for post in posts: |
|
|
|
request.append(post) |
|
|
|
|
|
|
|
return request |
|
|
|
return request |
|
|
|