summaryrefslogtreecommitdiff
path: root/app/posts/management
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2023-08-10 16:12:10 -0500
committerluxagraf <sng@luxagraf.net>2023-08-10 16:12:10 -0500
commit0b689714d9580ad4a0e4f4399df70ea8d5448040 (patch)
treec3974d5f4877a401c7e38520bba33949e0b02c1d /app/posts/management
parent15f3969829705ee78ba20fb0fd5201e84112b7e9 (diff)
posts: added updating feature to automatically update post pub dates
from wired RSS feed.
Diffstat (limited to 'app/posts/management')
-rw-r--r--app/posts/management/__init__.py0
-rw-r--r--app/posts/management/commands/__init__.py0
-rw-r--r--app/posts/management/commands/rss_updater.py42
3 files changed, 42 insertions, 0 deletions
diff --git a/app/posts/management/__init__.py b/app/posts/management/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/app/posts/management/__init__.py
diff --git a/app/posts/management/commands/__init__.py b/app/posts/management/commands/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/app/posts/management/commands/__init__.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
+
+