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"]): | ||||
|         # Get the feed | ||||
|         print("Parsing feed {}...".format(hook["name"])) | ||||
|         feed = feedparser.parse(hook["url"]) | ||||
|         try: | ||||
|             published_time = time.mktime(feed.entries[0]["published_parsed"]) | ||||
|             published_time = published_time + hook["offset"] | ||||
|         except KeyError: | ||||
|             published_time = now - 10  # Not sure what a sensible default here is | ||||
|         feeds = feedparser.parse(hook["url"]) | ||||
|         latest_post = [] | ||||
|         prev_best = 0 | ||||
|         for feed in feeds: | ||||
|             try: | ||||
|                 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 | ||||
|         new_hash = hashlib.sha3_512( | ||||
|             bytes(feed.entries[0]["title"], "utf-8") | ||||
|         ).hexdigest() | ||||
|         new_hash = hashlib.sha3_512(bytes(latest_post["title"], "utf-8")).hexdigest() | ||||
|         try: | ||||
|             if hook["lasthash"] != new_hash: | ||||
|                 app_config["feeds"][i]["lasthash"] = new_hash | ||||
|  | @ -121,8 +130,8 @@ def main(): | |||
|         webhook = { | ||||
|             "embeds": [ | ||||
|                 { | ||||
|                     "title": str(feed.entries[0]["title"]), | ||||
|                     "url": str(feed.entries[0]["link"]), | ||||
|                     "title": str(latest_post["title"]), | ||||
|                     "url": str(latest_post["link"]), | ||||
|                     "color": 216128, | ||||
|                     "footer": { | ||||
|                         "name": "DiscoRSS", | ||||
|  | @ -135,7 +144,7 @@ def main(): | |||
|                     "fields": [ | ||||
|                         { | ||||
|                             "name": "Excerpt from post:", | ||||
|                             "value": get_description(feed), | ||||
|                             "value": get_description(latest_post), | ||||
|                         } | ||||
|                     ], | ||||
|                 } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue