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