added show and episode images.

This commit is contained in:
Jens Timmerman 2020-09-28 02:02:57 +02:00
parent c004962c41
commit ad2d485da9
2 changed files with 71 additions and 38 deletions

View File

@ -16,7 +16,13 @@ struct ContentView: View {
NavigationView(){ NavigationView(){
List(VRTNu().shows, id: \.self){ show in List(VRTNu().shows, id: \.self){ show in
NavigationLink(destination: ShowView(show: show)){ NavigationLink(destination: ShowView(show: show)){
Text(show.title) HStack{
AsyncImage(url: show.imageURL,placeholder: {
//Image(name: "loading")
Text("Loading...")
}, image:{Image(uiImage:$0).resizable()}).frame(width: 200, height:200)
Text(show.title)
}
} }
} }
} }
@ -45,10 +51,10 @@ struct SeasonView: View {
NavigationLink(destination: VideoView(episode: episode)){ NavigationLink(destination: VideoView(episode: episode)){
VStack{ VStack{
Text(episode.name) Text(episode.name)
AsyncImage(url: episode.imageurl,placeholder: { AsyncImage(url: episode.imageURL,placeholder: {
//Image(name: "loading") //Image(name: "loading")
Text("Loading...") Text("Loading...")
},image:{Image(uiImage:$0)}) },image:{Image(uiImage:$0).resizable()}).frame(width: 300, height: 300)
} }
} }
} }
@ -63,7 +69,9 @@ struct VideoView: View {
var body: some View { var body: some View {
let url = episode.getVideo().hlsUrl let url = episode.getVideo().hlsUrl
let player = AVPlayer(url: url); let player = AVPlayer(url: url);
VideoPlayer(player: player).fixedSize() VideoPlayer(player: player).fixedSize().onAppear(){
player.play()
}
}; };

View File

@ -8,21 +8,25 @@
import Foundation import Foundation
struct Episode: Hashable{ struct Episode: Hashable, Comparable{
static func < (lhs: Episode, rhs: Episode) -> Bool {
return lhs.name < rhs.name
}
let name: String let name: String
let season: Season let season: Season
let imageURL: URL
lazy var video: Video = { lazy var video: Video = {
let just = JustOf<HTTP>() let just = JustOf<HTTP>()
//`requests.get('https://www.vrt.be/vrtnu/a-z/%s/%s/%s.mssecurevideo.json' % (show, season, episode)).json()` //`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 url = "https://www.vrt.be/vrtnu/a-z/" + season.show.showName + "/" + season.seasonName + "/" + name + ".mssecurevideo.json"
print(url)
let msecjson = just.get(url).json! let msecjson = just.get(url).json!
let mssecdict = msecjson as! NSDictionary let mssecdict = msecjson as! NSDictionary
print(msecjson) print(msecjson)
print(mssecdict.allValues[0])
let intmssecdict = mssecdict.allValues[0] as! NSDictionary let intmssecdict = mssecdict.allValues[0] as! NSDictionary
print(intmssecdict)
let videoid = intmssecdict.value(forKey: "videoid") as! String let videoid = intmssecdict.value(forKey: "videoid") as! String
let clientid = intmssecdict.value(forKey: "clientid") as! String let clientid = intmssecdict.value(forKey: "clientid") as! String
@ -66,6 +70,8 @@ struct Episode: Hashable{
] ]
let auth_json = just.post("https://accounts.eu1.gigya.com/accounts.login", data: auth_data).json! let auth_json = just.post("https://accounts.eu1.gigya.com/accounts.login", data: auth_data).json!
let auth_info = auth_json as! NSDictionary let auth_info = auth_json as! NSDictionary
print(auth_info)
// no token is returnd but necessary cookies are set // no token is returnd but necessary cookies are set
just.post("https://token.vrt.be", json:[ just.post("https://token.vrt.be", json:[
"uid": auth_info.value(forKey: "UID"), "uid": auth_info.value(forKey: "UID"),
@ -86,6 +92,7 @@ struct Episode: Hashable{
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 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 tokendict = tokenjson as! NSDictionary
let token = tokendict.value(forKey: "vrtPlayerToken") as! String let token = tokendict.value(forKey: "vrtPlayerToken") as! String
print(token)
var mediaurl = "https://media-services-public.vrt.be/vualto-video-aggregator-web/rest/external/v1/videos/" + videoid var mediaurl = "https://media-services-public.vrt.be/vualto-video-aggregator-web/rest/external/v1/videos/" + videoid
@ -94,22 +101,23 @@ struct Episode: Hashable{
mediaurl = mediaurl + clientid + "@PROD" mediaurl = mediaurl + clientid + "@PROD"
print(mediaurl) print(mediaurl)
let videojson = just.get(mediaurl).json! let videojson = just.get(mediaurl).json!
print(videojson)
let videodict = videojson as! NSDictionary let videodict = videojson as! NSDictionary
let duration = videodict.value(forKey: "duration") as! Double let duration = videodict.value(forKey: "duration") as! Double
let title = videodict.value(forKey: "title") as! String let title = videodict.value(forKey: "title") as! String
let targetURLs = videodict.value(forKey: "targetUrls") as! [NSDictionary] let targetURLs = videodict.value(forKey: "targetUrls") as! [NSDictionary]
let videourl = targetURLs[0].value(forKey: "url") as! String let videourl = targetURLs[0].value(forKey: "url") as! String
print(videourl)
//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() //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)!, return Video(hlsUrl: URL(string: videourl)!,
title: title, title: title,
duration: duration) duration: duration)
}() }()
let imageurl: URL
init(season: Season, episodeName: String){ init(season: Season, episodeName: String, imageURL: URL){
self.name = episodeName self.name = episodeName
self.season = season self.season = season
self.imageurl = URL(string: "https://images.vrt.be/w320hx/2019/05/02/dbc03914-6cbc-11e9-abcc-02b7b76bf47f.jpg")! self.imageURL = imageURL
} }
func getVideo() -> Video{ func getVideo() -> Video{
@ -119,33 +127,39 @@ struct Episode: Hashable{
} }
func getData(url: URL, regexPattern: String) -> [String]{ func parseData(data: String, regexPattern: String) -> [String]{
let data = Just.get(url).text!
print(data)
let range = NSRange(location: 0, length: data.count) let range = NSRange(location: 0, length: data.count)
let regex = try! NSRegularExpression(pattern: regexPattern) let regex = try! NSRegularExpression(pattern: regexPattern)
let matches = regex.matches(in: data, range: range) let matches = regex.matches(in: data, range: range)
let nsString = data as NSString let nsString = data as NSString
let output = Array(Set(matches.map {nsString.substring(with: $0.range)})) //let output = Array(Set(matches.map {nsString.substring(with: $0.range)}))
let output = matches.map {nsString.substring(with: $0.range)}
print(output) print(output)
return output return output
} }
struct Season: Hashable{ struct Season: Hashable, Comparable{
static func < (lhs: Season, rhs: Season) -> Bool {
return lhs.seasonName < rhs.seasonName
}
static func == (lhs: Season, rhs: Season) -> Bool { static func == (lhs: Season, rhs: Season) -> Bool {
return lhs.seasonUrl == rhs.seasonUrl return lhs.seasonURL == rhs.seasonURL
} }
let title: String let title: String
let show: Show let show: Show
let seasonUrl: URL let seasonURL: URL
let seasonName: String let seasonName: String
lazy var episodes: [Episode] = { 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))` //`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 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) let imageregexPattern = "data-responsive-image=\".*(jpg|png)"
let data = Just.get("https://www.vrt.be/vrtnu/a-z/" + show.showName + "/" + seasonName + ".lists.all-episodes/").text!
let output = Array(Set(parseData(data: data, regexPattern: regexPattern)))
let imageoutput = parseData(data: data, regexPattern: imageregexPattern)
var episode: String var episode: String
var myepisodes: [Episode] var myepisodes: [Episode]
@ -154,9 +168,11 @@ struct Season: Hashable{
episode = output[i].replacingOccurrences(of: "vrtnu/a-z/" + show.showName + "/" + seasonName + "/", with: "").replacingOccurrences(of: "/", with: "") episode = output[i].replacingOccurrences(of: "vrtnu/a-z/" + show.showName + "/" + seasonName + "/", with: "").replacingOccurrences(of: "/", with: "")
print(episode) print(episode)
print(seasonName) print(seasonName)
myepisodes.append(Episode(season: self, episodeName: episode)) let image = URL(string: imageoutput[i].replacingOccurrences(of: "data-responsive-image=\"", with: "https:"))!
myepisodes.append(Episode(season: self, episodeName: episode, imageURL: image))
} }
myepisodes.sort()
return myepisodes return myepisodes
@ -168,7 +184,7 @@ struct Season: Hashable{
self.seasonName = seasonName self.seasonName = seasonName
print(seasonName) print(seasonName)
print(show.showName) print(show.showName)
self.seasonUrl = URL(string: "https://www.vrt.be/vrtnu/a-z/" + show.showName + "/" + seasonName)! self.seasonURL = URL(string: "https://www.vrt.be/vrtnu/a-z/" + show.showName + "/" + seasonName)!
} }
func getEpisodes() -> [Episode]{ func getEpisodes() -> [Episode]{
@ -178,19 +194,25 @@ struct Season: Hashable{
} }
struct Show: Hashable{ struct Show: Hashable, Comparable{
static func < (lhs: Show, rhs: Show) -> Bool {
return lhs.showName < rhs.showName
}
static func == (lhs: Show, rhs: Show) -> Bool { static func == (lhs: Show, rhs: Show) -> Bool {
return lhs.showUrl == rhs.showUrl return lhs.showURL == rhs.showURL
} }
let showName: String let showName: String
let showUrl: URL let showURL: URL
let title: String let title: String
let imageURL: URL
lazy var seasons: [Season] = { lazy var seasons: [Season] = {
//`re.findall('value="#parsys_container_banner_%s_(.*)">' % show, requests.get('https://www.vrt.be/vrtnu/a-z/%s/' % show).text)` //`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 regexPattern = "value=\"#parsys_container_banner_" + showName + "_(.*)\">"
let output = getData(url: showUrl, regexPattern: regexPattern) let output = parseData(data: Just.get(showURL).text!, regexPattern: regexPattern)
var season: String var season: String
var myseasons: [Season] var myseasons: [Season]
@ -202,16 +224,17 @@ struct Show: Hashable{
print(showName) print(showName)
myseasons.append(Season(show: self, seasonName: season, title: season)) myseasons.append(Season(show: self, seasonName: season, title: season))
} }
myseasons.sort()
return myseasons return myseasons
}() }()
let imageUrl: URL
init(showName: String, title: String) { init(showName: String, title: String, imageURL: URL) {
self.showUrl = URL(string: "https://www.vrt.be/vrtnu/a-z/" + showName)! self.showURL = URL(string: "https://www.vrt.be/vrtnu/a-z/" + showName)!
self.showName = showName self.showName = showName
self.title = title self.title = title
//TODO: get image urls for shows //TODO: get image urls for shows
self.imageUrl = URL(string: "https://images.vrt.be/w320hx/2019/05/02/dbc03914-6cbc-11e9-abcc-02b7b76bf47f.jpg")! self.imageURL = imageURL
} }
@ -219,11 +242,6 @@ struct Show: Hashable{
var mutableself = self var mutableself = self
return mutableself.seasons return mutableself.seasons
} }
func getimageurl() -> URL{
//TODO: stub
return self.imageUrl
}
} }
extension NSTextCheckingResult { extension NSTextCheckingResult {
@ -243,17 +261,24 @@ struct VRTNu {
init() { init() {
print("init") print("init")
let regexPattern = "/a-z/(.*).relevant" let regexPattern = "a href=\"/vrtnu/a-z/(.*).relevant"
let imageregexPattern = "data-responsive-image=\".*(jpg|png)"
let data = Just.get("https://www.vrt.be/vrtnu/a-z/").text!
print(data)
let output = getData(url: URL(string: "https://www.vrt.be/vrtnu/a-z/")!, regexPattern: regexPattern) let output = parseData(data: data, regexPattern: regexPattern)
let imageoutput = parseData(data: data, regexPattern: imageregexPattern)
var show: String var show: String
var image: String
var myshows: [Show] var myshows: [Show]
myshows = [] myshows = []
for i in 0 ..< output.count{ for i in 0 ..< output.count{
show = output[i].replacingOccurrences(of: ".relevant", with: "").replacingOccurrences(of: "/a-z/", with: "") show = output[i].replacingOccurrences(of: ".relevant", with: "").replacingOccurrences(of: "a href=\"/vrtnu/a-z/", with: "")
myshows.append(Show(showName: show, title: show)) image = imageoutput[i].replacingOccurrences(of: "data-responsive-image=\"", with: "https:")
myshows.append(Show(showName: show, title: show, imageURL: URL(string: image)!))
} }
myshows.sort()
self.shows = myshows self.shows = myshows
//print(self.shows) //print(self.shows)