bgneal@294: """ bgneal@294: import_old_podcasts.py - For importing podcasts from SG101 1.0 as csv files. bgneal@294: """ bgneal@333: from __future__ import with_statement bgneal@294: import csv bgneal@294: import datetime bgneal@294: bgneal@294: from django.core.management.base import LabelCommand, CommandError bgneal@294: bgneal@294: from podcast.models import Channel, Item bgneal@294: bgneal@294: bgneal@294: class Command(LabelCommand): bgneal@294: args = '' bgneal@294: help = 'Imports podcasts from the old database in CSV format' bgneal@294: bgneal@294: def handle_label(self, filename, **options): bgneal@294: """ bgneal@294: Process each line in the CSV file given by filename by bgneal@294: creating a new weblink object and saving it to the database. bgneal@294: bgneal@294: """ bgneal@294: try: bgneal@294: self.channel = Channel.objects.get(pk=1) bgneal@294: except Channel.DoesNotExist: bgneal@294: raise CommandError("Need a default channel with pk=1") bgneal@294: bgneal@294: try: bgneal@294: with open(filename, "rb") as f: bgneal@294: self.reader = csv.DictReader(f) bgneal@294: try: bgneal@294: for row in self.reader: bgneal@294: self.process_row(row) bgneal@294: except csv.Error, e: bgneal@294: raise CommandError("CSV error: %s %s %s" % ( bgneal@294: filename, self.reader.line_num, e)) bgneal@294: bgneal@294: except IOError: bgneal@294: raise CommandError("Could not open file: %s" % filename) bgneal@294: bgneal@294: def process_row(self, row): bgneal@294: """ bgneal@294: Process one row from the CSV file: create an object for the row bgneal@294: and save it in the database. bgneal@294: bgneal@294: """ bgneal@294: item = Item(channel=self.channel, bgneal@294: title=row['title'], bgneal@294: author=row['author'], bgneal@294: subtitle=row['subtitle'], bgneal@294: summary=row['summary'], bgneal@294: enclosure_url=row['enclosure_url'], bgneal@294: alt_enclosure_url='', bgneal@294: enclosure_length=int(row['enclosure_length']), bgneal@294: enclosure_type=row['enclosure_type'], bgneal@294: guid=row['guid'], bgneal@294: pubdate=datetime.datetime.strptime(row['pubdate'], bgneal@294: "%Y-%m-%d %H:%M:%S"), bgneal@294: duration=row['duration'], bgneal@294: keywords=row['keywords'], bgneal@294: explicit=row['explicit']) bgneal@294: bgneal@294: item.save()