From c0364859bcd76733d67184dcfbe46c97a3f4fe53 Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Sun, 20 Nov 2022 15:50:35 -0500 Subject: [PATCH] Better Discord RPC --- PlayTools/DiscordActivity/DiscordIPC.swift | 84 +++++++++++++++++++--- 1 file changed, 75 insertions(+), 9 deletions(-) diff --git a/PlayTools/DiscordActivity/DiscordIPC.swift b/PlayTools/DiscordActivity/DiscordIPC.swift index a50b43c8..1f655703 100644 --- a/PlayTools/DiscordActivity/DiscordIPC.swift +++ b/PlayTools/DiscordActivity/DiscordIPC.swift @@ -20,13 +20,15 @@ class DiscordIPC { } else { ipc = SwordRPC(appId: custom.applicationID) } - let activity = createActivity(from: custom) - ipc.connect() - ipc.setPresence(activity) + Task.init(priority: .background) { + let activity = await createActivity(from: custom) + ipc.connect() + ipc.setPresence(activity) + } } } - func createActivity(from custom: DiscordActivity) -> RichPresence { + func createActivity(from custom: DiscordActivity) async -> RichPresence { var activity = RichPresence() if custom.details.isEmpty { @@ -48,10 +50,10 @@ class DiscordIPC { activity.assets.largeText = poweredStr } - let logo = "https://github.com/PlayCover/playcover-website/raw/master/src/assets/images/play-cover.png" + let logo = "https://raw.githubusercontent.com/PlayCover/PlaySite/master/src/assets/square-logo.png" if custom.image.isEmpty { let bundleID = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String ?? "" - if let appImage = loadImage(bundleID: bundleID) { + if let appImage = await loadImage(bundleID: bundleID) { activity.assets.largeImage = appImage activity.assets.largeText = nil activity.assets.smallImage = logo @@ -73,8 +75,72 @@ class DiscordIPC { return activity } - // TODO : load App icon image from appstore - func loadImage(bundleID: String) -> String? { - return nil + func loadImage(bundleID: String) async -> String? { + let lookup = "http://itunes.apple.com/lookup?bundleId=\(bundleID)" + guard let url = URL(string: lookup) else { return nil } + + do { + let (data, _) = try await URLSession.shared.data(for: URLRequest(url: url)) + let decoder = JSONDecoder() + let jsonResult: ITunesResponse = try decoder.decode(ITunesResponse.self, from: data) + if jsonResult.resultCount > 0 { + return jsonResult.results[0].artworkUrl512 + } else { + return nil + } + } catch { + return nil + } } } + +struct ITunesResult: Decodable { + var isGameCenterEnabled: Bool + var supportedDevices: [String] + var features: [String] + var advisories: [String] + var screenshotUrls: [String] + var ipadScreenshotUrls: [String] + var appletvScreenshotUrls: [String] + var artworkUrl60: String + var artworkUrl512: String + var artworkUrl100: String + var artistViewUrl: String + var kind: String + var isVppDeviceBasedLicensingEnabled: Bool + var currentVersionReleaseDate: String + var releaseNotes: String + var description: String + var trackId: Int + var trackName: String + var bundleId: String + var sellerName: String + var genreIds: [String] + var primaryGenreName: String + var primaryGenreId: Int + var currency: String + var formattedPrice: String + var contentAdvisoryRating: String + var averageUserRatingForCurrentVersion: Float + var userRatingCountForCurrentVersion: Int + var trackViewUrl: String + var trackContentRating: String + var averageUserRating: Float + var minimumOsVersion: String + var trackCensoredName: String + var languageCodesISO2A: [String] + var fileSizeBytes: String + var releaseDate: String + var artistId: Int + var artistName: String + var genres: [String] + var price: Float + var version: String + var wrapperType: String + var userRatingCount: Int +} + +struct ITunesResponse: Decodable { + var resultCount: Int + var results: [ITunesResult] +}