ea3f8054d238169b5d0cb2216822d4ef11e33b0d
[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 def parse_name(name):
10     name_pieces = name.rsplit(' ', 1)
11     if len(name_pieces) == 1:
12         return name_pieces[0], ''
13     else:
14         return name_pieces
15
16
17
18 class Command(BaseCommand):
19     def add_arguments(self, parser):
20         parser.add_argument('path')
21
22     def handle(self, path, **kwargs):
23         with open(path) as f:
24             data = json.load(f)
25         for item in data:
26             if item['model'] == 'pdcounter.bookstub':
27                 notes = []
28                 slug = item['fields']['slug']
29                 book, created = Book.objects.get_or_create(slug=slug)
30                 if item['fields']['translator'] and not book.translators.exists():
31                     notes.append('tłum.: ' + item['fields']['translator'])
32                 book.title = book.title or item['fields']['title']
33                 book.pd_year = book.pd_year or item['fields']['pd']
34                 notes = '\n'.join(notes)
35                 if notes and notes not in book.notes:
36                     book.notes = '\n'.join([notes, book.notes])
37                 book.save()
38
39                 if not book.authors.exists():
40                     first_name, last_name = parse_name(item['fields']['author'])
41                     author, created = Author.objects.get_or_create(first_name=first_name, last_name=last_name)
42                     if not author.slug:
43                         author.slug = slugify(author_name)
44                         author.save()
45                     book.authors.set([author])
46             elif item['model'] == 'pdcounter.author':
47                 slug = item['fields']['slug']
48                 author, created = Author.objects.get_or_create(slug=slug)
49                 if not author.first_name and not author.last_name:
50                     author.first_name, author.last_name = parse_name(item['fields']['name'])
51                     author.year_of_death = author.year_of_death or item['fields']['death']
52                     author.notes = author.notes or item['fields']['description']
53                     author.gazeta_link = author.gazeta_link or item['fields']['gazeta_link']
54                     author.save()
55                     wiki_link = item['fields']['wiki_link']
56                     assert not wiki_link # Welp
57             else:
58                 print(item)
59                 break
60