Get Image Thumbnails from Rumble?

I want to get a thumbnail image for videos from Rumble.

When getting images from Youtube I just do like this:


Want to same from Rumble video url-


4

1 Answer

I checked the rumble webpage of the link you provided. I am not sure if there is a smarter/faster way but here is a way to get the thumbnailUrl from their html code.

func getThumbnailFromRumble() {
        let url = URL(string:"")!
        URLSession.shared.dataTask(with: url){ data, response, error in
                guard error == nil else { return }
                guard let httpURLResponse = response as? HTTPURLResponse,
                      httpURLResponse.statusCode == 200,
                      let data = data else {
                    return
                }
                
            let str = String(data: data, encoding: .utf8) // get the htm response as string
            let prefix = "\"thumbnailUrl\":" 
            let suffix = ",\"uploadDate\""   
            
            let matches = str?.match("(\(prefix)(...*)\(suffix))")
            if let thumbnailUrlMatch = matches?.first?.first {
                let thumbnailUrl = thumbnailUrlMatch
                    .replacingOccurrences(of: prefix, with: "") // remove prefix from urlstring
                    .replacingOccurrences(of: suffix, with: "") // remove suffix from urlstring
                    .replacingOccurrences(of: "\"", with: "") // remove escaping characters from urlstring
                
                if let url = URL(string: thumbnailUrl),
                   let data = try? Data(contentsOf: url) {
                    
                     let uiImage = UIImage(data: data)
                     // use the uiimage        
                }
            }
            
        }.resume()
    }

I use this extension to get the necessary string part from the html response

extension String {
    func match(_ regex: String) -> [[String]] {
        let nsString = self as NSString
        return (try? NSRegularExpression(pattern: regex, options: []))?.matches(in: self, options: [], range: NSMakeRange(0, nsString.length)).map { match in
            (0..<match.numberOfRanges).map { match.range(at: $0).location == NSNotFound ? "" : nsString.substring(with: match.range(at: $0)) }
        } ?? []
    }
}

Feel free to improve the code.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest