from django.core.management.base import BaseCommand, CommandError import datetime import feedparser from urllib.parse import urlparse from django.contrib.auth import get_user_model from posts.models import PostStatus, Post User = get_user_model() """ 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(self, 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 if story_type == "review": """ TODO: add first and last names so I have everyone in the DB Then parse and add reviews, but change views so we don't see them in views where we just want a list of guides to update """ user = User.objects.get(firstname=item.author.split(" ")[0],lastname=item.author.split(" ")[1]) dt = datetime.datetime.strptime(item.published, '%a, %d %b %Y %H:%M:%S %z').date() post = Post.objects.get(url=item.link) user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL) title = models.CharField(max_length=512, blank=True, null=True) body = models.TextField(blank=True, null=True) url = models.CharField(max_length=512, blank=True, null=True) date_last_pub = models.DateField() guid = models.CharField(max_length=512, blank=True, null=True, db_index=True) author = models.CharField(max_length=255, blank=True, null=True) post_type = models.IntegerField(choices=PostType.choices, default=PostType.GUIDE) template_type = models.IntegerField(choices=TemplateType.choices, default=TemplateType.STORY) update_frequency = models.BigIntegerField(help_text="In days") products = models.ManyToManyField(ProductLink, blank=True, null=True) needs_update = models.BooleanField(default=False) is_live = models.BooleanField(default=True) post_status = models.IntegerField(choices=PostStatus.choices, default=PostStatus.PUBLISHED)