summaryrefslogtreecommitdiff
path: root/app/gtd/management/commands/rss_updater.py
blob: 2e13fc9203a312db47b6d399e9ff1b3e928a52fe (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from django.core.management.base import BaseCommand, CommandError
from django.core.exceptions import ObjectDoesNotExist

import datetime
import feedparser
from urllib.parse import urlparse

from django.contrib.auth import get_user_model

from gtd.models import PostStatus, WiredPost
"""
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 = WiredPost.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()
                        post = WiredPost.objects.get(url=item.link)
                        #print(post.date_last_pub)
                        self.stdout.write(
                            self.style.SUCCESS('Successfully updated post "%s"' % post.title)
                        )
                    except ObjectDoesNotExist:
                        #print("skipping ", item.title)
                        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
                """
                dt = datetime.datetime.strptime(item.published, '%a, %d %b %Y %H:%M:%S %z').date()

                post = WiredPost.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)
'''