Skip to content
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

Closed
Desgard opened this issue Jan 23, 2018 · 4 comments
Closed

The findAll API have something wrong! #10

Desgard opened this issue Jan 23, 2018 · 4 comments

Comments

@Desgard
Copy link

Desgard commented Jan 23, 2018

// Regex
static let trendingDevListRegex = "<li class=\"d-sm-flex flex-justify(.|\n)+?</li>"
static let trendingDevRegex = "<?li class=\"d-sm-flex flex-justify(.|\n)+?id=\"pa-(.+?)\"(.|\n)+?</li>"

I use the trendingDevListRegex to pattern the Github Trending - Developers Page, and use the trendingDevRegex to pattern each one. And every match can be able to lose the first character just like the following screen shot:

488fab0e-8c68-4e42-8625-73487299e560

It was very strange !! 🤣 (The bad network refuse me to upload the image..wait for some time.)

@cezheng
Copy link
Owner

cezheng commented Jan 24, 2018

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

@Desgard
Copy link
Author

Desgard commented Jan 24, 2018

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
    }
}

@cezheng
Copy link
Owner

cezheng commented Jan 24, 2018

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.

@Desgard
Copy link
Author

Desgard commented Jan 25, 2018

@cezheng Yeah! Thanks for the help 😊

@Desgard Desgard closed this as completed Jan 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants