From c004962c41d41f85be219ce687a6008630c8a7bf Mon Sep 17 00:00:00 2001 From: Jens Timmerman Date: Sun, 27 Sep 2020 23:30:18 +0200 Subject: [PATCH] created working app with art auth system --- vrtnu/vrtnu/ContentView.swift | 11 +- vrtnu/vrtnu/VrtNuLayout.swift | 191 +++++++++++++++++++++++++++------- 2 files changed, 160 insertions(+), 42 deletions(-) diff --git a/vrtnu/vrtnu/ContentView.swift b/vrtnu/vrtnu/ContentView.swift index 8e836d0..2b8ae9d 100644 --- a/vrtnu/vrtnu/ContentView.swift +++ b/vrtnu/vrtnu/ContentView.swift @@ -41,8 +41,8 @@ struct SeasonView: View { var season: Season var body: some View { NavigationView(){ - List(season.episodes, id:\.self){ episode in - NavigationLink(destination: VideoView(url: episode.video.hlsUrl)){ + List(season.getEpisodes(), id:\.self){ episode in + NavigationLink(destination: VideoView(episode: episode)){ VStack{ Text(episode.name) AsyncImage(url: episode.imageurl,placeholder: { @@ -57,9 +57,11 @@ struct SeasonView: View { } struct VideoView: View { - var url: URL + var episode: Episode + var body: some View { + let url = episode.getVideo().hlsUrl let player = AVPlayer(url: url); VideoPlayer(player: player).fixedSize() }; @@ -68,7 +70,8 @@ struct VideoView: View { } struct ContentView_Previews: PreviewProvider { static var previews: some View { - SeasonView(season: VRTNu().shows[0].getSeasons()[0]) + //SeasonView(season: VRTNu().shows[0].getSeasons()[0]) + ContentView() } } diff --git a/vrtnu/vrtnu/VrtNuLayout.swift b/vrtnu/vrtnu/VrtNuLayout.swift index a509a3b..a74743b 100644 --- a/vrtnu/vrtnu/VrtNuLayout.swift +++ b/vrtnu/vrtnu/VrtNuLayout.swift @@ -10,41 +10,172 @@ import Foundation struct Episode: Hashable{ let name: String - let video: Video + let season: Season + + + lazy var video: Video = { + let just = JustOf() + //`requests.get('https://www.vrt.be/vrtnu/a-z/%s/%s/%s.mssecurevideo.json' % (show, season, episode)).json()` + let url = "https://www.vrt.be/vrtnu/a-z/" + season.show.showName + "/" + season.seasonName + "/" + name + ".mssecurevideo.json" + let msecjson = just.get(url).json! + let mssecdict = msecjson as! NSDictionary + print(msecjson) + print(mssecdict.allValues[0]) + let intmssecdict = mssecdict.allValues[0] as! NSDictionary + print(intmssecdict) + let videoid = intmssecdict.value(forKey: "videoid") as! String + let clientid = intmssecdict.value(forKey: "clientid") as! String + + /* + get login token + ``` + session = requests.session() + username = urllib.parse.quote('', safe='') + password = urllib.parse.quote('', safe='') + auth_data = { + 'APIKey': '3_0Z2HujMtiWq_pkAjgnS2Md2E11a1AwZjYiBETtwNE-EoEHDINgtnvcAOpNgmrVGy', + 'targetEnv': 'jssdk', + 'loginID': username, + 'password': password, + 'authMode': 'cookie', + } + auth_info = requests.post('https://accounts.eu1.gigya.com/accounts.login', data=auth_data).json() + # When requesting a token, no actual token is returned, but the + # necessary cookies are set. + session.post('https://token.vrt.be', + headers={ + 'Content-Type': 'application/json', + 'Referer': 'https://www.vrt.be/vrtnu/', + }, + data=json.dumps({ + 'uid': auth_info['UID'], + 'uidsig': auth_info['UIDSignature'], + 'ts': auth_info['signatureTimestamp'], + 'email': auth_info['profile']['email'], + }).encode('utf-8')) + token = session.post('https://media-services-public.vrt.be/vualto-video-aggregator-web/rest/external/v1/tokens', headers={'Content-Type': 'application/json'}, data=b'').json()['vrtPlayerToken'] + ```*/ + let username = "testuser" + let password = "testpw" + let auth_data = [ + "ApiKey": "3_0Z2HujMtiWq_pkAjgnS2Md2E11a1AwZjYiBETtwNE-EoEHDINgtnvcAOpNgmrVGy", + "targetEnv": "jssdk", + "loginID": username, + "password": password, + "authMode": "cookie", + ] + let auth_json = just.post("https://accounts.eu1.gigya.com/accounts.login", data: auth_data).json! + let auth_info = auth_json as! NSDictionary + // no token is returnd but necessary cookies are set + just.post("https://token.vrt.be", json:[ + "uid": auth_info.value(forKey: "UID"), + "uidsig": auth_info.value(forKey: "UIDSignature"), + "ts": auth_info.value(forKey: "signatureTimestamp"), + //"email": auth_info.value(forKey: "profile"['email'], + "email": username + ], + headers: [ + "Conetnt-Type": "application/json", + "Referer": "https://www.vrt.be/vrtnu/", + ] + + ) + + ///token = session.post('https://media-services-public.vrt.be/vualto-video-aggregator-web/rest/external/v1/tokens', headers={'Content-Type': 'application/json'}, data=b'').json()['vrtPlayerToken'] + + let tokenjson = just.post("https://media-services-public.vrt.be/vualto-video-aggregator-web/rest/external/v1/tokens", headers:["Content-Type": "application/json"]).json! + let tokendict = tokenjson as! NSDictionary + let token = tokendict.value(forKey: "vrtPlayerToken") as! String + + + var mediaurl = "https://media-services-public.vrt.be/vualto-video-aggregator-web/rest/external/v1/videos/" + videoid + mediaurl = mediaurl + "?vrtPlayerToken=" + mediaurl = mediaurl + token + "&client=" + mediaurl = mediaurl + clientid + "@PROD" + print(mediaurl) + let videojson = just.get(mediaurl).json! + let videodict = videojson as! NSDictionary + let duration = videodict.value(forKey: "duration") as! Double + let title = videodict.value(forKey: "title") as! String + let targetURLs = videodict.value(forKey: "targetUrls") as! [NSDictionary] + let videourl = targetURLs[0].value(forKey: "url") as! String + //session.get('https://media-services-public.vrt.be/vualto-video-aggregator-web/rest/external/v1/videos/%s?vrtPlayerToken=%s&client=%s@PROD' %(video_id, token, clientid)).json() + return Video(hlsUrl: URL(string: videourl)!, + title: title, + duration: duration) + }() let imageurl: URL - init(episodeurl: URL){ - self.name = "test" - self.video = Video(hlsUrl: URL(string: "https://remix-cf.lwc.vrtcdn.be/remix/ecd69313-4a39-4297-95b1-aede167725b7/remix.ism/.m3u8")!, - title: "best of dinges", - duration: 2946) + init(season: Season, episodeName: String){ + self.name = episodeName + self.season = season self.imageurl = URL(string: "https://images.vrt.be/w320hx/2019/05/02/dbc03914-6cbc-11e9-abcc-02b7b76bf47f.jpg")! } + + func getVideo() -> Video{ + var lazyself = self + return lazyself.video + } } +func getData(url: URL, regexPattern: String) -> [String]{ + let data = Just.get(url).text! + print(data) + let range = NSRange(location: 0, length: data.count) + let regex = try! NSRegularExpression(pattern: regexPattern) + let matches = regex.matches(in: data, range: range) + let nsString = data as NSString + let output = Array(Set(matches.map {nsString.substring(with: $0.range)})) + print(output) + return output +} + struct Season: Hashable{ static func == (lhs: Season, rhs: Season) -> Bool { return lhs.seasonUrl == rhs.seasonUrl } let title: String - let showName: String - let seasonName: String + let show: Show let seasonUrl: URL - let episodes: [Episode] + let seasonName: String - init(showName: String, seasonName: String, title: String){ - self.showName = showName + lazy var episodes: [Episode] = { + //`set(re.findall('vrtnu/a-z/%s/%s/([^"]*)/' %(show, season),requests.get('https://www.vrt.be/vrtnu/a-z/%s/%s.lists.all-episodes/' % (show, season)).text))` + + let regexPattern = "vrtnu/a-z/" + show.showName + "/" + seasonName + "/([^\"]*)/" + let output = getData(url: URL(string: "https://www.vrt.be/vrtnu/a-z/" + show.showName + "/" + seasonName + ".lists.all-episodes/")!, regexPattern: regexPattern) + + var episode: String + var myepisodes: [Episode] + myepisodes = [] + for i in 0 ..< output.count{ + episode = output[i].replacingOccurrences(of: "vrtnu/a-z/" + show.showName + "/" + seasonName + "/", with: "").replacingOccurrences(of: "/", with: "") + print(episode) + print(seasonName) + myepisodes.append(Episode(season: self, episodeName: episode)) + } + + return myepisodes + + + }() + + init(show: Show, seasonName: String, title: String){ + self.show = show self.title = title self.seasonName = seasonName print(seasonName) - print(showName) - self.seasonUrl = URL(string: "https://www.vrt.be/vrtnu/a-z/" + showName + "/" + seasonName)! - self.episodes = [ - Episode(episodeurl: URL(string: "https://www.vrt.be/vrtnu/a-z/het-peulengaleis/2005-hm/het-peulengaleis-s2005-hma1/")!) - ] + print(show.showName) + self.seasonUrl = URL(string: "https://www.vrt.be/vrtnu/a-z/" + show.showName + "/" + seasonName)! } + + func getEpisodes() -> [Episode]{ + var mutableself = self + return mutableself.episodes + } + } struct Show: Hashable{ @@ -57,18 +188,10 @@ struct Show: Hashable{ let title: String lazy var seasons: [Season] = { //`re.findall('value="#parsys_container_banner_%s_(.*)">' % show, requests.get('https://www.vrt.be/vrtnu/a-z/%s/' % show).text)` - let seasonsdata = Just.get(showUrl).text! - print(seasonsdata) + + let regexPattern = "value=\"#parsys_container_banner_" + showName + "_(.*)\">" + let output = getData(url: showUrl, regexPattern: regexPattern) - let range = NSRange(location: 0, length: seasonsdata.count) - let regex = try! NSRegularExpression(pattern: "value=\"#parsys_container_banner_" - + showName + "_(.*)\">") - let seasons = regex.matches(in: seasonsdata, range: range) - let nsString = seasonsdata as NSString - - let output = Array(Set(seasons.map {nsString.substring(with: $0.range)})) - - print(output) var season: String var myseasons: [Season] myseasons = [] @@ -77,7 +200,7 @@ struct Show: Hashable{ + showName + "_", with: "").replacingOccurrences(of: "\">", with: "") print(season) print(showName) - myseasons.append(Season(showName: showName, seasonName: season, title: season)) + myseasons.append(Season(show: self, seasonName: season, title: season)) } return myseasons }() @@ -120,18 +243,10 @@ struct VRTNu { init() { print("init") - //TODO: fetch shows - let showsdata = Just.get("https://www.vrt.be/vrtnu/a-z/").text! - //print(showsdata) - let range = NSRange(location: 0, length: showsdata.count) + let regexPattern = "/a-z/(.*).relevant" - let regex = try! NSRegularExpression(pattern: "/a-z/(.*).relevant") - let shows = regex.matches(in: showsdata, range: range) - let nsString = showsdata as NSString + let output = getData(url: URL(string: "https://www.vrt.be/vrtnu/a-z/")!, regexPattern: regexPattern) - let output = Array(Set(shows.map {nsString.substring(with: $0.range)})) - - //print(output) var show: String var myshows: [Show] myshows = []