Skip to content

Instantly share code, notes, and snippets.

@pybites
Created April 4, 2022 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pybites/721e6969438c232531b8f4fe0d6fa570 to your computer and use it in GitHub Desktop.
Save pybites/721e6969438c232531b8f4fe0d6fa570 to your computer and use it in GitHub Desktop.
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