-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The findAll
API have something wrong!
#10
Comments
Thanks for reporting the issue. Can you provide a snippet of runnable code so that I can easily reproduce it from Playground? (define a string literal that produces incorrect result, then call findall on it) BTW, using regex on HTML is never a good practice as it is very fragile and unreliable. You should use a XML/HTML parser and it will be a lot easier. You can take a look on my other project https://github.com/cezheng/Fuzi |
There are some 3rd framework in my source code, so I paste the code in this comment directly: //
// SepiTrendingHelper.swift
// Sepicat
//
// Created by Harry Twan on 23/01/2018.
// Copyright © 2018 Desgard_Duan. All rights reserved.
//
import UIKit
import Alamofire
import PySwiftyRegex
import CocoaLumberjack
class SepiTrending: NSObject {
static let shared = SepiTrending()
struct SimpleDevModel {
var login = ""
var name = ""
var avatar_url = ""
var repo = ""
}
enum category {
case repo
case dev
}
struct const {
static let trendingRepoUrl = "https://github.com/trending"
static let trendingDevUrl = "https://github.com/trending/developers"
// Regex
static let trendingDevListRegex = "<li class=\"d-sm-flex flex-justify(.|\n)+?</li>"
static let trendingDevRegex = ".*?\"d-sm-flex flex-justify(.|\n)+?id=\"pa-(.+?)\"(.|\n)+?src=\"(https://avatars.+?)\".+?<span class=\"repo\".+?>(.+?)</span>.+</li>"
}
/// 获取 Trending User 列表
public func fetchDeveloperTrendingList(completion: @escaping (Bool, [SimpleDevModel]?) -> Void) {
guard let url = URL(string: const.trendingDevUrl) else {
DDLogDebug("URL 解析错误")
completion(false, nil)
return
}
Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: GithubManager.header)
.responseData { response in
switch response.result {
case .success(let data):
guard let html = String(data: data, encoding: .utf8) else {
DDLogDebug("Data resolve error")
return
}
let res = self.resolve(html: html, type: .dev)
completion(true, res)
case .failure(let error):
DDLogDebug("Fetch trending error: \(error.localizedDescription)")
completion(false, nil)
}
}
}
/// Resolve String
private func resolve(html: String, type: category) -> [SimpleDevModel] {
let regex = re.compile(const.trendingDevListRegex)
let subRegex = re.compile(const.trendingDevRegex)
let ss = regex.findall(html).map { $0.replacingOccurrences(of: "\n", with: "") }
var res: [SimpleDevModel] = []
for s in ss {
if let m = subRegex.match(s) {
var thisOne = SimpleDevModel()
thisOne.login = m.group(2) ?? ""
thisOne.avatar_url = m.group(4) ?? ""
if let repo = m.group(5) {
thisOne.repo = repo.replacingOccurrences(of: " ", with: "")
}
res.append(thisOne)
}
}
return res
}
} |
Turns out to be the same problem as #5, and Swift 4 migration tool incorrectly dropped the utf16. It should be fixed with 2.0.1. Please confirm. |
@cezheng Yeah! Thanks for the help 😊 |
I use the
trendingDevListRegex
to pattern the Github Trending - Developers Page, and use thetrendingDevRegex
to pattern each one. And every match can be able to lose the first character just like the following screen shot:It was very strange !! 🤣 (The bad network refuse me to upload the image..wait for some time.)
The text was updated successfully, but these errors were encountered: