get pornhub api in swift

To get data from the Pornhub API in Swift, you can use URLSession to create the network request and Codable to parse the JSON data. Here's an example of how to do it:

main.swift
struct PornhubAPIResponse: Codable {
    let videos: [PornhubVideo]
}

struct PornhubVideo: Codable {
    let title: String
    let duration: Int
    let views: Int
    // add other properties you want to use
}

func searchPornhub(query: String, completion: @escaping (Result<[PornhubVideo], Error>) -> Void) {
    let urlString = "https://www.pornhub.com/webmasters/search?id=<YOUR_ID_HERE>&search=\(query)"
    guard let url = URL(string: urlString) else {
        completion(.failure(NSError(domain: "Invalid URL", code: 0, userInfo: nil)))
        return
    }
    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
        guard let data = data else {
            completion(.failure(error ?? NSError(domain: "Unknown error", code: 0, userInfo: nil)))
            return
        }
        do {
            let response = try JSONDecoder().decode(PornhubAPIResponse.self, from: data)
            completion(.success(response.videos))
        } catch {
            completion(.failure(error))
        }
    }
    task.resume()
}
1059 chars
32 lines

Replace <YOUR_ID_HERE> with your API ID, which you can get by signing up for a Pornhub API account. Then call searchPornhub with a query string, and it will return an array of PornhubVideo objects.

gistlibby LogSnag