68 lines
2.0 KiB
Swift
68 lines
2.0 KiB
Swift
// scrape git.kpgpmc.org for new projects 100% legit no scam
|
|
|
|
import Foundation
|
|
let gitea = URL(string: "https://git.kpgpmc.org/explore/repos")!
|
|
var request = URLRequest(url: gitea)
|
|
request.httpMethod = "GET"
|
|
|
|
if CommandLine.arguments.count > 1,
|
|
let cookiestring = String?(CommandLine.arguments[1]) {
|
|
request.setValue("i_like_gitea=\(cookiestring)", forHTTPHeaderField: "Cookie")
|
|
}
|
|
|
|
let semaphore = DispatchSemaphore(value: 0)
|
|
let search_string = "tw-text-primary name"
|
|
let hrefRegex = #/href="([^"]+)"/#
|
|
var counter = 0
|
|
var counter2 = 0
|
|
var output = ""
|
|
|
|
let task = URLSession.shared.dataTask(with: request) {data, response, error in
|
|
defer {
|
|
semaphore.signal()
|
|
}
|
|
if let error = error {
|
|
print("error:", error)
|
|
return
|
|
}
|
|
guard let data = data else {
|
|
print("no data recieved")
|
|
return
|
|
}
|
|
if let text = String(data: data, encoding: .utf8) {
|
|
text.enumerateLines { line, stop in
|
|
if line.contains(search_string) {
|
|
if let match = try? hrefRegex.firstMatch(in: line) {
|
|
counter += 1
|
|
|
|
if counter % 2 == 0 {
|
|
counter2 += 1
|
|
print("project: \(counter2) \(match.output.1)", to: &output)
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
task.resume()
|
|
semaphore.wait()
|
|
let stringtoint = "\(counter / 2)"
|
|
let url1 = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
|
|
let filePath = url1.appending(path: "number_of_projects.txt")
|
|
let filePath2 = url1.appending(path: "projects.txt")
|
|
let data1 = Data(stringtoint.utf8)
|
|
let data2 = Data(output.utf8)
|
|
if (FileManager.default.createFile(atPath: filePath.path(), contents: data1, attributes: nil)) {
|
|
// print("file created successfully")
|
|
}
|
|
else {
|
|
print("something went wrong.")
|
|
}
|
|
if (FileManager.default.createFile(atPath: filePath2.path(), contents: data2, attributes: nil)) {
|
|
// print("file created successfully")
|
|
}
|
|
else {
|
|
print("something went wrong.")
|
|
}
|