Skip to content

Instantly share code, notes, and snippets.

@pybites
Created April 4, 2022 16:09
Embed
What would you like to do?
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment