Created
April 4, 2022 16:09
-
-
Save pybites/721e6969438c232531b8f4fe0d6fa570 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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