annotate gpp/podcast/management/commands/import_old_podcasts.py @ 244:a61b1c598001

Updated to markItUp 1.1.8. Ticket #94.
author Brian Neal <bgneal@gmail.com>
date Fri, 17 Sep 2010 02:27:40 +0000
parents 1085dc38399e
children
rev   line source
bgneal@237 1 """
bgneal@237 2 import_old_podcasts.py - For importing podcasts from SG101 1.0 as csv files.
bgneal@237 3 """
bgneal@237 4 import csv
bgneal@237 5 import datetime
bgneal@237 6
bgneal@237 7 from django.core.management.base import LabelCommand, CommandError
bgneal@237 8
bgneal@237 9 from podcast.models import Channel, Item
bgneal@237 10
bgneal@237 11
bgneal@237 12 class Command(LabelCommand):
bgneal@237 13 args = '<filename filename ...>'
bgneal@237 14 help = 'Imports podcasts from the old database in CSV format'
bgneal@237 15
bgneal@237 16 def handle_label(self, filename, **options):
bgneal@237 17 """
bgneal@237 18 Process each line in the CSV file given by filename by
bgneal@237 19 creating a new weblink object and saving it to the database.
bgneal@237 20
bgneal@237 21 """
bgneal@237 22 try:
bgneal@237 23 self.channel = Channel.objects.get(pk=1)
bgneal@237 24 except Channel.DoesNotExist:
bgneal@237 25 raise CommandError("Need a default channel with pk=1")
bgneal@237 26
bgneal@237 27 try:
bgneal@237 28 with open(filename, "rb") as f:
bgneal@237 29 self.reader = csv.DictReader(f)
bgneal@237 30 try:
bgneal@237 31 for row in self.reader:
bgneal@237 32 self.process_row(row)
bgneal@237 33 except csv.Error, e:
bgneal@237 34 raise CommandError("CSV error: %s %s %s" % (
bgneal@237 35 filename, self.reader.line_num, e))
bgneal@237 36
bgneal@237 37 except IOError:
bgneal@237 38 raise CommandError("Could not open file: %s" % filename)
bgneal@237 39
bgneal@237 40 def process_row(self, row):
bgneal@237 41 """
bgneal@237 42 Process one row from the CSV file: create an object for the row
bgneal@237 43 and save it in the database.
bgneal@237 44
bgneal@237 45 """
bgneal@237 46 item = Item(channel=self.channel,
bgneal@237 47 title=row['title'],
bgneal@237 48 author=row['author'],
bgneal@237 49 subtitle=row['subtitle'],
bgneal@237 50 summary=row['summary'],
bgneal@237 51 enclosure_url=row['enclosure_url'],
bgneal@237 52 alt_enclosure_url='',
bgneal@237 53 enclosure_length=int(row['enclosure_length']),
bgneal@237 54 enclosure_type=row['enclosure_type'],
bgneal@237 55 guid=row['guid'],
bgneal@237 56 pubdate=datetime.datetime.strptime(row['pubdate'],
bgneal@237 57 "%Y-%m-%d %H:%M:%S"),
bgneal@237 58 duration=row['duration'],
bgneal@237 59 keywords=row['keywords'],
bgneal@237 60 explicit=row['explicit'])
bgneal@237 61
bgneal@237 62 item.save()