Пример #1
0
    def __init__(self, url, article_tag, title_tag, date_tag, content_tag, guid_tag=None, author_tag=None,
                 description_tag=None, category_tag=None, image_tag=None, video_tag=None):
        self.short_url = shorten_url(url)
        urls = open_file_to_list("../files/visited/%s" % self.short_url)
        for u in urls:
            # Set the class names are for the parser to recognise the content.
            # url of the article
            self.url = u
            self.article_tag = article_tag
            self.title_tag = title_tag
            self.author_tag = author_tag
            self.date_tag = date_tag
            self.guid_tag = guid_tag
            self.description_tag = description_tag
            self.content_tag = content_tag
            self.category_tag = category_tag
            self.image_tag = image_tag
            self.video_tag = video_tag

            # Values to send to the article
            self.title = ""
            self.author = ""
            self.date = None
            self.guid = ""
            self.description = ""
            self.content = ""
            self.category = ""
            self.images = []
            self.videos = []

            self.parse_article()
Пример #2
0
    def __init__(self, title, date, guid, content, url, description=None,
                 author=None, category=None, images=None, videos=None):
        # nieuws titel, string, required
        self.title = title
        # auteur, string
        self.author = author
        # publicatie datum, datetime, required
        self.date = date
        # unieke waarde (vaak de url), string, required
        self.guid = guid
        # korte nieuws samenvatting, string
        self.description = description
        # nieuws bericht, string
        self.content = content
        # link naar nieuwsbericht, string, required
        self.url = url
        short_url = shorten_url(self.url)
        # categorie van het nieuwsbericht, string
        self.category = category
        # urls naar afbeeldingen van het nieuwsbericht (jpg of png), array of strings
        self.images = images
        # urls naar videos van het nieuwsbericht, array of strings
        self.videos = videos

        try:
            # The directory the news items will be stored
            directory = "../files/news_items/%s" % short_url
            date = self.date.strftime("%d%m%Y")
            path = "%s/%s-%s" % (directory, self.title, date)
            # Create the directory if it does not exist.
            if not os.path.exists(directory):
                os.makedirs(directory)
            # Write the article data to a text file.
            with open(path, "w") as file:
                file.write("titel: %s\n\n" % self.title)
                file.write("auteur: %s\n\n" % self.author)
                file.write("datum: %s\n\n" % self.date)
                file.write("guid: %s\n\n" % self.guid)
                file.write("omschrijving:\n%s\n\n" % self.description)
                file.write("inhoud:\n%s\n\n" % self.content)
                file.write("url: %s\n\n" % self.url)
                if self.category:
                    file.write("categorie: %s\n\n" % self.category)
                if self.images:
                    file.write("afbeeldingen:\n")
                    for image in self.images:
                        file.write("%s\n" % image)
                if self.videos:
                    file.write("video's:\n")
                    for video in self.videos:
                        file.write("%s\n" % video)

            print "News item created"
        except:
            print "Error saving data to file!"