##// END OF EJS Templates
Source fetching: Use proper time stamps, log start and end timestamps
neko259 -
r1969:95da31b9 default
parent child Browse files
Show More
@@ -1,66 +1,67 b''
1 1 import feedparser
2 2 import logging
3 3
4 4 from time import mktime
5 5 from datetime import datetime
6 6
7 7 from django.db import models, transaction
8 8 from django.utils.dateparse import parse_datetime
9 9 from django.utils.timezone import utc
10 10 from django.utils import timezone
11 11 from boards.models import Post
12 12 from boards.models.post import TITLE_MAX_LENGTH
13 13
14 14
15 15 SOURCE_TYPE_MAX_LENGTH = 100
16 16 SOURCE_TYPE_RSS = 'RSS'
17 17 TYPE_CHOICES = (
18 18 (SOURCE_TYPE_RSS, SOURCE_TYPE_RSS),
19 19 )
20 20
21 21
22 22 class ThreadSource(models.Model):
23 23 class Meta:
24 24 app_label = 'boards'
25 25
26 26 name = models.TextField()
27 27 thread = models.ForeignKey('Thread')
28 28 timestamp = models.DateTimeField()
29 29 source = models.TextField()
30 30 source_type = models.CharField(max_length=SOURCE_TYPE_MAX_LENGTH,
31 31 choices=TYPE_CHOICES)
32 32
33 33 def __str__(self):
34 34 return self.name
35 35
36 36 @transaction.atomic
37 37 def fetch_latest_posts(self):
38 38 """Creates new posts with the info fetched since the timestamp."""
39 39 logger = logging.getLogger('boards.source')
40 40
41 41 if self.thread.is_archived():
42 42 logger.error('The thread {} is archived, please try another one'.format(self.thread))
43 43 else:
44 start_timestamp = timezone.localtime(self.timestamp)
44 start_timestamp = self.timestamp
45 45 last_timestamp = start_timestamp
46 logger.info('Start timestamp is {}'.format(start_timestamp))
46 47 if self.thread.is_bumplimit():
47 48 logger.warn('The thread {} has reached its bumplimit, please create a new one'.format(self.thread))
48 49 if self.source_type == SOURCE_TYPE_RSS:
49 50 feed = feedparser.parse(self.source)
50 51 items = sorted(feed.entries, key=lambda entry: entry.published_parsed)
51 52 for item in items:
52 53 title = item.title[:TITLE_MAX_LENGTH]
53 54 timestamp = datetime.fromtimestamp(mktime(item.published_parsed), tz=utc)
54 55 if not timestamp:
55 56 logger.error('Invalid timestamp {} for {}'.format(item.published, title))
56 57 else:
57 58 if timestamp > last_timestamp:
58 59 last_timestamp = timestamp
59
60 60 if timestamp > start_timestamp:
61 61 Post.objects.create_post(title=title, text=item.description, thread=self.thread, file_urls=[item.link])
62 62 logger.info('Fetched item {} from {} into thread {}'.format(
63 63 title, self.name, self.thread))
64 logger.info('New timestamp is {}'.format(last_timestamp))
64 65 self.timestamp = last_timestamp
65 66 self.save(update_fields=['timestamp'])
66 67
General Comments 0
You need to be logged in to leave comments. Login now