Skip to content

Commit

Permalink
lott of stuffsss
Browse files Browse the repository at this point in the history
  • Loading branch information
cranci1 committed May 17, 2024
1 parent ccad842 commit b98d003
Show file tree
Hide file tree
Showing 20 changed files with 550 additions and 1,086 deletions.
4 changes: 0 additions & 4 deletions AnimeGen.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
133C4C8B2BE67CAB00258EDD /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 133C4C8A2BE67CAB00258EDD /* ViewController.swift */; };
133C4C902BE67CAD00258EDD /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 133C4C8F2BE67CAD00258EDD /* Assets.xcassets */; };
136BD7DB2BE7CA6F00ED23AE /* HistoryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 136BD7D92BE7CA6F00ED23AE /* HistoryView.swift */; };
136BD7DC2BE7CA6F00ED23AE /* ImageHistory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 136BD7DA2BE7CA6F00ED23AE /* ImageHistory.swift */; };
136BD7DE2BE7CAA300ED23AE /* HistoryButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 136BD7DD2BE7CAA300ED23AE /* HistoryButton.swift */; };
136BD7E02BE7CB4D00ED23AE /* RewindButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 136BD7DF2BE7CB4D00ED23AE /* RewindButton.swift */; };
136BD7E22BE7CCC400ED23AE /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 136BD7E12BE7CCC400ED23AE /* PrivacyInfo.xcprivacy */; };
Expand Down Expand Up @@ -68,7 +67,6 @@
133C4C8F2BE67CAD00258EDD /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
133C4C942BE67CAD00258EDD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
136BD7D92BE7CA6F00ED23AE /* HistoryView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HistoryView.swift; sourceTree = "<group>"; };
136BD7DA2BE7CA6F00ED23AE /* ImageHistory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageHistory.swift; sourceTree = "<group>"; };
136BD7DD2BE7CAA300ED23AE /* HistoryButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryButton.swift; sourceTree = "<group>"; };
136BD7DF2BE7CB4D00ED23AE /* RewindButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RewindButton.swift; sourceTree = "<group>"; };
136BD7E12BE7CCC400ED23AE /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
Expand Down Expand Up @@ -169,7 +167,6 @@
isa = PBXGroup;
children = (
136BD7D92BE7CA6F00ED23AE /* HistoryView.swift */,
136BD7DA2BE7CA6F00ED23AE /* ImageHistory.swift */,
);
path = History;
sourceTree = "<group>";
Expand Down Expand Up @@ -386,7 +383,6 @@
13D4FC1C2BE770EC00BC3D1C /* purr.swift in Sources */,
13D4FC292BE7710500BC3D1C /* HmtaiReader.swift in Sources */,
136BD7E82BE7E24100ED23AE /* Developer.swift in Sources */,
136BD7DC2BE7CA6F00ED23AE /* ImageHistory.swift in Sources */,
13D4FBF02BE76D4000BC3D1C /* popup-Banner.swift in Sources */,
13D4FC2A2BE7710500BC3D1C /* HmtaiSender.swift in Sources */,
13D4FC262BE770EC00BC3D1C /* nekos-best.swift in Sources */,
Expand Down
Binary file not shown.
145 changes: 79 additions & 66 deletions AnimeGen/APIs Requests/waifu-im.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,80 +8,93 @@
import UIKit

extension ViewController {

func loadImageFromWaifuIm() {
startLoadingIndicator()

DispatchQueue.global().async {
let isNSFW = UserDefaults.standard.bool(forKey: "enableExplictiCont")
let apiEndpoint = "https://api.waifu.im/search"

var components = URLComponents(string: apiEndpoint)
components?.queryItems = [
URLQueryItem(name: "is_nsfw", value: isNSFW ? "true" : "false")
]

guard let url = components?.url else {
print("Invalid URL")
self.stopLoadingIndicator()
return
}

var request = URLRequest(url: url)
request.httpMethod = "GET"

URLSession.shared.dataTask(with: request) { [weak self] (data, response, error) in
guard let self = self else { return }

DispatchQueue.main.async {
if let error = error {
print("Error: \(error)")
self.stopLoadingIndicator()
let isNSFW = UserDefaults.standard.bool(forKey: "enableExplictiCont")
let apiEndpoint = "https://api.waifu.im/search"

guard var components = URLComponents(string: apiEndpoint) else {
print("Invalid URL")
stopLoadingIndicator()
return
}
components.queryItems = [
URLQueryItem(name: "is_nsfw", value: isNSFW ? "true" : "false")
]

guard let url = components.url else {
print("Invalid URL")
stopLoadingIndicator()
return
}

var request = URLRequest(url: url)
request.httpMethod = "GET"

URLSession.shared.dataTask(with: request) { [weak self] (data, response, error) in
guard let self = self else { return }

DispatchQueue.main.async {
defer {
self.stopLoadingIndicator()
}

if let error = error {
print("Error: \(error)")
return
}

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

guard let data = data else {
print("No data received")
return
}

do {
guard let jsonResponse = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let images = jsonResponse["images"] as? [[String: Any]],
let firstImage = images.first,
let imageUrlString = firstImage["url"] as? String,
let imageUrl = URL(string: imageUrlString),
let tagsArray = firstImage["tags"] as? [[String: Any]] else {
print("Failed to parse JSON response or missing necessary data.")
return
}

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

self.currentImageURL = imageUrlString

let tags = tagsArray.compactMap { $0["name"] as? String }

guard let imageData = try? Data(contentsOf: imageUrl),
let newImage = UIImage(data: imageData) else {
print("Failed to load image data.")
return
}

do {
if let jsonData = data,
let jsonResponse = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any],
let images = jsonResponse["images"] as? [[String: Any]],
let firstImage = images.first,
let imageUrlString = firstImage["url"] as? String,
let imageUrl = URL(string: imageUrlString),
let tagsArray = firstImage["tags"] as? [[String: Any]] {

self.currentImageURL = imageUrlString

let tags = tagsArray.compactMap { $0["name"] as? String }

if let data = try? Data(contentsOf: imageUrl), let newImage = UIImage(data: data) {
DispatchQueue.main.async {
self.imageView.image = newImage
self.addToHistory(image: newImage)
self.animateImageChange(with: newImage)
self.tagsLabel.isHidden = false
self.addImageToHistory(image: newImage, tags: tags)
self.updateUIWithTags(tags)
self.stopLoadingIndicator()
self.incrementCounter()
}
} else {
print("Failed to load image data.")
self.stopLoadingIndicator()
}
} else {
print("Failed to parse JSON response or missing necessary data.")
self.stopLoadingIndicator()
}
}

self.handleImageLoadingCompletion(with: newImage, andTags: tags)

} catch {
print("Error decoding JSON: \(error)")
}
}.resume()
}
}.resume()
}

private func handleImageLoadingCompletion(with image: UIImage, andTags tags: [String]) {
DispatchQueue.main.async {
self.imageView.image = image
self.addToHistory(image: image)
self.animateImageChange(with: image)
self.tagsLabel.isHidden = false
self.addImageToHistory(image: image, tags: tags)
self.updateUIWithTags(tags)
self.incrementCounter()
}
}

}
62 changes: 34 additions & 28 deletions AnimeGen/APIs Requests/waifu-it.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ extension ViewController {
stopLoadingIndicator()
return
}

var request = URLRequest(url: url)
request.setValue(Secrets.waifuItToken, forHTTPHeaderField: "Authorization")

let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
DispatchQueue.main.async {
if let error = error {
Expand All @@ -50,35 +50,29 @@ extension ViewController {

if let data = data, let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], let imageUrlString = jsonResponse["url"] 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) {
self.imageView.image = animatedImage
self.addImageToHistory(image: animatedImage, tags: [randomCategory])
self.animateImageChange(with: animatedImage)
self.addToHistory(image: animatedImage)
} else {
print("Failed to create animated image from GIF data.")
DispatchQueue.global().async {
if let imageData = try? Data(contentsOf: imageUrl) {
DispatchQueue.main.async {
if imageUrlString.lowercased().hasSuffix(".gif") {
if let animatedImage = UIImage.animatedImage(with: UIImage.gifData(data: imageData) ?? [], duration: 1.0) {
self.handleImageLoadingCompletion(with: animatedImage, tags: [randomCategory], imageUrlString: imageUrlString)
} else {
print("Failed to create animated image from GIF data.")
self.stopLoadingIndicator()
}
} else {
if let newImage = UIImage(data: imageData) {
self.handleImageLoadingCompletion(with: newImage, tags: [randomCategory], imageUrlString: imageUrlString)
} else {
print("Failed to load image data.")
self.stopLoadingIndicator()
}
}
}
} else {
if let newImage = UIImage(data: imageData) {
self.imageView.image = newImage
self.addImageToHistory(image: newImage, tags: [randomCategory])
self.animateImageChange(with: newImage)
self.addToHistory(image: newImage)
} else {
print("Failed to load image data.")
}
print("Failed to load image data.")
self.stopLoadingIndicator()
}

self.currentImageURL = imageUrlString
self.tagsLabel.isHidden = false
self.updateUIWithTags([randomCategory])
self.incrementCounter()
self.stopLoadingIndicator()
} else {
print("Failed to load image data.")
self.stopLoadingIndicator()
}
} else {
print("Failed to parse JSON response or missing necessary data.")
Expand All @@ -89,4 +83,16 @@ extension ViewController {

task.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()
}
}
77 changes: 46 additions & 31 deletions AnimeGen/APIs Requests/waifu-pics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,8 @@ extension ViewController {
return
}

if let data = data, let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], let imageUrlString = jsonResponse["url"] 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) {
self.imageView.image = animatedImage
self.addImageToHistory(image: animatedImage, tags: [randomCategory])
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.addImageToHistory(image: newImage, tags: [randomCategory])
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()
}
if let data = data, let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], let imageUrlString = jsonResponse["url"] as? String {
self.loadImage(with: imageUrlString, tags: [randomCategory])
} else {
print("Failed to parse JSON response or missing necessary data.")
self.stopLoadingIndicator()
Expand All @@ -88,4 +59,48 @@ extension ViewController {
task.resume()
}

private func loadImage(with imageUrlString: String, tags: [String]) {
guard let imageUrl = URL(string: imageUrlString) else {
print("Invalid image URL")
stopLoadingIndicator()
return
}

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

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()
}
}
8 changes: 4 additions & 4 deletions AnimeGen/Buttons func/Refresh-API-Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ extension ViewController {
var apiOptions: [String]

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

0 comments on commit b98d003

Please sign in to comment.