vrtnu-app/vrtnu/vrtnu/VrtNuLayout.swift

159 lines
5.5 KiB
Swift

//
// VrtNuLayout.swift
// vrtnu
//
// Created by Jens Timmerman on 25/09/2020.
//
import Foundation
struct Episode: Hashable{
let name: String
let video: Video
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)
self.imageurl = URL(string: "https://images.vrt.be/w320hx/2019/05/02/dbc03914-6cbc-11e9-abcc-02b7b76bf47f.jpg")!
}
}
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 seasonUrl: URL
let episodes: [Episode]
init(showName: String, seasonName: String, title: String){
self.showName = showName
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/")!)
]
}
}
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 seasonsdata = Just.get(showUrl).text!
print(seasonsdata)
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 = []
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(showName: showName, 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")
//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 regex = try! NSRegularExpression(pattern: "/a-z/(.*).relevant")
let shows = regex.matches(in: showsdata, range: range)
let nsString = showsdata as NSString
let output = Array(Set(shows.map {nsString.substring(with: $0.range)}))
//print(output)
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"),
]*/
}
}