Importing catalogue from pdcounter dump.
[redakcja.git] / src / catalogue / management / commands / import_catalogue_from_wl_dump.py
1 import json
2 import sys
3 from django.core.management import BaseCommand
4 from slugify import slugify
5 import wikidata
6 from catalogue.models import Book, Author
7
8
9 class Command(BaseCommand):
10     def add_arguments(self, parser):
11         parser.add_argument('path')
12
13     def handle(self, path, **kwargs):
14         with open(path) as f:
15             data = json.load(f)
16         for item in data:
17             if item['model'] == 'pdcounter.bookstub':
18                 continue
19                 notes = []
20                 slug = item['fields']['slug']
21                 book, created = Book.objects.get_or_create(slug=slug)
22                 if item['fields']['translator'] and not book.translators.exists():
23                     notes.append('tłum.: ' + item['fields']['translator'])
24                 book.title = book.title or item['fields']['title']
25                 book.pd_year = book.pd_year or item['fields']['pd']
26                 notes = '\n'.join(notes)
27                 if notes and notes not in book.notes:
28                     book.notes = '\n'.join([notes, book.notes])
29                 book.save()
30
31                 if not book.authors.exists():
32                     author_name = item['fields']['author']
33                     name_pieces = author_name.rsplit(' ', 1)
34                     if len(name_pieces) == 1:
35                         first_name, last_name = name_pieces, ''
36                     else:
37                         first_name, last_name = name_pieces
38
39                     author, created = Author.objects.get_or_create(first_name=first_name, last_name=last_name)
40                     if not author.slug:
41                         print(author.slug, author_name)
42                         author.slug = slugify(author_name)
43                         author.save()
44                     book.authors.set([author])
45             elif item['model'] == 'pdcounter.author':
46                 slug = item['fields']['slug']
47                 author, created = Author.objects.get_or_create(slug=slug)
48                 if not author.first_name and not author.last_name:
49                     author_name = item['fields']['name']
50                     name_pieces = author_name.rsplit(' ', 1)
51                     if len(name_pieces) == 1:
52                         author.first_name, author.last_name = name_pieces, ''
53                     else:
54                         author.first_name, author.last_name = name_pieces
55                     author.year_of_death = author.year_of_death or item['fields']['death']
56                     author.notes = author.notes or item['fields']['description']
57                     author.gazeta_link = author.gazeta_link or item['fields']['gazeta_link']
58                     wiki_link = item['fields']['wiki_link']
59                     assert not wiki_link # Welp
60             else:
61                 print(item)
62                 break
63