Skip to content

Instantly share code, notes, and snippets.

@pybites
Created April 4, 2022 16:09
Show Gist options
  • Select an option

  • Save pybites/721e6969438c232531b8f4fe0d6fa570 to your computer and use it in GitHub Desktop.

Select an option

Save pybites/721e6969438c232531b8f4fe0d6fa570 to your computer and use it in GitHub Desktop.

Revisions

  1. pybites created this gist Apr 4, 2022.
    37 changes: 37 additions & 0 deletions article_importer.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    from django.core.management.base import BaseCommand
    import feedparser
    from django.db.utils import IntegrityError

    from blog.models import Article

    FEED_URL = "https://pybit.es/feed/"


    class Command(BaseCommand):
    help = 'Import Pybites article feed into the DB'

    def handle(self, *args, **options):
    feed = feedparser.parse(FEED_URL)
    import_count = 0
    for entry in feed.entries:
    try:
    Article.objects.create(
    title=entry.title,
    link=entry.links[0].href,
    summary=entry.summary)
    import_count += 1
    except IntegrityError:
    self.stderr.write(f"Article {entry.title} is already in DB")

    self.stdout.write(f"{import_count} articles imported")


    # where models.py has:
    #
    # from django.db import models
    #
    #
    # class Article(models.Model):
    # title = models.CharField(max_length=500, unique=True)
    # link = models.URLField(unique=True)
    # summary = models.TextField()