From 0b689714d9580ad4a0e4f4399df70ea8d5448040 Mon Sep 17 00:00:00 2001 From: luxagraf Date: Thu, 10 Aug 2023 16:12:10 -0500 Subject: posts: added updating feature to automatically update post pub dates from wired RSS feed. --- app/posts/management/commands/rss_updater.py | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 app/posts/management/commands/rss_updater.py (limited to 'app/posts/management/commands/rss_updater.py') diff --git a/app/posts/management/commands/rss_updater.py b/app/posts/management/commands/rss_updater.py new file mode 100644 index 0000000..7ac26e6 --- /dev/null +++ b/app/posts/management/commands/rss_updater.py @@ -0,0 +1,42 @@ +from django.core.management.base import BaseCommand, CommandError + +import datetime +import feedparser +from urllib.parse import urlparse + +from posts.models import PostStatus, Post + +""" +run from a cronscript that looks line this: + +*/1 * * * * cd /home/lxf/sites/wired.luxagraf.net && source /home/lxf/sites/wired.luxagraf.net/venv/bin/activate && /home/lxf/sites/wired.luxagraf.net/venv/bin/python /home/lxf/sites/wired.luxagraf.net/manage.py rss_updater --settings=config.settings +""" + +class Command(BaseCommand): + help = "Update all published posts" + + def is_deal(tags): + for tag in tags: + if tag['term'] == "Deals": + return True + return False + + def handle(self, *args, **options): + feed = feedparser.parse("https://www.wired.com/feed/tag/commerce/latest/rss") + for item in feed.entries: + url = urlparse(item.link) + story_type = url.path.split('/')[1] + if story_type == "story" or "gallery": + if not self.is_deal(item.tags): + try: + post = Post.objects.get(url=item.link) + post.date_last_pub = datetime.datetime.strptime(item.published, '%a, %d %b %Y %H:%M:%S %z').date() + post.post_status = PostStatus.PUBLISHED + post.save() + self.stdout.write( + self.style.SUCCESS('Successfully updated post "%s"' % post.title) + ) + except: + continue + + -- cgit v1.2.3-70-g09d2