scinfu/SwiftSoup

How to get Title and image from Twitter URLs ?

rahulvyas opened this issue · 1 comments

Does anyone knows how we can parse/get twitter URL's ? for example this URL

https://twitter.com/Matt_B4DG3R/status/1338441742249504770?s=20

I'm not able to get anything from this while Facebook parses it properly. Attached Facebook screenshot

Screenshot 2020-12-14 at 6 32 43 PM

Twitter don't get page content immediately, it getting it by JS
You can use hidden webview, improve WKNavigationDelegate, load there twitter URL.

 func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
            webView.evaluateJavaScript("document.getElementsByTagName('html')[0].innerHTML") { (html, error) in
                do {
                    try self.getURL(html)
                } catch {
                    print(error)
                }
            }
            
        }

Of course no one can guarantee that page loaded, so I advice add delay
getURL - function with parameter HTML
It look like this

func getURL(_ innerHTML: Any?) throws {
            guard let htmlString = innerHTML as? String else {
                return
            }
           let doc = try SwiftSoup.parse(htmlString)
// parsing like usual html

        }

Facebook uses Twitter API, not paring