vrtnu-app/vrtnu/vrtnu/VrtNuLayout.swift

274 lines
11 KiB
Swift

//
// VrtNuLayout.swift
// vrtnu
//
// Created by Jens Timmerman on 25/09/2020.
//
import Foundation
struct Episode: Hashable{
let name: String
let season: Season
lazy var video: Video = {
let just = JustOf<HTTP>()
//`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('<vrtnu username here>', safe='')
password = urllib.parse.quote('<vrtnu password here>', 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(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 show: Show
let seasonUrl: URL
let seasonName: String
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(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{
static func == (lhs: Show, rhs: Show) -> Bool {
return lhs.showUrl == rhs.showUrl
}
let showName: String
let showUrl: URL
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 regexPattern = "value=\"#parsys_container_banner_" + showName + "_(.*)\">"
let output = getData(url: showUrl, regexPattern: regexPattern)
var season: String
var myseasons: [Season]
myseasons = []
for i in 0 ..< output.count{
season = output[i].replacingOccurrences(of: "value=\"#parsys_container_banner_"
+ showName + "_", with: "").replacingOccurrences(of: "\">", with: "")
print(season)
print(showName)
myseasons.append(Season(show: self, seasonName: season, title: season))
}
return myseasons
}()
let imageUrl: URL
init(showName: String, title: String) {
self.showUrl = URL(string: "https://www.vrt.be/vrtnu/a-z/" + showName)!
self.showName = showName
self.title = title
//TODO: get image urls for shows
self.imageUrl = URL(string: "https://images.vrt.be/w320hx/2019/05/02/dbc03914-6cbc-11e9-abcc-02b7b76bf47f.jpg")!
}
func getSeasons() -> [Season]{
var mutableself = self
return mutableself.seasons
}
func getimageurl() -> URL{
//TODO: stub
return self.imageUrl
}
}
extension NSTextCheckingResult {
func groups(testedString:String) -> [String] {
var groups = [String]()
for i in 0 ..< self.numberOfRanges
{
let group = String(testedString[Range(self.range(at: i), in: testedString)!])
groups.append(group)
}
return groups
}
}
struct VRTNu {
let shows: [Show]
init() {
print("init")
let regexPattern = "/a-z/(.*).relevant"
let output = getData(url: URL(string: "https://www.vrt.be/vrtnu/a-z/")!, regexPattern: regexPattern)
var show: String
var myshows: [Show]
myshows = []
for i in 0 ..< output.count{
show = output[i].replacingOccurrences(of: ".relevant", with: "").replacingOccurrences(of: "/a-z/", with: "")
myshows.append(Show(showName: show, title: show))
}
self.shows = myshows
//print(self.shows)
/*self.shows = [
Show(showUrl: URL(string: "a-z/het-peulengaleis/")!, title: "Het Peulengaleis"),
Show(showUrl: URL(string: "https://www.vrt.be/vrtnu/a-z/het-peulengaleis2/")!, title: "Het Peulengaleis 2"),
Show(showUrl: URL(string: "https://www.vrt.be/vrtnu/a-z/het-peulengaleis3/")!, title: "Het Peulengaleis 3"),
Show(showUrl: URL(string: "https://www.vrt.be/vrtnu/a-z/het-peulengaleis4/")!, title: "Het Peulengaleis 4"),
// Show(showUrl: URL(string: "https://www.vrt.be/vrtnu/a-z/het-peulengaleis5/")!, title: "Het Peulengaleis 5"),
//Show(showUrl: URL(string: "https://www.vrt.be/vrtnu/a-z/het-peulengaleis6/")!, title: "Het Peulengaleis 6"),
//Show(showUrl: URL(string: "https://www.vrt.be/vrtnu/a-z/het-peulengaleis7/")!, title: "Het Peulengaleis 7"),
//Show(showUrl: URL(string: "https://www.vrt.be/vrtnu/a-z/het-peulengaleis8/")!, title: "Het Peulengaleis 8"),
]*/
}
}