-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
286 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// | ||
// Request.swift | ||
// Osmium | ||
// | ||
// Created by Francesco on 05/06/24. | ||
// | ||
|
||
import UIKit | ||
|
||
extension ViewController { | ||
|
||
func getUserDefaultsValue<T>(key: String, defaultValue: T) -> T { | ||
return UserDefaults.standard.object(forKey: key) as? T ?? defaultValue | ||
} | ||
|
||
func sendPostRequest() { | ||
guard let urlText = urlTextField.text, !urlText.isEmpty else { | ||
writeToConsole("URL text field is empty") | ||
showAlert(title: "Error", message: "Please enter a valid URL") | ||
return | ||
} | ||
|
||
self.writeToConsole("Starting process...") | ||
|
||
UserDefaults.standard.set(urlText, forKey: "url") | ||
|
||
guard let url = URL(string: "https://api.cobalt.tools/api/json") else { | ||
writeToConsole("Invalid API URL") | ||
showAlert(title: "Error", message: "Invalid API URL") | ||
return | ||
} | ||
|
||
let requestBody: [String: Any] = [ | ||
"url": getUserDefaultsValue(key: "url", defaultValue: "https://example.com/video"), | ||
"vCodec": getUserDefaultsValue(key: "vCodec", defaultValue: "h264"), | ||
"vQuality": getUserDefaultsValue(key: "vQuality", defaultValue: "720"), | ||
"aFormat": getUserDefaultsValue(key: "aFormat", defaultValue: "mp3"), | ||
"filenamePattern": getUserDefaultsValue(key: "filenamePattern", defaultValue: "classic"), | ||
"isAudioOnly": getUserDefaultsValue(key: "isAudioOnly", defaultValue: false), | ||
"isTTFullAudio": getUserDefaultsValue(key: "isTTFullAudio", defaultValue: false), | ||
"isAudioMuted": getUserDefaultsValue(key: "isAudioMuted", defaultValue: false), | ||
"dubLang": getUserDefaultsValue(key: "dubLang", defaultValue: false), | ||
"disableMetadata": getUserDefaultsValue(key: "disableMetadata", defaultValue: false), | ||
"twitterGif": getUserDefaultsValue(key: "twitterGif", defaultValue: false), | ||
"tiktokH265": getUserDefaultsValue(key: "tiktokH265", defaultValue: false) | ||
] | ||
|
||
if debug { | ||
writeToConsole("Request Body:") | ||
requestBody.forEach { key, value in | ||
writeToConsole("\(key): \(value)") | ||
} | ||
} | ||
|
||
guard let jsonData = try? JSONSerialization.data(withJSONObject: requestBody) else { | ||
writeToConsole("Failed to serialize JSON data") | ||
showAlert(title: "Error", message: "Failed to process request") | ||
return | ||
} | ||
|
||
var request = URLRequest(url: url) | ||
request.httpMethod = "POST" | ||
request.setValue("application/json", forHTTPHeaderField: "Accept") | ||
request.setValue("application/json", forHTTPHeaderField: "Content-Type") | ||
request.httpBody = jsonData | ||
|
||
let task = URLSession.shared.dataTask(with: request) { [weak self] data, response, error in | ||
guard let self = self else { return } | ||
|
||
if let error = error { | ||
self.writeToConsole("Error: \(error)") | ||
self.showAlert(title: "Error", message: "Failed to process request") | ||
return | ||
} | ||
|
||
guard let data = data else { | ||
self.writeToConsole("No data") | ||
self.showAlert(title: "Error", message: "No data received") | ||
return | ||
} | ||
|
||
if self.debug { | ||
if let responseString = String(data: data, encoding: .utf8) { | ||
self.writeToConsole("Response: \(responseString)") | ||
} else { | ||
self.writeToConsole("Unable to convert response data to string") | ||
} | ||
} | ||
|
||
do { | ||
guard let jsonObject = try JSONSerialization.jsonObject(with: data) as? [String: Any] else { | ||
throw NSError(domain: "ParsingError", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to parse JSON"]) | ||
} | ||
|
||
guard let status = jsonObject["status"] as? String, let mediaURLString = jsonObject["url"] as? String else { | ||
throw NSError(domain: "ParsingError", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to extract required data from JSON"]) | ||
} | ||
|
||
switch status { | ||
case "redirect": | ||
if self.saveMedia { | ||
self.saveMediaToGallery(urlString: mediaURLString) | ||
} else { | ||
self.openURLInSafari(urlString: mediaURLString) | ||
} | ||
self.writeToConsole("Saving media...") | ||
|
||
case "stream": | ||
self.openURLInSafari(urlString: mediaURLString) | ||
self.writeToConsole("Opening link...") | ||
|
||
default: | ||
self.writeToConsole("Unexpected status in response") | ||
self.showAlert(title: "Error", message: "Unexpected status in response") | ||
} | ||
} catch { | ||
self.writeToConsole("Error: \(error.localizedDescription)") | ||
self.showAlert(title: "Error", message: "Failed to parse response") | ||
} | ||
} | ||
task.resume() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// GallerySaver.swift | ||
// Osmium | ||
// | ||
// Created by Francesco on 05/06/24. | ||
// | ||
|
||
import UIKit | ||
|
||
extension ViewController { | ||
|
||
func saveMediaToGallery(urlString: String) { | ||
guard let url = URL(string: urlString) else { | ||
writeToConsole("Invalid URL") | ||
return | ||
} | ||
|
||
let session = URLSession.shared | ||
let task = session.dataTask(with: url) { [weak self] (data, response, error) in | ||
guard let self = self else { return } | ||
|
||
if let error = error { | ||
self.handleMediaSavingError(error) | ||
return | ||
} | ||
|
||
guard let data = data else { | ||
self.handleMediaSavingError("No data received") | ||
return | ||
} | ||
|
||
guard let contentType = response?.mimeType else { | ||
self.handleMediaSavingError("Unknown content type") | ||
return | ||
} | ||
|
||
switch contentType { | ||
case _ where contentType.hasPrefix("image"): | ||
self.saveImageToGallery(data) | ||
case _ where contentType.hasPrefix("video"): | ||
self.saveVideoToGallery(data) | ||
default: | ||
self.handleMediaSavingError("Unsupported content type: \(contentType)") | ||
} | ||
} | ||
task.resume() | ||
} | ||
|
||
private func saveImageToGallery(_ imageData: Data) { | ||
guard let image = UIImage(data: imageData) else { | ||
handleMediaSavingError("Unable to create image from data") | ||
return | ||
} | ||
|
||
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) | ||
writeToConsole("Image saved to gallery successfully") | ||
showAlert(title: "Success", message: "Media saved to gallery successfully") | ||
} | ||
|
||
private func saveVideoToGallery(_ videoData: Data) { | ||
let tempURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("video.mp4") | ||
do { | ||
try videoData.write(to: tempURL) | ||
UISaveVideoAtPathToSavedPhotosAlbum(tempURL.path, nil, nil, nil) | ||
writeToConsole("Video saved to gallery successfully") | ||
showAlert(title: "Success", message: "Media saved to gallery successfully") | ||
} catch { | ||
handleMediaSavingError("Error saving video to gallery: \(error)") | ||
} | ||
} | ||
|
||
private func handleMediaSavingError(_ errorMessage: String) { | ||
writeToConsole("Error: \(errorMessage)") | ||
showAlert(title: "Error", message: errorMessage) | ||
} | ||
|
||
private func handleMediaSavingError(_ error: Error) { | ||
handleMediaSavingError(error.localizedDescription) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// URLSaver.swift | ||
// Osmium | ||
// | ||
// Created by Francesco on 05/06/24. | ||
// | ||
|
||
import UIKit | ||
|
||
extension ViewController { | ||
|
||
func openURLInSafari(urlString: String) { | ||
guard let url = URL(string: urlString) else { | ||
writeToConsole("Invalid URL") | ||
return | ||
} | ||
DispatchQueue.main.async { | ||
UIApplication.shared.open(url, options: [:], completionHandler: nil) | ||
self.writeToConsole("Done!") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.