Skip to content

Commit

Permalink
Disable no-longer-owned and game covers
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartones committed Sep 3, 2022
1 parent f2c000e commit 7f97ed5
Show file tree
Hide file tree
Showing 22 changed files with 11 additions and 315 deletions.
86 changes: 0 additions & 86 deletions finishedgames/catalogsources/adapters/giant_bomb_adapter.py
@@ -1,5 +1,4 @@
import json
import os
import time
from typing import Any, Callable, Dict, List, Optional, Tuple, cast # NOQA: F401

Expand All @@ -10,7 +9,6 @@
from django.conf import settings
from django.core.management.base import OutputWrapper
from django.core.management.color import Style
from PIL import Image

from finishedgames import constants

Expand Down Expand Up @@ -263,11 +261,6 @@ def _results_to_game_entities(self, results: Dict) -> List[Tuple[FetchedGame, Li

game = FetchedGame(**data)

if result["image"] and result["image"]["thumb_url"] and settings.USE_COVERS:
cover_filename = self._fetch_cover(result["image"]["thumb_url"], game.name_for_cover())
if cover_filename is not None:
game.cover = cover_filename

platforms = [] # type: List[FetchedPlatform]
for platform_result in result["platforms"]:
# NOTE: Only platforms not hidden will be linked
Expand All @@ -284,85 +277,6 @@ def _results_to_game_entities(self, results: Dict) -> List[Tuple[FetchedGame, Li

return entities

def _fetch_cover(self, url: str, filename: str) -> Optional[str]:
# Upon import, original files will be pre-prended with the source id to avoid collisions.
# However, once processed the name is generic and considers only the game name,
# so first source to add a cover "wins"

cover_path = os.path.join(settings.COVERS_IMPORT_PATH, filename + ".png")
# already have a cover, assume is ok and skip not even fetching
if os.path.exists(cover_path):
return filename

response = requests.get(url, headers={"user-agent": settings.CATALOG_SOURCES_ADAPTER_USER_AGENT}, stream=True)

if self._is_placeholder_cover(response):
return None

extension = url[url.rfind(".") :].lower()
if len(extension) not in [".png", ".jpg", ".jpeg"]:
# Their CDN doesn't contain image extension in the URL sometimes
if "image/jpeg" in response.headers["content-type"]:
extension = ".jpg"
elif "image/png" in response.headers["content-type"]:
extension = ".png"
else:
return None

original_path = os.path.join(
settings.COVERS_IMPORT_PATH,
"{prefix}_{name}{extension}".format(
prefix=GiantBombAdapter.source_id(), name=filename, extension=extension
),
)

if response.status_code == 200:
return self._resize_cover(response, filename, original_path, cover_path)

return None

def _is_placeholder_cover(self, response: requests.Response) -> bool:
return response.headers.get("etag", "") in [
"57c944b54644e25ec5695a8a50a44d00",
'"57c944b54644e25ec5695a8a50a44d00"',
]

def _resize_cover(
self, response: requests.Response, filename: str, original_path: str, destination_path: str
) -> Optional[str]:
try:
with open(original_path, "wb") as file_handle:
for content_chunk in response.iter_content(1024):
file_handle.write(content_chunk)

# TODO: could read from memory? and avoid lots of IO
image = Image.open(original_path)

source_width, source_height = image.size
if source_width // source_height > 1:
new_width = settings.COVER_IMAGE_WIDTH
ratio = settings.COVER_IMAGE_WIDTH * 100 // source_width
new_height = source_height * ratio // 100
else:
new_height = settings.COVER_IMAGE_HEIGHT
ratio = settings.COVER_IMAGE_HEIGHT * 100 // source_height
new_width = source_width * ratio // 100
output_image = image.resize((new_width, new_height))
# fixes error 'cannot write mode CMYK as PNG'
output_image.convert("RGB").save(destination_path, "PNG", optimize=True)
os.unlink(original_path)

self.stdout.write(self.stdout_style.SUCCESS(destination_path))
return filename
except Exception as error:
self.stdout.write(
self.stdout_style.WARNING(
"Error {} downloading & readying cover from {}".format(error, response.request.url)
)
)

return None

def _get_platform_cached(self, source_platform_id: int) -> Optional[FetchedPlatform]:
if source_platform_id not in self.platforms_cache:
try:
Expand Down
9 changes: 0 additions & 9 deletions finishedgames/catalogsources/admin/forms.py
Expand Up @@ -71,7 +71,6 @@ class SingleFetchedGameImportForm(forms.Form):
fg_platform_ids = forms.CharField(label="Fetched Platform Ids", disabled=True)
fg_platforms = forms.CharField(label="Fetched Platforms", disabled=True)
fetched_dlc_or_expansion = forms.BooleanField(label="DLC/Expansion", disabled=True)
fetched_cover = forms.CharField(label="Cover filename", disabled=True)
source_id = forms.CharField(label="Source identifier", max_length=50, disabled=True)
source_game_id = forms.CharField(label="Source game identifier", max_length=50, disabled=True)
source_url = forms.CharField(label="Resource source URI", max_length=255, disabled=True)
Expand All @@ -97,13 +96,6 @@ class SingleGameImportForm(forms.Form):
)
platforms = forms.ModelMultipleChoiceField(queryset=Platform.objects.order_by(Lower("name")))
dlc_or_expansion = forms.BooleanField(label="DLC/Expansion", initial=False, required=False)
cover = forms.CharField(
label="Cover filename",
max_length=100,
initial="",
required=False,
widget=forms.TextInput(attrs={"size": "60", "class": "vTextField"}),
)
parent_game = forms.ModelChoiceField(
label="Parent game",
queryset=Game.objects.order_by(Lower("name")),
Expand All @@ -129,7 +121,6 @@ class GamesImportForm(forms.Form):
names = SimpleArrayField(forms.CharField(max_length=200))
publish_date_strings = SimpleArrayField(forms.CharField(max_length=4))
dlcs_or_expansions = SimpleArrayField(forms.BooleanField(required=False))
covers = SimpleArrayField(forms.CharField(max_length=100, required=False))
platforms_lists = SimpleArrayField(forms.CharField(max_length=500))
parent_game_ids = SimpleArrayField(forms.IntegerField())
source_display_names = SimpleArrayField(forms.CharField(max_length=255))
Expand Down
2 changes: 0 additions & 2 deletions finishedgames/catalogsources/admin/models.py
Expand Up @@ -52,7 +52,6 @@ class FetchedGameAdmin(FetchedGameAdminViewsMixin, FGModelAdmin):
]
search_fields = ["name"]
readonly_fields = [
"cover",
"last_modified_date",
"change_hash",
]
Expand Down Expand Up @@ -81,7 +80,6 @@ class FetchedGameAdmin(FetchedGameAdminViewsMixin, FGModelAdmin):
"dlc_or_expansion",
"parent_game",
"hidden",
"cover",
"fg_game",
"source_game_id",
("source_id", "source_url"),
Expand Down
5 changes: 0 additions & 5 deletions finishedgames/catalogsources/admin/views.py
Expand Up @@ -106,7 +106,6 @@ def import_setup_view(self, request: HttpRequest) -> TemplateResponse:
"fg_platform_ids": fg_platform_ids,
"fg_platforms": "",
"fetched_dlc_or_expansion": fetched_game.dlc_or_expansion,
"fetched_cover": fetched_game.cover,
"source_id": fetched_game.source_id,
"source_game_id": fetched_game.source_game_id,
"source_url": fetched_game.source_url,
Expand Down Expand Up @@ -141,7 +140,6 @@ def import_setup_view(self, request: HttpRequest) -> TemplateResponse:
"publish_date": fetched_game.fg_game.publish_date,
"platforms": platforms_list,
"dlc_or_expansion": fetched_game.fg_game.dlc_or_expansion,
"cover": fetched_game.fg_game.cover,
}
)

Expand Down Expand Up @@ -242,7 +240,6 @@ def import_view(self, request: HttpRequest) -> HttpResponseRedirect:
name=name,
publish_date_string=game_form.cleaned_data["publish_date"],
dlc_or_expansion=game_form.cleaned_data["dlc_or_expansion"],
cover=game_form.cleaned_data["cover"],
platforms=game_form.cleaned_data["platforms"],
fetched_game_id=game_form.cleaned_data["fetched_game_id"],
game_id=game_form.cleaned_data["game_id"],
Expand Down Expand Up @@ -273,7 +270,6 @@ def import_batch_view(self, request: HttpRequest) -> HttpResponseRedirect:
"dlcs_or_expansions": [
(True if dlc == "true" else False) for dlc in request.POST.getlist("dlcs_or_expansions")
],
"covers": request.POST.getlist("covers"),
"platforms_lists": request.POST.getlist("platforms_lists"),
"parent_game_ids": request.POST.getlist("parent_game_ids"),
"source_display_names": request.POST.getlist("source_display_names"),
Expand Down Expand Up @@ -306,7 +302,6 @@ def import_batch_view(self, request: HttpRequest) -> HttpResponseRedirect:
name=name,
publish_date_string=games_form.cleaned_data["publish_date_strings"][index],
dlc_or_expansion=games_form.cleaned_data["dlcs_or_expansions"][index],
cover=games_form.cleaned_data["covers"][index],
platforms=platforms,
fetched_game_id=games_form.cleaned_data["fetched_game_ids"][index],
game_id=game_id,
Expand Down
Expand Up @@ -99,8 +99,6 @@ def _upsert_results(self, results: List[Tuple[FetchedGame, List[FetchedPlatform]
try:
existing_game = FetchedGame.objects.get(source_game_id=game.source_game_id, source_id=game.source_id)
existing_game.name = game.name
if not existing_game.cover and game.cover is not None:
existing_game.cover = game.cover
existing_game.source_game_id = game.source_game_id
existing_game.source_id = game.source_id
existing_game.source_url = game.source_url
Expand Down
Expand Up @@ -60,7 +60,6 @@ def import_games(self, max_items: int, exclude_unreleased: bool) -> None:
dlc_or_expansion=fetched_game.dlc_or_expansion,
platforms=available_platforms,
fetched_game_id=fetched_game.id,
cover=fetched_game.cover,
game_id=None,
parent_game_id=None,
source_display_name=source_display_names[fetched_game.source_id],
Expand Down
Expand Up @@ -7,7 +7,7 @@


class Command(BaseCommand):
help = "Syncs already imported, not hidden, Fetched Games cover"
help = "Syncs already imported, not hidden, Fetched Games"

def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument("max_items", type=int)
Expand Down
21 changes: 1 addition & 20 deletions finishedgames/catalogsources/managers.py
@@ -1,6 +1,4 @@
import os
import re
from shutil import copyfile
from typing import List, Optional, Tuple, cast

from catalogsources.helpers import clean_string_field
Expand Down Expand Up @@ -31,7 +29,6 @@ def import_fetched_game(
name: Optional[str] = None,
publish_date_string: Optional[str] = None,
dlc_or_expansion: Optional[bool] = None,
cover: Optional[str] = None,
game_id: Optional[int] = None,
parent_game_id: Optional[int] = None,
source_display_name: Optional[str] = None,
Expand Down Expand Up @@ -63,17 +60,6 @@ def import_fetched_game(
game.parent_game_id = parent_game_id
else:
game.parent_game = None
if include_all_fields or "cover" in cast(List[str], update_fields_filter):
# do not error if 'cover' empty (source import might not have it)
# Remark: only sets cover if not had one, never updates it
if game.cover is None and cover:
try:
source_path = os.path.join(settings.COVERS_IMPORT_PATH, cover + ".png")
destination_path = os.path.join(settings.COVERS_PATH, cover + ".png")
copyfile(source_path, destination_path)
except Exception as error:
raise GameImportSaveError(str(error))
game.cover = cover

# update always the url for this source
if source_display_name and source_url:
Expand Down Expand Up @@ -164,7 +150,6 @@ def import_fetched_games_fixing_duplicates_appending_publish_date(cls, fetched_g
name=fixed_name,
publish_date_string=str(fetched_game.publish_date),
dlc_or_expansion=fetched_game.dlc_or_expansion,
cover=fetched_game.cover,
platforms=available_platform_ids,
fetched_game_id=fetched_game_id,
# TODO: include parent_game
Expand Down Expand Up @@ -209,7 +194,6 @@ def import_fetched_games_fixing_duplicates_appending_platform(cls, fetched_game_
name=fixed_name,
publish_date_string=str(fetched_game.publish_date),
dlc_or_expansion=fetched_game.dlc_or_expansion,
cover=fetched_game.cover,
platforms=available_platform_ids,
fetched_game_id=fetched_game_id,
# TODO: include parent_game
Expand Down Expand Up @@ -256,7 +240,6 @@ def import_fetched_games_linking_if_name_and_year_matches(cls, fetched_game_ids:
name=fetched_game.name,
publish_date_string=str(fetched_game.publish_date),
dlc_or_expansion=fetched_game.dlc_or_expansion,
cover=fetched_game.cover,
platforms=available_platform_ids,
game_id=game_id,
fetched_game_id=fetched_game_id,
Expand Down Expand Up @@ -300,12 +283,11 @@ def sync_fetched_games(cls, fetched_game_ids: List[int], force_sync: bool = Fals
cls.import_fetched_game(
publish_date_string=publish_date,
platforms=available_platforms,
cover=fetched_game.cover,
game_id=fetched_game.fg_game.id,
fetched_game_id=fetched_game_id,
source_display_name=source_display_name,
source_url=fetched_game.source_url,
update_fields_filter=["publish_date", "platforms", "cover"],
update_fields_filter=["publish_date", "platforms"],
)
count_synced += 1

Expand All @@ -327,7 +309,6 @@ def _attempt_import(
name=fetched_game.name,
publish_date_string=str(fetched_game.publish_date),
dlc_or_expansion=fetched_game.dlc_or_expansion,
cover=fetched_game.cover,
platforms=available_platform_ids,
fetched_game_id=fetched_game_id,
# TODO: include parent_game
Expand Down
2 changes: 1 addition & 1 deletion finishedgames/catalogsources/models.py
Expand Up @@ -80,7 +80,7 @@ def _get_fields_for_hash(self) -> str:
fg_game_id=self.fg_game if self.fg_game else "",
source_game_id=self.source_game_id,
source_url=self.source_url,
cover=self.cover if self.cover else "",
cover="",
)

def __str__(self) -> str:
Expand Down
3 changes: 0 additions & 3 deletions finishedgames/catalogsources/templates/game_import_batch.html
Expand Up @@ -39,7 +39,6 @@ <h3>Choose fields to import:</h3>
<input type="checkbox" name="fields" value="publish_date" checked>Publish Date<br />
<input type="checkbox" name="fields" value="platforms" checked>Platforms<br />
<input type="checkbox" name="fields" value="dlc_or_expansion">DLC/Expansion<br />
<input type="checkbox" name="fields" value="cover">Cover<br />
<input type="checkbox" name="fields" value="parent_game">Parent Game<br />
</p>

Expand Down Expand Up @@ -81,8 +80,6 @@ <h3>Items that will be imported:</h3>
value="{{ fetched_game.publish_date }}">
<input type="hidden" name="{{games_form.dlcs_or_expansions.html_name}}"
value="{% if fetched_game.dlc_or_expansion %}true{% else %}false{% endif %}">
<input type="hidden" name="{{games_form.covers.html_name}}"
value="{{ fetched_game.cover }}">
<input type="hidden" name="{{games_form.platforms_lists.html_name}}"
value="{{ platforms }}">
{% if fetched_game.parent_game and fetched_game.parent_game.fg_game %}
Expand Down
Expand Up @@ -136,21 +136,6 @@ <h3>Source Fetched Game <input onclick="copyAllFields(this, 'imported-game')" ty
</div>
</div>
</div>
<div class="form-row">
<div>
{{ fetched_game_form.fetched_cover.label_tag }}
<div class="with-copy-btn">
<div class="readonly" id="id_{{ fetched_game_form.fetched_cover.html_name }}">
<span>{{fetched_game_form.fetched_cover.value}}</span>
</div>
<input id="{{ fetched_game_form.fetched_cover.html_name }}"
data-source="id_{{ fetched_game_form.fetched_cover.html_name }}"
data-destination="id_{{ game_form.cover.html_name }}"
onclick="copyField(this)" type="button" value="" class="btn-operation"
title="Copy value">
</div>
</div>
</div>
<div class="form-row">
<label for="parent_game">
{{ fetched_game_form.fetched_parent_game_name.label }}:<br/><br/>
Expand Down Expand Up @@ -286,15 +271,6 @@ <h3>
type="button" value="" class="btn-operation" title="Reset to original value">
</div>
</div>
<div class="form-row">
<div class="checkbox-row">
{{ game_form.cover.label_tag }}
{{ game_form.cover }}
<input data-target="id_{{ game_form.cover.html_name }}"
data-original="{{ game_form.cover.value }}" onclick="revertField(this)"
type="button" value="" class="btn-operation" title="Reset to original value">
</div>
</div>
<div class="form-row">
<div>
{{ game_form.parent_game.label_tag }}
Expand Down

0 comments on commit 7f97ed5

Please sign in to comment.