Quick and dirty hack to check for latest post in a feed
This is needed in case the feed is not in reverse chronological order, like most feeds. This needs testing still.
This commit is contained in:
parent
a1a6998e52
commit
3def57a933
1 changed files with 21 additions and 12 deletions
33
discorss.py
33
discorss.py
|
@ -100,16 +100,25 @@ def main():
|
||||||
for i, hook in enumerate(app_config["feeds"]):
|
for i, hook in enumerate(app_config["feeds"]):
|
||||||
# Get the feed
|
# Get the feed
|
||||||
print("Parsing feed {}...".format(hook["name"]))
|
print("Parsing feed {}...".format(hook["name"]))
|
||||||
feed = feedparser.parse(hook["url"])
|
feeds = feedparser.parse(hook["url"])
|
||||||
try:
|
latest_post = []
|
||||||
published_time = time.mktime(feed.entries[0]["published_parsed"])
|
prev_best = 0
|
||||||
published_time = published_time + hook["offset"]
|
for feed in feeds:
|
||||||
except KeyError:
|
try:
|
||||||
published_time = now - 10 # Not sure what a sensible default here is
|
published_time = time.mktime(feed["published_parsed"])
|
||||||
|
published_time = published_time + hook["offset"]
|
||||||
|
except KeyError:
|
||||||
|
published_time = feed["published"]
|
||||||
|
print(published_time)
|
||||||
|
sys.exit(254)
|
||||||
|
if published_time > prev_best:
|
||||||
|
latest_post = feed
|
||||||
|
prev_best = published_time
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
|
||||||
# Hash the title of the latest post and use that to determine if it's been posted
|
# Hash the title of the latest post and use that to determine if it's been posted
|
||||||
new_hash = hashlib.sha3_512(
|
new_hash = hashlib.sha3_512(bytes(latest_post["title"], "utf-8")).hexdigest()
|
||||||
bytes(feed.entries[0]["title"], "utf-8")
|
|
||||||
).hexdigest()
|
|
||||||
try:
|
try:
|
||||||
if hook["lasthash"] != new_hash:
|
if hook["lasthash"] != new_hash:
|
||||||
app_config["feeds"][i]["lasthash"] = new_hash
|
app_config["feeds"][i]["lasthash"] = new_hash
|
||||||
|
@ -121,8 +130,8 @@ def main():
|
||||||
webhook = {
|
webhook = {
|
||||||
"embeds": [
|
"embeds": [
|
||||||
{
|
{
|
||||||
"title": str(feed.entries[0]["title"]),
|
"title": str(latest_post["title"]),
|
||||||
"url": str(feed.entries[0]["link"]),
|
"url": str(latest_post["link"]),
|
||||||
"color": 216128,
|
"color": 216128,
|
||||||
"footer": {
|
"footer": {
|
||||||
"name": "DiscoRSS",
|
"name": "DiscoRSS",
|
||||||
|
@ -135,7 +144,7 @@ def main():
|
||||||
"fields": [
|
"fields": [
|
||||||
{
|
{
|
||||||
"name": "Excerpt from post:",
|
"name": "Excerpt from post:",
|
||||||
"value": get_description(feed),
|
"value": get_description(latest_post),
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue