article.py 1.3 KB
Newer Older
cynddl's avatar
cynddl committed
1
from django.db import models
cynddl's avatar
cynddl committed
2
3
from django.utils.translation import ugettext_lazy as _
from taggit.managers import TaggableManager
4
from .vote import UnDVotedMixin
luxcem's avatar
luxcem committed
5
6
from newspaper import Article as ArticleParser

cynddl's avatar
cynddl committed
7

8
class Article(UnDVotedMixin):
cynddl's avatar
cynddl committed
9
10
11
12
13
14
15
16
17
18
19
20
21
    url = models.URLField("URL")
    lang = models.CharField(_("Language"), max_length=50)
    metadata = models.TextField(_("Opengraph metadata"))
    screenshot = models.ImageField(_("Article screenshot"))
    title = models.CharField(_("Article title"), max_length=255)
    extracts = models.TextField(_("Content extracts"))

    published_at = models.DateTimeField(_("Publication date"))
    created_at = models.DateTimeField(_("Creation date"), auto_now_add=True)
    updated_at = models.DateTimeField(_("Last update"), auto_now=True)
    published = models.BooleanField(_("Published"))

    tags = TaggableManager()
22

23
24
    # TODO: adds links to user

25
26
27
    class Meta:
        verbose_name = _("Article")
        verbose_name_plural = _("Articles")
luxcem's avatar
luxcem committed
28
29
30

    def __str__(self):
        return self.title
dave's avatar
dave committed
31
32

    def parse(self):
luxcem's avatar
luxcem committed
33
        article = ArticleParser(url=self.url, language=self.lang)
dave's avatar
dave committed
34
35
36
37
38
39
        article.download()
        article.parse()
        self.title = article.title
        self.extracts = article.text
        # self.screenshot = getScreenshot(self.url)
        # TODO: save metadata