comparison gpp/podcast/management/commands/import_old_podcasts.py @ 237:1085dc38399e

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