Skip to content

Commit

Permalink
added nekos.life + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cranci1 committed May 29, 2024
1 parent 4073206 commit 7c4c76d
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 11 deletions.
4 changes: 4 additions & 0 deletions AnimeGen.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
13C1E03C2BE9068600A27DEE /* AboutPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13C1E03B2BE9068600A27DEE /* AboutPage.swift */; };
13C1E03E2BE9183400A27DEE /* Credits-Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13C1E03D2BE9183400A27DEE /* Credits-Images.xcassets */; };
13C1E0402BE92BB300A27DEE /* APIsCredits.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13C1E03F2BE92BB300A27DEE /* APIsCredits.swift */; };
13C861652C033DE70066DC71 /* nekos-life.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13C861642C033DE70066DC71 /* nekos-life.swift */; };
13D4FBE02BE7661A00BC3D1C /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 13D4FBDF2BE7661A00BC3D1C /* Main.storyboard */; };
13D4FBE62BE7689500BC3D1C /* Tags.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13D4FBE52BE7689400BC3D1C /* Tags.swift */; };
13D4FBE92BE768C100BC3D1C /* HeartButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13D4FBE82BE768C100BC3D1C /* HeartButton.swift */; };
Expand Down Expand Up @@ -86,6 +87,7 @@
13C1E03B2BE9068600A27DEE /* AboutPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutPage.swift; sourceTree = "<group>"; };
13C1E03D2BE9183400A27DEE /* Credits-Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Credits-Images.xcassets"; sourceTree = "<group>"; };
13C1E03F2BE92BB300A27DEE /* APIsCredits.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIsCredits.swift; sourceTree = "<group>"; };
13C861642C033DE70066DC71 /* nekos-life.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "nekos-life.swift"; sourceTree = "<group>"; };
13D4FBDF2BE7661A00BC3D1C /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
13D4FBE52BE7689400BC3D1C /* Tags.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tags.swift; sourceTree = "<group>"; };
13D4FBE82BE768C100BC3D1C /* HeartButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeartButton.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -208,6 +210,7 @@
13D4FC122BE770EB00BC3D1C /* waifu-it.swift */,
13D4FC152BE770EC00BC3D1C /* waifu-pics.swift */,
13A8496A2BF479DA009A9442 /* n-sfw.swift */,
13C861642C033DE70066DC71 /* nekos-life.swift */,
);
path = "APIs Requests";
sourceTree = "<group>";
Expand Down Expand Up @@ -384,6 +387,7 @@
13D4FC1E2BE770EC00BC3D1C /* waifu-im.swift in Sources */,
13D4FC1D2BE770EC00BC3D1C /* waifu-it.swift in Sources */,
1393C3AA2BEBC94300704137 /* Dev-History.swift in Sources */,
13C861652C033DE70066DC71 /* nekos-life.swift in Sources */,
13D4FC1C2BE770EC00BC3D1C /* purr.swift in Sources */,
13D4FC292BE7710500BC3D1C /* HmtaiReader.swift in Sources */,
136BD7E82BE7E24100ED23AE /* Developer.swift in Sources */,
Expand Down
Binary file not shown.
111 changes: 111 additions & 0 deletions AnimeGen/APIs Requests/nekos-life.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//
// nekos-life.swift
// AnimeGen
//
// Created by cranci on 26/05/24.
//

import UIKit
import SDWebImage

extension ViewController {

func loadImageFromNekosLife() {
startLoadingIndicator()

let startTime = DispatchTime.now()

DispatchQueue.global().async { [weak self] in
guard let self = self else { return }

let endpointPrefix = "https://nekos.life/api/v2/img/"
let categories = ["tickle", "slap", "pat", "neko", "kiss", "hug", "fox_girl", "feed", "cuddle", "ngif", "smug", "wallpaper", "gecg", "avatar", "waifu"]

let randomIndex = Int(arc4random_uniform(UInt32(categories.count)))
let randomCategory = categories[randomIndex]

guard let url = URL(string: "\(endpointPrefix)\(randomCategory)") else {
print("Invalid URL")
DispatchQueue.main.async {
self.stopLoadingIndicator()
}
return
}

let request = URLRequest(url: url)

self.fetchImage(with: request, startTime: startTime, tag: randomCategory)
}
}

private func fetchImage(with request: URLRequest, startTime: DispatchTime, tag: String) {
URLSession.shared.dataTask(with: request) { [weak self] (data, response, error) in
guard let self = self else { return }

if let error = error {
print("Error: \(error)")
DispatchQueue.main.async {
self.stopLoadingIndicator()
}
return
}

guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
print("Invalid HTTP response")
DispatchQueue.main.async {
self.stopLoadingIndicator()
}
return
}

guard let data = data,
let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let imageUrlString = jsonResponse["url"] as? String else {
print("Invalid image data or missing response headers")
DispatchQueue.main.async {
self.stopLoadingIndicator()
}
return
}

DispatchQueue.main.async {
let isGif = imageUrlString.hasSuffix(".gif")

if isGif {
self.imageView.sd_setImage(with: URL(string: imageUrlString), placeholderImage: nil, options: .allowInvalidSSLCertificates, context: nil, progress: nil, completed: { (image, error, cacheType, url) in
if let error = error {
print("Error loading GIF image: \(error)")
} else {
self.handleImageLoadingCompletion(with: image ?? UIImage(), tags: [tag], imageUrlString: imageUrlString)
}
})
} else {
self.imageView.sd_setImage(with: URL(string: imageUrlString), placeholderImage: nil, options: [], context: nil, progress: nil, completed: { (image, error, cacheType, url) in
if let error = error {
print("Error loading image: \(error)")
} else {
self.handleImageLoadingCompletion(with: image ?? UIImage(), tags: [tag], imageUrlString: imageUrlString)
}
})
}

let endTime = DispatchTime.now()
let executionTime = endTime.uptimeNanoseconds - startTime.uptimeNanoseconds
print("Execution time: \(Double(executionTime) / 1_000_000_000) seconds")
}
}.resume()
}

private func handleImageLoadingCompletion(with newImage: UIImage, tags: [String], imageUrlString: String) {
addImageToHistory(image: newImage, tags: tags)
currentImageURL = imageUrlString
updateUIWithTags(tags)
addToHistory(image: newImage)
tagsLabel.isHidden = false
imageView.image = newImage
animateImageChange(with: newImage)
stopLoadingIndicator()
incrementCounter()
}
}

10 changes: 6 additions & 4 deletions AnimeGen/Buttons func/Refresh-API-Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ extension ViewController {
loadImageFromNekoBot()
case "n-sfw.com":
loadImageFromNSFW()
case "nekos.life":
loadImageFromNekosLife()
default:
print("Unknown API: \(title)")
}
Expand All @@ -64,11 +66,11 @@ extension ViewController {
let apiOptions: [String]

if #available(iOS 14.0, *) {
apiOptions = developerAPIs ? ["Purr", "kyoko", "n-sfw.com", "NekoBot", "nekos.moe", "Nekos api", "nekos.best", "Hmtai api", "waifu.it", "waifu.pics", "waifu.im", "pic.re"]
: ["Purr", "n-sfw.com", "NekoBot", "nekos.moe", "Nekos api", "nekos.best", "waifu.pics", "waifu.im", "pic.re"]
apiOptions = developerAPIs ? ["Purr", "kyoko", "n-sfw.com", "nekos.life", "NekoBot", "nekos.moe", "Nekos api", "nekos.best", "Hmtai api", "waifu.it", "waifu.pics", "waifu.im", "pic.re"]
: ["Purr", "n-sfw.com", "nekos.life", "NekoBot", "nekos.moe", "Nekos api", "nekos.best", "waifu.pics", "waifu.im", "pic.re"]
} else {
apiOptions = developerAPIs ? ["Purr", "kyoko", "n-sfw.com", "NekoBot", "nekos.moe", "nekos.best", "Hmtai api", "waifu.it", "waifu.pics", "waifu.im"]
: ["Purr", "n-sfw.com", "NekoBot", "nekos.moe", "nekos.best", "waifu.pics", "waifu.im"]
apiOptions = developerAPIs ? ["Purr", "kyoko", "n-sfw.com", "nekos.life", "NekoBot", "nekos.moe", "nekos.best", "Hmtai api", "waifu.it", "waifu.pics", "waifu.im"]
: ["Purr", "n-sfw.com", "nekos.life", "NekoBot", "nekos.moe", "nekos.best", "waifu.pics", "waifu.im"]
}

apiOptions.forEach { option in
Expand Down
2 changes: 1 addition & 1 deletion AnimeGen/ImageSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class Settings {
static let imageHeight: Double = 0.6
}

private let queue = DispatchQueue(label: "com.example.SettingsQueue", attributes: .concurrent)
private let queue = DispatchQueue(label: "me.cranci.SettingsQueue", attributes: .concurrent)

var imageWidth: Double {
get {
Expand Down
Loading

0 comments on commit 7c4c76d

Please sign in to comment.