From 2c355a5ca75f6969edd5ef1eca132caf585daac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Gro=C3=9F?= Date: Wed, 7 Sep 2022 16:26:00 +0200 Subject: [PATCH] Import project --- .gitignore | 2 + assets/import/bands/.gitkeep | 0 assets/import/characters/.gitkeep | 0 example_band.json | 7 ++++ example_characters.json | 18 +++++++++ main.py | 67 +++++++++++++++++++++++++++++++ requirements.txt | 10 +++++ 7 files changed, 104 insertions(+) create mode 100644 .gitignore create mode 100644 assets/import/bands/.gitkeep create mode 100644 assets/import/characters/.gitkeep create mode 100644 example_band.json create mode 100644 example_characters.json create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e4b72e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv/ +assets/ \ No newline at end of file diff --git a/assets/import/bands/.gitkeep b/assets/import/bands/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/assets/import/characters/.gitkeep b/assets/import/characters/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/example_band.json b/example_band.json new file mode 100644 index 0000000..5d41cde --- /dev/null +++ b/example_band.json @@ -0,0 +1,7 @@ +{ + "id": "1", + "name": "BAND-MAID", + "description": "Band-Maid (stylized as BAND-MAID) is an all girl rock band from Tokyo that formed in July 2013. The band combines a rock sound with a maid image modeled on Japanese maid cafés.", + "imageURL": "https://cdn.discordapp.com/attachments/851543504831119380/1009467684490063892/unknown.png", + "enabled": 0 +} \ No newline at end of file diff --git a/example_characters.json b/example_characters.json new file mode 100644 index 0000000..5d9ce36 --- /dev/null +++ b/example_characters.json @@ -0,0 +1,18 @@ +[ + { + "id": "1", + "bandId": "1", + "name": "Miku Kobato", + "description": "Miku Kobato is a Japanese singer, songwriter and guitarist. She is the initial founding member and main lyricist for BAND-MAID.", + "imageIdentifier": "bandmaid/miku.png", + "enabled": 0 + }, + { + "id": "2", + "bandId": "1", + "name": "Akane Hirose", + "description": "Akane Hirose is a Japanese drummer and founding member of BAND-MAID.", + "imageIdentifier": "bandmaid/akane.png", + "enabled": 0 + } +] \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..4d39953 --- /dev/null +++ b/main.py @@ -0,0 +1,67 @@ +import discogs_client, json, re, urllib.request, os + +client = discogs_client.Client('ExampleApplication/0.1', user_token="") + +band_id = +member_id = +discogs_id = input("Enter Discogs ID: ") +band = client.artist(discogs_id) + +band_json = {} +band_json['name'] = band.name +band_json['id'] = band_id +band_json['description'] = band.profile +band_json['image'] = band.images[0]['uri'] +band_json['enabled'] = 0 + +members_json = [] + +def resolve_ref(match): + print(f"Resolving {match.group()} => ", end="") + ref = client.artist(int(match.group()[2:-1])) + print(f"{ref.name}") + return ref.name + +def sanitize_path(path): + return path.replace(" ", "_") + +def image_closest_to_aspect(images): + aspect = 0.6 + closest = None + for image in images: + if image['width'] / image['height'] > aspect and image['width'] < image['height']: + if closest is None or image['width'] < closest['width']: + closest = image + return closest if closest is not None else images[0] + +band_folder = sanitize_path(band.name) +if not os.path.exists(f"assets/{band_folder}"): + os.makedirs(f"assets/{band_folder}") + + +for member in band.members: + print(member.name) + profile = str(member.profile) + profile = re.sub(r'\[a[0-9|a-zA-Z]*\]', resolve_ref, profile) + profile = re.sub(r'\[a=[0-9|a-zA-Z ]*\]', lambda match: match.group()[3:-1], profile) + + print("Downloading image...") + image_path = f"{band_folder}/{member.id}_{sanitize_path(member.name)}.jpeg" + urllib.request.urlretrieve(image_closest_to_aspect(member.images)['uri'], f"assets/{image_path}") + + member = { + 'id': member_id, + 'bandId': band_id, + 'name': member.name, + 'description': profile, + 'imageIdentifier': image_path, + 'enabled': 0 + } + members_json.append(member) + member_id += 1 + +with open(f"aseets/import/characters/{band_folder}_characters.json", "w") as characters_file: + json.dump(members_json, characters_file, indent=4, sort_keys=False) + +with open(f"assets/import/bands/{band_folder}.json", "w") as band_file: + json.dump(band_json, band_file, indent=4, sort_keys=False) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2129e5f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,10 @@ +certifi==2022.6.15 +charset-normalizer==2.0.12 +idna==3.3 +oauthlib==3.2.0 +pkg-resources==0.0.0 +python-dateutil==2.8.2 +python3-discogs-client==2.3.15 +requests==2.27.1 +six==1.16.0 +urllib3==1.26.12