Skip to content
Open
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
94 changes: 93 additions & 1 deletion Ruddarr/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -3835,6 +3835,26 @@
}
}
},
"Popular Movies" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Popular Movies"
}
}
}
},
"Popular Series" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Popular Series"
}
}
}
},
"Popular This Week" : {
"localizations" : {
"en" : {
Expand Down Expand Up @@ -3907,6 +3927,10 @@
}
}
},
"Preview Unavailable" : {
"comment" : "A label describing a movie preview that is unavailable.",
"isCommentAutoGenerated" : true
},
"Previous Airing" : {
"localizations" : {
"en" : {
Expand Down Expand Up @@ -4500,6 +4524,46 @@
}
}
},
"See all popular movies" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "See all popular movies"
}
}
}
},
"See all popular series" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "See all popular series"
}
}
}
},
"See all upcoming movies" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "See all upcoming movies"
}
}
}
},
"See all upcoming series" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "See all upcoming series"
}
}
}
},
"Seeders" : {
"comment" : "Release filter",
"localizations" : {
Expand Down Expand Up @@ -5108,6 +5172,14 @@
}
}
},
"Unable to open this movie preview." : {
"comment" : "A message displayed when a movie preview cannot be opened.",
"isCommentAutoGenerated" : true
},
"Unable to open this series preview." : {
"comment" : "A description of the error that occurs when trying to open a series preview.",
"isCommentAutoGenerated" : true
},
"Unaired" : {
"comment" : "(Single word) Episode status label",
"localizations" : {
Expand Down Expand Up @@ -5260,6 +5332,26 @@
}
}
},
"Upcoming Movies" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Upcoming Movies"
}
}
}
},
"Upcoming Series" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Upcoming Series"
}
}
}
},
"Upgrade to Sonarr v4.0.5 or newer." : {
"localizations" : {
"en" : {
Expand Down Expand Up @@ -5541,5 +5633,5 @@
}
}
},
"version" : "1.0"
"version" : "1.1"
}
115 changes: 96 additions & 19 deletions Ruddarr/Services/Discovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,59 @@ import SwiftUI
@Observable
class Discovery {
static let shared = Discovery()
static let url: String = "https://api.ruddarr.com"
static let url: String = "http://192.168.40.73:8787"
static let railItemLimit = 6

private var movieItems: DiscoveryItems?
private var seriesItems: DiscoveryItems?
private var moviePopularItems: DiscoveryItems?
private var movieUpcomingItems: DiscoveryItems?
private var seriesPopularItems: DiscoveryItems?
private var seriesUpcomingItems: DiscoveryItems?

enum MediaType: String {
case movies
case series
}

var movies: [DiscoveryItem] {
guard let items = movieItems?.popular else { return [] }
guard Platform.deviceType == .phone else { return items }
return Array(items.prefix(24))
items(from: moviePopularItems)
}

var upcomingMovies: [DiscoveryItem] {
items(from: movieUpcomingItems)
}

var series: [DiscoveryItem] {
guard let items = seriesItems?.popular else { return [] }
items(from: seriesPopularItems)
}

var upcomingSeries: [DiscoveryItem] {
items(from: seriesUpcomingItems)
}

private func items(from response: DiscoveryItems?) -> [DiscoveryItem] {
guard let items = response?.popular else { return [] }
guard Platform.deviceType == .phone else { return items }
return Array(items.prefix(24))
}

func fetch(_ type: MediaType) async {
switch type {
case .movies:
if isCurrentWindow(movieItems?.timestamp) { return }
movieItems = await load(.movies)
if !isCurrentWindow(moviePopularItems?.timestamp) {
moviePopularItems = await load(.movies, .popular)
}

if !isCurrentWindow(movieUpcomingItems?.timestamp) {
movieUpcomingItems = await load(.movies, .upcoming)
}
case .series:
if isCurrentWindow(seriesItems?.timestamp) { return }
seriesItems = await load(.series)
if !isCurrentWindow(seriesPopularItems?.timestamp) {
seriesPopularItems = await load(.series, .popular)
}

if !isCurrentWindow(seriesUpcomingItems?.timestamp) {
seriesUpcomingItems = await load(.series, .upcoming)
}
}
}

Expand All @@ -49,17 +72,15 @@ class Discovery {
return calendar.isDateInToday(date)
}

private func load(_ type: MediaType) async -> DiscoveryItems? {
private func load(_ type: MediaType, _ section: DiscoverySection) async -> DiscoveryItems? {
// return PreviewData.loadObject(name: "popular-\(type.rawValue)")

guard let baseURL = URL(string: Discovery.url) else { return nil }

do {
let url = baseURL
.appending(path: "/discover/\(type.rawValue)")
.appending(queryItems: [
URLQueryItem(name: "language", value: Locale.current.identifier(.bcp47))
])
.appending(path: "/\(section.endpoint)/\(type.rawValue)")
.appending(queryItems: queryItems(for: section))

var request = URLRequest(url: url)
request.addValue("application/json", forHTTPHeaderField: "Accept")
Expand All @@ -69,23 +90,79 @@ class Discovery {
let statusCode = (response as? HTTPURLResponse)?.statusCode ?? 599

guard statusCode < 400 else {
leaveBreadcrumb(.error, category: "discovery", message: "Bad status code", data: ["status": statusCode])
leaveBreadcrumb(.error, category: "discovery", message: "Bad status code", data: [
"status": statusCode,
"endpoint": section.endpoint,
"type": type.rawValue,
])

return nil
}

return try JSONDecoder().decode(DiscoveryItems.self, from: json)
} catch {
leaveBreadcrumb(.error, category: "discovery", message: "Request failed", data: ["error": error])
leaveBreadcrumb(.error, category: "discovery", message: "Request failed", data: [
"error": error,
"endpoint": section.endpoint,
"type": type.rawValue,
])
}

return nil
}

private func queryItems(for section: DiscoverySection) -> [URLQueryItem] {
var items: [URLQueryItem] = []

let language = Locale.current.identifier(.bcp47)
if !language.isEmpty {
items.append(.init(name: "language", value: language))
}

if section == .upcoming {
let region = Locale.current.region?.identifier ?? "US"
items.append(.init(name: "region", value: region))
}

return items
}
}

struct DiscoveryItems: Codable, Equatable {
let timestamp: String
let popular: [DiscoveryItem]

enum CodingKeys: String, CodingKey {
case timestamp
case popular
case upcoming
}

init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
timestamp = try container.decode(String.self, forKey: .timestamp)
popular = try container.decodeIfPresent([DiscoveryItem].self, forKey: .popular)
?? container.decodeIfPresent([DiscoveryItem].self, forKey: .upcoming)
?? []
}

func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(timestamp, forKey: .timestamp)
try container.encode(popular, forKey: .popular)
}
}

enum DiscoverySection: String, Hashable {
case popular
case upcoming

var endpoint: String {
switch self {
case .popular: "discover"
case .upcoming: "upcoming"
}
}
}

struct DiscoveryItem: Identifiable, Codable, Equatable {
Expand All @@ -98,7 +175,7 @@ struct DiscoveryItem: Identifiable, Codable, Equatable {
let vote_average: Double
let vote_count: Int
let score: Double
let poster_path: String
let poster_path: String?

enum ItemType: String, Codable {
case movie
Expand Down
Loading
Loading