Skip to content

Commit

Permalink
Re-add API file without key, images
Browse files Browse the repository at this point in the history
  • Loading branch information
aheze committed Feb 28, 2021
1 parent a9a3be6 commit 2f0d837
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 6 deletions.
Binary file added Assets/AvailableGitHub.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/SearchView-Detail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/SearchView.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 4 additions & 6 deletions VidQuery.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
/* Begin PBXBuildFile section */
128336C625EAB39200EEAEBE /* ToolbarModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 128336C525EAB39200EEAEBE /* ToolbarModifier.swift */; };
128336CD25EAB3D000EEAEBE /* Containers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 128336CC25EAB3D000EEAEBE /* Containers.swift */; };
128336D025EAB42200EEAEBE /* TMDB_API.swift in Sources */ = {isa = PBXBuildFile; fileRef = 128336CF25EAB42200EEAEBE /* TMDB_API.swift */; };
128336D425EAB67100EEAEBE /* FeaturedCard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 128336D325EAB67100EEAEBE /* FeaturedCard.swift */; };
128336D725EAB69700EEAEBE /* CategoryCard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 128336D625EAB69700EEAEBE /* CategoryCard.swift */; };
128336DE25EAD43400EEAEBE /* TextFieldClearButtonModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 128336DD25EAD43400EEAEBE /* TextFieldClearButtonModifier.swift */; };
Expand All @@ -34,7 +33,6 @@
3C65DE8C25EB64A200D541DF /* SearchView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C3E3B3625EB00EA00C8ADA9 /* SearchView.swift */; };
3C65DE8E25EB64A200D541DF /* StarsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C3E3B3E25EB16B900C8ADA9 /* StarsView.swift */; };
3C65DE8F25EB64A200D541DF /* Containers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 128336CC25EAB3D000EEAEBE /* Containers.swift */; };
3C65DE9025EB64A200D541DF /* TMDB_API.swift in Sources */ = {isa = PBXBuildFile; fileRef = 128336CF25EAB42200EEAEBE /* TMDB_API.swift */; };
3C65DE9125EB64A200D541DF /* FeaturedCard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 128336D325EAB67100EEAEBE /* FeaturedCard.swift */; };
3C65DE9225EB64A200D541DF /* CategoryCard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 128336D625EAB69700EEAEBE /* CategoryCard.swift */; };
3C65DE9325EB64A200D541DF /* TextFieldClearButtonModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 128336DD25EAD43400EEAEBE /* TextFieldClearButtonModifier.swift */; };
Expand All @@ -46,12 +44,12 @@
3C914EEE25E9FBED00FAB882 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3C914EED25E9FBED00FAB882 /* Assets.xcassets */; };
3C914EF125E9FBED00FAB882 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3C914EF025E9FBED00FAB882 /* Preview Assets.xcassets */; };
3CA83C5B25EB774D002FD507 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3C914EED25E9FBED00FAB882 /* Assets.xcassets */; };
3CA83C6125EC138A002FD507 /* TMDB_API.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CA83C6025EC138A002FD507 /* TMDB_API.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
128336C525EAB39200EEAEBE /* ToolbarModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToolbarModifier.swift; sourceTree = "<group>"; };
128336CC25EAB3D000EEAEBE /* Containers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Containers.swift; sourceTree = "<group>"; };
128336CF25EAB42200EEAEBE /* TMDB_API.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TMDB_API.swift; sourceTree = "<group>"; };
128336D325EAB67100EEAEBE /* FeaturedCard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeaturedCard.swift; sourceTree = "<group>"; };
128336D625EAB69700EEAEBE /* CategoryCard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CategoryCard.swift; sourceTree = "<group>"; };
128336DD25EAD43400EEAEBE /* TextFieldClearButtonModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextFieldClearButtonModifier.swift; sourceTree = "<group>"; };
Expand All @@ -73,6 +71,7 @@
3C914EF025E9FBED00FAB882 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
3C914EF225E9FBED00FAB882 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3C914EF325E9FBED00FAB882 /* VidQuery.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = VidQuery.entitlements; sourceTree = "<group>"; };
3CA83C6025EC138A002FD507 /* TMDB_API.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TMDB_API.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -180,7 +179,7 @@
3C3E3B3625EB00EA00C8ADA9 /* SearchView.swift */,
3C3E3B3D25EB16B000C8ADA9 /* Details */,
128336CC25EAB3D000EEAEBE /* Containers.swift */,
128336CF25EAB42200EEAEBE /* TMDB_API.swift */,
3CA83C6025EC138A002FD507 /* TMDB_API.swift */,
128336DA25EAB6B900EEAEBE /* Cards */,
128336E025EAD43700EEAEBE /* Modifiers */,
3C914EED25E9FBED00FAB882 /* Assets.xcassets */,
Expand Down Expand Up @@ -320,7 +319,6 @@
3C65DE8F25EB64A200D541DF /* Containers.swift in Sources */,
128336FC25EB7F0200EEAEBE /* BackgroundColorModifier.swift in Sources */,
1283371425EB914300EEAEBE /* DetailView.swift in Sources */,
3C65DE9025EB64A200D541DF /* TMDB_API.swift in Sources */,
3C65DE9125EB64A200D541DF /* FeaturedCard.swift in Sources */,
3C65DE9225EB64A200D541DF /* CategoryCard.swift in Sources */,
3C65DE9325EB64A200D541DF /* TextFieldClearButtonModifier.swift in Sources */,
Expand All @@ -335,13 +333,13 @@
1283370425EB804D00EEAEBE /* AboutView.swift in Sources */,
3C3E3B3725EB00EA00C8ADA9 /* SearchView.swift in Sources */,
3C914EEC25E9FBEB00FAB882 /* ContentView.swift in Sources */,
128336D025EAB42200EEAEBE /* TMDB_API.swift in Sources */,
1283370025EB800D00EEAEBE /* AppDelegate.swift in Sources */,
128336F825EB7EFF00EEAEBE /* ForegroundColorModifier.swift in Sources */,
3C502BFA25EA062300D61772 /* DetailView.swift in Sources */,
128336DE25EAD43400EEAEBE /* TextFieldClearButtonModifier.swift in Sources */,
128336D725EAB69700EEAEBE /* CategoryCard.swift in Sources */,
3C914EEA25E9FBEB00FAB882 /* VidQueryApp.swift in Sources */,
3CA83C6125EC138A002FD507 /* TMDB_API.swift in Sources */,
3C65DE4125EB3E4D00D541DF /* GalleryView.swift in Sources */,
128336F225EB7D9600EEAEBE /* BackgroundColorModifier.swift in Sources */,
128336C625EAB39200EEAEBE /* ToolbarModifier.swift in Sources */,
Expand Down
148 changes: 148 additions & 0 deletions VidQuery/TMDB_API.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//
// TMDB_API.swift
// VidQuery
//
// Created by H. Kamran on 2/27/21.
//
import Foundation
import SwiftyJSON

public class TMDB_API {
private let readToken: String = "TMDB_READ_TOKEN"


let baseUrl = URL(string: "https://api.themoviedb.org")

public init() {}

public func search(query: String? = nil, genre: Genre? = nil, mediaType: MediaType, completionHandler: @escaping (([MediaResult]) -> Void)) {
var searchUrl: URL?

var genreSearch = false

if let genre = genre {
genreSearch = true
searchUrl = URL(string: "3/discover/movie?with_genres=\(genre.id)", relativeTo: baseUrl)!
} else if let query = query {
genreSearch = false
searchUrl = URL(string: "3/search/multi?query=\(query.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? query)", relativeTo: baseUrl)!
}

guard let searchUrlUnwrapped = searchUrl else { return }

performRequest(url: searchUrlUnwrapped) { data in
var results = [MediaResult]()

do {
let json = try JSON(data: data)

json["results"].forEach { _, value in
var result = MediaResult()
let genreIds = value["genre_ids"].arrayValue.map { $0.intValue }

if genreSearch == true {
result.type = .movies
result.movie = TMDBMovieResult(id: value["id"].intValue, title: value["title"].stringValue, originalTitle: value["original_title"].stringValue, overview: value["overview"].stringValue, adult: value["adult"].boolValue, posterPath: value["poster_path"].stringValue, releaseDate: value["release_date"].stringValue, genreIds: genreIds, popularity: value["popularity"].floatValue, voteCount: value["vote_count"].intValue, voteAverage: value["vote_average"].floatValue)
} else {
if value["media_type"] == "movie" {
let genreIds = value["genre_ids"].arrayValue.map { $0.intValue }

result.type = .movies
result.movie = TMDBMovieResult(id: value["id"].intValue, title: value["title"].stringValue, originalTitle: value["original_title"].stringValue, overview: value["overview"].stringValue, adult: value["adult"].boolValue, posterPath: value["poster_path"].stringValue, releaseDate: value["release_date"].stringValue, genreIds: genreIds, popularity: value["popularity"].floatValue, voteCount: value["vote_count"].intValue, voteAverage: value["vote_average"].floatValue)

} else if value["media_type"] == "tv" {
let genreIds = value["genre_ids"].arrayValue.map { $0.intValue }
let originCountry = value["origin_country"].arrayValue.map { $0.stringValue }

result.type = .tvShows
result.tv = TMDBTVResult(id: value["id"].intValue, name: value["name"].stringValue, originalName: value["original_name"].stringValue, overview: value["overview"].stringValue, posterPath: value["poster_path"].stringValue, firstAirDate: value["first_air_date"].stringValue, originCountry: originCountry, genreIds: genreIds, popularity: value["popularity"].floatValue, voteCount: value["vote_count"].intValue, voteAverage: value["vote_average"].floatValue)
}
}

results.append(result)
}

completionHandler(results)
} catch {
print("[JSON Error] \(error.localizedDescription)")
}
}
}

public func getGenres(completionHandler: @escaping (([Genre]) -> Void)) {
let genresUrl = URL(string: "3/genre/movie/list?language=en-US", relativeTo: baseUrl)!

performRequest(url: genresUrl) { data in
var genres: [Genre] = []

do {
let json = try JSON(data: data)
json["genres"].forEach { _, genre in
genres.append(Genre(id: genre["id"].intValue, name: genre["name"].stringValue))
}

completionHandler(genres)
} catch {
print("[JSON Error] \(error.localizedDescription)")
}
}
}

public func getByGenre() {}

public func getTrending(mediaType: MediaType, completionHandler: @escaping (([MediaResult]) -> Void)) {
let trendingUrl = URL(string: "3/trending/\(mediaType)/day", relativeTo: baseUrl)!

performRequest(url: trendingUrl) { data in
var trending: [MediaResult] = []

do {
let json = try JSON(data: data)

json["results"].forEach { _, value in
var result = MediaResult()
let genreIds = value["genre_ids"].arrayValue.map { $0.intValue }

if value["media_type"] == "movie" {
result.type = .movies
result.movie = TMDBMovieResult(id: value["id"].intValue, title: value["title"].stringValue, originalTitle: value["original_title"].stringValue, overview: value["overview"].stringValue, adult: value["adult"].boolValue, posterPath: value["poster_path"].stringValue, releaseDate: value["release_date"].stringValue, genreIds: genreIds, popularity: value["popularity"].floatValue, voteCount: value["vote_count"].intValue, voteAverage: value["vote_average"].floatValue)

} else if value["media_type"] == "tv" {
let originCountry = value["origin_country"].arrayValue.map { $0.stringValue }

result.type = .tvShows
result.tv = TMDBTVResult(id: value["id"].intValue, name: value["name"].stringValue, originalName: value["original_name"].stringValue, overview: value["overview"].stringValue, posterPath: value["poster_path"].stringValue, firstAirDate: value["first_air_date"].stringValue, originCountry: originCountry, genreIds: genreIds, popularity: value["popularity"].floatValue, voteCount: value["vote_count"].intValue, voteAverage: value["vote_average"].floatValue)
}

trending.append(result)
}

completionHandler(trending)
} catch {
print("[JSON Error] \(error.localizedDescription)")
}
}
}

private func performRequest(url: URL, completionHandler: @escaping (Data) -> Void) {
let session = URLSession.shared
var request = URLRequest(url: url)

request.setValue("Bearer \(readToken)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("utf-8", forHTTPHeaderField: "charset")

let task = session.dataTask(with: request) { data, _, error in
if error == nil {
if let data = data {
completionHandler(data)
}
} else {
// TODO: Add no read token error handling
print("Error: \(String(describing: error))")
}
}

task.resume()
}
}

0 comments on commit 2f0d837

Please sign in to comment.