Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #343 from blackcandy-org/discogs-sync-image
Automatically fetch cover art from discogs after sync
- Loading branch information
Showing
47 changed files
with
640 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Controller } from '@hotwired/stimulus' | ||
import { installEventHandler } from './mixins/event_handler' | ||
|
||
export default class extends Controller { | ||
static values = { | ||
defaultUrl: String | ||
} | ||
|
||
initialize () { | ||
installEventHandler(this) | ||
} | ||
|
||
connect () { | ||
if (!this.hasDefaultUrlValue) { return } | ||
|
||
this.handleEvent('error', { | ||
on: this.element, | ||
with: this.loadDefault | ||
}) | ||
} | ||
|
||
loadDefault = () => { | ||
this.element.src = this.defaultUrlValue | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
class Integrations::Discogs < Integrations::Service | ||
base_uri "https://api.discogs.com" | ||
|
||
def initialize(token = Setting.discogs_token) | ||
self.class.headers "Authorization" => "Discogs token=#{token}" | ||
end | ||
|
||
def cover_image(object) | ||
case object | ||
when Artist | ||
artist_cover_image(object) | ||
when Album | ||
album_cover_image(object) | ||
end | ||
end | ||
|
||
private | ||
|
||
def search_cover_image(options) | ||
json = request("/database/search", options) | ||
image_url = json&.dig(:results, 0, :cover_image) | ||
return unless image_url.present? | ||
|
||
download_image(image_url) | ||
end | ||
|
||
def artist_cover_image(artist) | ||
options = {query: {type: "artist", q: artist.name}, format: :plain} | ||
search_cover_image(options) | ||
end | ||
|
||
def album_cover_image(album) | ||
options = {query: {type: "master", release_title: album.name, artist: album.artist.name}, format: :plain} | ||
search_cover_image(options) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
require "open-uri" | ||
|
||
class Integrations::Service | ||
include HTTParty | ||
|
||
def request(path, options = {}, method = :get) | ||
response = self.class.send(method, path, options) | ||
raise TooManyRequests if response.code.to_i == 429 | ||
|
||
parse_json(response) | ||
end | ||
|
||
def download_image(url) | ||
image_file = OpenURI.open_uri(url) | ||
content_type = image_file.content_type | ||
image_format = Mime::Type.lookup(content_type).symbol | ||
return unless image_format.present? | ||
|
||
{ | ||
io: image_file, | ||
filename: "cover.#{image_format}", | ||
content_type: content_type | ||
} | ||
rescue OpenURI::HTTPError => e | ||
raise TooManyRequests if e.io.status[0].to_i == 429 | ||
end | ||
|
||
private | ||
|
||
def parse_json(response) | ||
JSON.parse response.body, symbolize_names: true if response.success? | ||
end | ||
|
||
class TooManyRequests < StandardError; end | ||
end |
Oops, something went wrong.