import Foundation
class Scraper {
private static let API_KEY = "YOUR_API_KEY"
private static let BASE_URL = "https://api.scrapecreators.com"
private static let ENDPOINT_PATH = "/v1/facebook/adLibrary/search/ads"
static func scrape(completion: @escaping (Result<String, Error>) -> Void) {
// Build query parameters
var components = URLComponents(string: BASE_URL + ENDPOINT_PATH)!
components.queryItems = [
URLQueryItem(name: "query", value: "running"),
URLQueryItem(name: "search_type", value: "keyword_unordered"),
URLQueryItem(name: "ad_type", value: "all"),
URLQueryItem(name: "country", value: "ALL"),
URLQueryItem(name: "status", value: "ACTIVE"),
URLQueryItem(name: "media_type", value: "ALL"),
URLQueryItem(name: "start_date", value: "2025-01-05"),
URLQueryItem(name: "end_date", value: "2025-02-16"),
URLQueryItem(name: "cursor", value: "AQHRYLVDkoMkvGv7yK1rcce-vJmKiKv330R4v3j9KHSOaYvmF1bq1QkotG0rgW8Fkrj-"),
URLQueryItem(name: "trim", value: "false")
]
guard let url = components.url else {
completion(.failure(NSError(domain: "Invalid URL", code: -1, userInfo: nil)))
return
}
var request = URLRequest(url: url)
request.setValue(API_KEY, forHTTPHeaderField: "x-api-key")
URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
completion(.failure(error))
return
}