summaryrefslogtreecommitdiff
path: root/app/posts/management/commands/rss_updater.py
blob: ad8551bddd4deb55a3afd7ef8199f74ce6dce5a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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(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