Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Retrogaming.cloud grid provider #470

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ice/gridproviders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
"consolegrid_provider",
"grid_image_provider",
"local_provider",
"retrogamingcloud_provider",
]
64 changes: 64 additions & 0 deletions ice/gridproviders/retrogamingcloud_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python
# encoding: utf-8
"""
retrogamingcloud_provider.py

Created by W on 2017-06-13.
Copyright (c) 2017 W Anders. All rights reserved.
"""

import sys
import os
import urllib
import json

import grid_image_provider

from ice.logs import logger

class RetroGamingCloudProvider(grid_image_provider.GridImageProvider):

@staticmethod
def is_enabled():
return True

@staticmethod
def api_url():
return "http://retrogaming.cloud/api/v1/game"

def download_image(self, url):
(path, headers) = urllib.urlretrieve(url)
return path

def rgc_get_result(self, url):
try:
response = urllib.urlopen(url)
return json.loads(response.read())["results"]
except IOError as error:
logger.debug(
"There was an error contacting Retrogaming.cloud"
)
return None

def rgc_search(self, rom):
api_root = self.api_url()
url = "%s?name=%s" % (api_root, rom.name)
return self.rgc_get_result(url)

def rgc_get_media(self, game_id):
api_root = self.api_url()
url = "%s/%s/media" % (api_root, game_id)
return self.rgc_get_result(url)

def image_for_rom(self, rom):
game_results = self.rgc_search(rom)
game_by_console = [game for game in game_results if game["platform"]["key"] == rom.console.shortname]
game_verify_by_name = next((game for game in game_by_console if game["name"] == rom.name), None)
if game_verify_by_name is None:
return None
game_id = game_verify_by_name["id"]
game_media = self.rgc_get_media(game_id)
game_grid_url = next((grid["game"]["most_popular_media_url"] for grid in game_media), None)
if game_grid_url is None:
return None
return self.download_image(game_grid_url)
2 changes: 2 additions & 0 deletions ice/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from gridproviders.combined_provider import CombinedProvider
from gridproviders.consolegrid_provider import ConsoleGridProvider
from gridproviders.local_provider import LocalProvider
from gridproviders.retrogamingcloud_provider import RetroGamingCloudProvider
from persistence.backed_object_manager import BackedObjectManager
from persistence.config_file_backing_store import ConfigFileBackingStore
from persistence.adapters.console_adapter import ConsoleBackedObjectAdapter
Expand Down Expand Up @@ -73,6 +74,7 @@ def image_provider(config):
providerByName = {
"local": LocalProvider,
"consolegrid": ConsoleGridProvider,
"retrogamingcloud": RetroGamingCloudProvider,
}
normalize = lambda s: s.strip().lower()
names = map(normalize, config.provider_spec.split(","))
Expand Down