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