Skip to content

Commit

Permalink
removed garbage api request code
Browse files Browse the repository at this point in the history
  • Loading branch information
cranci1 committed May 3, 2024
1 parent 6fedfb4 commit 94c36d7
Show file tree
Hide file tree
Showing 16 changed files with 197 additions and 534 deletions.
Binary file not shown.
1 change: 0 additions & 1 deletion AnimeGen/APIs/Hmtai/HmtaiSender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by cranci on 03/03/24.
//

import Foundation
import UIKit

extension ViewController {
Expand Down
99 changes: 31 additions & 68 deletions AnimeGen/APIs/kyoko.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,66 @@ extension ViewController {
let apiEndpoint = "\(endpointPrefix)\(randomCategory)"

guard let url = URL(string: apiEndpoint) else {
showAlert(withTitle: "Invalid URL", message: "Please wait, the api may be down.", viewController: self)
print("Invalid URL")
stopLoadingIndicator()
return
}

let task = URLSession.shared.dataTask(with: url) { [weak self] (data, response, error) in
guard let self = self else { return }
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
DispatchQueue.main.async {
if let error = error {
self.showAlert(withTitle: "Error!", message: "\(error)", viewController: self)
print("Error: \(error)")
self.stopLoadingIndicator()
return
}

guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
self.showAlert(withTitle: "Invalid HTTP response", message: "Please wait, the api may be down.", viewController: self)
print("Invalid HTTP response")
self.stopLoadingIndicator()
return
}

if let data = data {
do {
if let jsonResponse = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
if let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let requestResult = jsonResponse["RequestResult"] as? [String: Any],
let imageUrlString = requestResult["url"] as? String,
let imageUrl = URL(string: imageUrlString) {

self.loadImage(from: imageUrl, withCategory: randomCategory)


if let imageData = try? Data(contentsOf: imageUrl) {
if imageUrlString.lowercased().hasSuffix(".gif") {
if let animatedImage = UIImage.animatedImage(with: UIImage.gifData(data: imageData) ?? [], duration: 1.0) {
self.imageView.image = animatedImage
self.animateImageChange(with: animatedImage)
self.addToHistory(image: animatedImage)
} else {
print("Failed to create animated image from GIF data.")
}
} else {
if let newImage = UIImage(data: imageData) {
self.imageView.image = newImage
self.animateImageChange(with: newImage)
self.addToHistory(image: newImage)
} else {
print("Failed to load image data.")
}
}

self.currentImageURL = imageUrlString
self.tagsLabel.isHidden = false
self.updateUIWithTags([randomCategory])
self.stopLoadingIndicator()
self.incrementCounter()
} else {
print("Failed to load image data.")
self.stopLoadingIndicator()
}
} else {
self.showAlert(withTitle: "Error!", message: "Failed to parse JSON response or missing necessary data.", viewController: self)
print("Failed to parse JSON response or missing necessary data.")
self.stopLoadingIndicator()
}
} catch {
self.showAlert(withTitle: "Error!", message: "Failed to parse JSON response.", viewController: self)
print("Failed to parse JSON response:", error)
self.stopLoadingIndicator()
}
} else {
self.showAlert(withTitle: "Error!", message: "No data received from server.", viewController: self)
print("No data received from server.")
self.stopLoadingIndicator()
}
Expand All @@ -81,57 +97,4 @@ extension ViewController {
task.resume()
}

func loadImage(from url: URL, withCategory category: String) {
URLSession.shared.dataTask(with: url) { [weak self] (data, response, error) in
guard let self = self else { return }
if let error = error {
self.showAlert(withTitle: "Error!", message: "\(error)", viewController: self)
print("Error loading image:", error)
self.stopLoadingIndicator()
return
}

guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
self.showAlert(withTitle: "Invalid HTTP response", message: "Failed to load image.", viewController: self)
print("Invalid HTTP response")
self.stopLoadingIndicator()
return
}

if let data = data {
DispatchQueue.main.async {
if let image = UIImage(data: data) {
if url.absoluteString.lowercased().hasSuffix(".gif") {
if let animatedImage = UIImage.animatedImage(with: UIImage.gifData(data: data) ?? [], duration: 1.0) {
self.imageView.image = animatedImage
self.addToHistory(image: animatedImage)
self.animateImageChange(with: animatedImage)
} else {
self.showAlert(withTitle: "Error!", message: "Failed to create animated image from GIF data.", viewController: self)
print("Failed to create animated image from GIF data.")
}
} else {
self.imageView.image = image
self.addToHistory(image: image)
self.animateImageChange(with: image)
}

self.tagsLabel.isHidden = false
self.updateUIWithTags([category])
self.currentImageURL = url.absoluteString
self.stopLoadingIndicator()
self.incrementCounter()
} else {
self.showAlert(withTitle: "Error!", message: "Failed to load image data.", viewController: self)
print("Failed to load image data.")
self.stopLoadingIndicator()
}
}
} else {
self.showAlert(withTitle: "Error!", message: "No image data received from server.", viewController: self)
print("No image data received from server.")
self.stopLoadingIndicator()
}
}.resume()
}
}
30 changes: 0 additions & 30 deletions AnimeGen/APIs/nekos-best.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ extension ViewController {
let apiEndpoint = "https://nekos.best/api/v2/\(randomCategory)"

guard let url = URL(string: apiEndpoint) else {

if self.alert {
self.showAlert(withTitle: "Invalid URL", message: "Please wait, the api may be down.", viewController: self)
}

print("Invalid URL")
stopLoadingIndicator()
return
Expand All @@ -34,33 +29,18 @@ extension ViewController {
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
DispatchQueue.main.async {
if let error = error {

if self.alert {
self.showAlert(withTitle: "Error!", message: "\(error)", viewController: self)
}

print("Error: \(error)")
self.stopLoadingIndicator()
return
}

guard let httpResponse = response as? HTTPURLResponse else {

if self.alert {
self.showAlert(withTitle: "Invalid HTTP response", message: "Please wait, the api may be down.", viewController: self)
}

print("Invalid HTTP response")
self.stopLoadingIndicator()
return
}

guard httpResponse.statusCode == 200 else {

if self.alert {
self.showAlert(withTitle: "Invalid status code", message: "\(httpResponse.statusCode)", viewController: self)
}

print("Invalid status code: \(httpResponse.statusCode)")
self.stopLoadingIndicator()
return
Expand All @@ -86,23 +66,13 @@ extension ViewController {
self.addToHistory(image: newImage)
self.updateUIWithTags([], author: author, category: category)
self.stopLoadingIndicator()
self.incrementCounter()
} else {

if self.alert {
self.showAlert(withTitle: "Error!", message: "Failed to load image data.", viewController: self)
}

print("Failed to load image data.")
self.stopLoadingIndicator()
}
} else {
print("Failed to parse JSON response or missing necessary data.")
self.stopLoadingIndicator()

if self.alert {
self.showAlert(withTitle: "Error!", message: "Failed to parse JSON response or missing data.", viewController: self)
}
}
}
}
Expand Down
36 changes: 1 addition & 35 deletions AnimeGen/APIs/nekosapi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ extension ViewController {
let apiEndpoint = "https://api.nekosapi.com/v3/images/random?limit=1&rating=\(randomRating)"

guard let url = URL(string: apiEndpoint) else {

if self.alert {
self.showAlert(withTitle: "Invalid URL", message: "Please wait, the api may be down.", viewController: self)
}

print("Invalid URL")
stopLoadingIndicator()
return
Expand All @@ -39,33 +34,18 @@ extension ViewController {
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
DispatchQueue.main.async {
if let error = error {

if self.alert {
self.showAlert(withTitle: "Error!", message: "\(error)", viewController: self)
}

print("Error: \(error)")
self.stopLoadingIndicator()
return
}

guard let httpResponse = response as? HTTPURLResponse else {

if self.alert {
self.showAlert(withTitle: "Invalid HTTP response", message: "Please wait, the api may be down.", viewController: self)
}

print("Invalid HTTP response")
self.stopLoadingIndicator()
return
}

guard httpResponse.statusCode == 200 else {

if self.alert {
self.showAlert(withTitle: "Invalid status code", message: "\(httpResponse.statusCode)", viewController: self)
}

print("Invalid status code: \(httpResponse.statusCode)")
self.stopLoadingIndicator()
return
Expand All @@ -87,31 +67,17 @@ extension ViewController {
if let data = try? Data(contentsOf: imageUrl), let newImage = UIImage(data: data) {
self.imageView.image = newImage
self.animateImageChange(with: newImage)
self.addToHistory(image: newImage)

self.tagsLabel.isHidden = false

self.addToHistory(image: newImage)
self.updateUIWithTags(tags)

self.stopLoadingIndicator()

self.incrementCounter()
} else {
print("Failed to load image data.")
self.stopLoadingIndicator()

if self.alert {
self.showAlert(withTitle: "Error!", message: "Failed to load image data.", viewController: self)
}

}
} else {
print("Failed to parse JSON response or missing necessary data.")
self.stopLoadingIndicator()

if self.alert {
self.showAlert(withTitle: "Error!", message: "Failed to parse JSON response or missing data.", viewController: self)
}
}
}
}
Expand Down
42 changes: 14 additions & 28 deletions AnimeGen/APIs/nekosbot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ extension ViewController {
func loadImageFromNekoBot() {
startLoadingIndicator()

let categoriesSFW: [String] = ["neko", "coffee", "food", "kemonomimi",]
let categoriesNSFW: [String] = ["hentai", "hkitsune", "hanal", "hthigh", "hboobs", "yaoi"]
let categories: [String]
let endpointPrefix: String

let isExplicitContentEnabled = UserDefaults.standard.bool(forKey: "enableExplictiCont")
let randomCategory = isExplicitContentEnabled ? categoriesNSFW.randomElement() ?? "hentai" : categoriesSFW.randomElement() ?? "hass"
if UserDefaults.standard.bool(forKey: "enableExplictiCont") {
categories = ["hentai", "hkitsune", "hanal", "hthigh", "hboobs", "yaoi"]
endpointPrefix = "https://nekobot.xyz/api/image?type="
} else {
categories = ["neko", "coffee", "food", "kemonomimi"]
endpointPrefix = "https://nekobot.xyz/api/image?type="
}

let randomCategory = categories.randomElement() ?? "neko"

let apiEndpoint = "https://nekobot.xyz/api/image?type=\(randomCategory)"
let apiEndpoint = "\(endpointPrefix)\(randomCategory)"

guard let url = URL(string: apiEndpoint) else {
if self.alert {
self.showAlert(withTitle: "Invalid URL", message: "Please wait, the API may be down.", viewController: self)
}
print("Invalid URL")
stopLoadingIndicator()
return
Expand All @@ -32,24 +36,19 @@ extension ViewController {
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
DispatchQueue.main.async {
if let error = error {
if self.alert {
self.showAlert(withTitle: "Error!", message: "\(error)", viewController: self)
}
print("Error: \(error)")
self.stopLoadingIndicator()
return
}

guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
if self.alert {
self.showAlert(withTitle: "Invalid HTTP response", message: "Please wait, the API may be down.", viewController: self)
}
print("Invalid HTTP response")
self.stopLoadingIndicator()
return
}

if let data = data, let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], let imageUrlString = jsonResponse["message"] as? String, let imageUrl = URL(string: imageUrlString) {

if let imageData = try? Data(contentsOf: imageUrl) {
if imageUrlString.lowercased().hasSuffix(".gif") {
if let animatedImage = UIImage.animatedImage(with: UIImage.gifData(data: imageData) ?? [], duration: 1.0) {
Expand All @@ -58,38 +57,25 @@ extension ViewController {
self.addToHistory(image: animatedImage)
} else {
print("Failed to create animated image from GIF data.")
if self.alert {
self.showAlert(withTitle: "Error!", message: "Failed to create animated image from GIF data.", viewController: self)
}
}
} else {
if let newImage = UIImage(data: imageData) {
self.imageView.image = newImage
self.addToHistory(image: newImage)
self.animateImageChange(with: newImage)
self.addToHistory(image: newImage)
} else {
print("Failed to load image data.")
if self.alert {
self.showAlert(withTitle: "Error!", message: "Failed to load image data.", viewController: self)
}
}
}
self.currentImageURL = imageUrlString
self.tagsLabel.isHidden = false
self.updateUIWithTags([randomCategory])
self.stopLoadingIndicator()
self.incrementCounter()
} else {
print("Failed to load image data.")
if self.alert {
self.showAlert(withTitle: "Error!", message: "Failed to load image data.", viewController: self)
}
self.stopLoadingIndicator()
}
} else {
if self.alert {
self.showAlert(withTitle: "Error!", message: "Failed to parse JSON response or missing data.", viewController: self)
}
print("Failed to parse JSON response or missing necessary data.")
self.stopLoadingIndicator()
}
Expand Down
Loading

0 comments on commit 94c36d7

Please sign in to comment.