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(){
List(VRTNu().shows, id: \.self){ show in
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)){
VStack{
Text(episode.name)
AsyncImage(url: episode.imageurl,placeholder: {
AsyncImage(url: episode.imageURL,placeholder: {
//Image(name: "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 {
let url = episode.getVideo().hlsUrl
let player = AVPlayer(url: url);
VideoPlayer(player: player).fixedSize()
VideoPlayer(player: player).fixedSize().onAppear(){
player.play()
}
};

View File

@ -8,21 +8,25 @@
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 season: Season
let imageURL: URL
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"
print(url)
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
@ -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_info = auth_json as! NSDictionary
print(auth_info)
// no token is returnd but necessary cookies are set
just.post("https://token.vrt.be", json:[
"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 tokendict = tokenjson as! NSDictionary
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
@ -94,22 +101,23 @@ struct Episode: Hashable{
mediaurl = mediaurl + clientid + "@PROD"
print(mediaurl)
let videojson = just.get(mediaurl).json!
print(videojson)
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
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()
return Video(hlsUrl: URL(string: videourl)!,
title: title,
duration: duration)
}()
let imageurl: URL
init(season: Season, episodeName: String){
init(season: Season, episodeName: String, imageURL: URL){
self.name = episodeName
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{
@ -119,33 +127,39 @@ struct Episode: Hashable{
}
func getData(url: URL, regexPattern: String) -> [String]{
let data = Just.get(url).text!
print(data)
func parseData(data: String, regexPattern: String) -> [String]{
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)}))
//let output = Array(Set(matches.map {nsString.substring(with: $0.range)}))
let output = matches.map {nsString.substring(with: $0.range)}
print(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 {
return lhs.seasonUrl == rhs.seasonUrl
return lhs.seasonURL == rhs.seasonURL
}
let title: String
let show: Show
let seasonUrl: URL
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)
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 myepisodes: [Episode]
@ -154,9 +168,11 @@ struct Season: Hashable{
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))
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
@ -168,7 +184,7 @@ struct Season: Hashable{
self.seasonName = seasonName
print(seasonName)
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]{
@ -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 {
return lhs.showUrl == rhs.showUrl
return lhs.showURL == rhs.showURL
}
let showName: String
let showUrl: URL
let showURL: URL
let title: String
let imageURL: URL
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)
let output = parseData(data: Just.get(showURL).text!, regexPattern: regexPattern)
var season: String
var myseasons: [Season]
@ -202,16 +224,17 @@ struct Show: Hashable{
print(showName)
myseasons.append(Season(show: self, seasonName: season, title: season))
}
myseasons.sort()
return myseasons
}()
let imageUrl: URL
init(showName: String, title: String) {
self.showUrl = URL(string: "https://www.vrt.be/vrtnu/a-z/" + showName)!
init(showName: String, title: String, imageURL: URL) {
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")!
self.imageURL = imageURL
}
@ -219,11 +242,6 @@ struct Show: Hashable{
var mutableself = self
return mutableself.seasons
}
func getimageurl() -> URL{
//TODO: stub
return self.imageUrl
}
}
extension NSTextCheckingResult {
@ -243,17 +261,24 @@ struct VRTNu {
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 image: 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))
show = output[i].replacingOccurrences(of: ".relevant", with: "").replacingOccurrences(of: "a href=\"/vrtnu/a-z/", with: "")
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
//print(self.shows)