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