I am trying to start a project based on web scraping. I have the tools already setup for different platforms for JSON I use SwiftyJSON and for raw HTML I use hpple. My problem is I am trying to setup some generic class for content and some generic class for the fetcher for the content. Since every operation goes like this,
Login If there is username or password supply it. If it has captcha display and use the result Fetch the data using Alamofire Scrape the data either by using JSON or HTML Populate the content class.
I am wondering if there is a way to define some kind of protocol, enum or generic templates so that for each class I can define those different functions. I think if I can’t make this right, I will write the same code over and over again. This is what I have come up with. I will appreciate if you can help me to set this up right.
enum Company:Int {
case CNN
case BBC
case HN
case SO
var captcha:Bool {
switch self {
case CNN:
return false
case BBC:
return true
case HN:
return true
case SO:
return false
}
}
var description:String {
get {
switch self {
case CNN:
return "CNN"
case BBC:
return "BBC"
case HN:
return "Hacker News"
case SO:
return "Stack Overflow"
}
}
}
}
class Fetcher {
var username:String?
var password:String?
var url:String
var company:Company
init(company: Company, url:String) {
self.url = url
self.company = company
}
init(company: Company, url:String,username:String,password:String) {
self.url = url
self.company = company
self.username = username
self.password = password
}
func login() {
if username != nil {
// login
}
if company.captcha {
//show captcha
}
}
func fetch(){
}
func populate() {
}
}
class CNN: Fetcher {
}