Ejemplo n.º 1
0
def getProgramsForCategory(url):
  """
  Returns a list of programs for a specific category URL.
  """
  html = getPage(url)

  container = common.parseDOM(html, "div", attrs = { "id" : "[^\"']*playJs-alphabetic-list[^\"']*" })

  if not container:
    helper.errorMsg("Could not find container for URL "+url)
    return None

  articles = common.parseDOM(container, "article", attrs = { "class" : "[^\"']*play_videolist-element[^\"']*" })

  if not articles:
    helper.errorMsg("Could not find program links for URL "+url)
    return None

  programs = []
  for index, article in enumerate(articles):
    url = common.parseDOM(article, "a", ret="href")[0]
    title = common.parseDOM(article, "span", attrs= { "class" : "play_videolist-element__title-text" })[0]
    title = common.replaceHTMLCodes(title)
    thumbnail = common.parseDOM(article, "img", ret="src")[0]
    program = { "title": title, "url": url, "thumbnail": helper.prepareThumb(thumbnail, baseUrl=BASE_URL)}
    programs.append(program)
  return programs
Ejemplo n.º 2
0
def getChannels():
  """
  Returns the live channels from the page "Kanaler".
  """
  anchor_class = "[^\"']*play_zapper__menu-item-link[^\"']*"
  html = getPage(URL_TO_CHANNELS)

  container = common.parseDOM(html, "ul", attrs = { "data-play_tabarea" : "ta-schedule"})
  if not container:
    helper.errorMsg("No container found for channels")
    return None

  channels = []
  ch_boxes = common.parseDOM(container, "li")
  for box in ch_boxes:
    title = common.parseDOM(box, "a", attrs = {"class" : anchor_class}, ret = "data-channel")[0]
    url = common.parseDOM(box, "a", attrs = {"class" : anchor_class}, ret = "href")[0]
    plot = common.parseDOM(box, "span", attrs = {"class" : "[^\"']*play_zapper__menu-item-title[^\"']*"})[0]
    thumbnail = common.parseDOM(box, "a", attrs = {"class" : anchor_class}, ret = "data-thumbnail")[0]
    channels.append({
      "title" : title.title(),
      "url" : url,
      "thumbnail" : helper.prepareThumb(thumbnail, baseUrl=BASE_URL),
      "info" : { "title" : common.replaceHTMLCodes(plot), "plot" : common.replaceHTMLCodes(plot)}
    })

  return channels
Ejemplo n.º 3
0
def getCategories():
  """
  Returns a list of all categories.
  """
  html = getPage("/")


  container = common.parseDOM(html, "div", attrs = { "id": "[^\"']*playJs-categories[^\"']*" })
  if not container:
    helper.errorMsg("Could not find container")
    return None
  articles = common.parseDOM(container, "article")
  if not articles:
    helper.errorMsg("Could not find articles")
    return None
  thumbs = common.parseDOM(container, "img", attrs = { "class": "[^\"']*play_categorylist-element__thumbnail-image[^\"']*" }, ret = "src")
  if not thumbs:
    helper.errorMsg("Could not find thumbnails")
    return None
  categories = []

  for index, article in enumerate(articles):
    category = {}
    category["url"] = common.parseDOM(article, "a", ret = "href")[0]
    title = common.parseDOM(article, "a", ret="title")[0]

    if category["url"].endswith("oppetarkiv"):
      # Skip the "Oppetarkiv" category
      continue

    category["title"] = common.replaceHTMLCodes(title)
    category["thumbnail"] = helper.prepareThumb(thumbs[index], baseUrl=BASE_URL)
    categories.append(category)

  return categories
Ejemplo n.º 4
0
def getEpisodes(title):
  """
  Returns the episodes for a program URL.
  """
  url = BASE_URL+API_URL+"video_title_page;title="+title
  r = requests.get(url)
  if r.status_code != 200:
    common.log("Could not get JSON for "+url)
    return None
  programs = []
  for item in r.json()["relatedVideos"]["episodes"]:
    program = {}
    program["title"] = item["title"]
    try:
      program["title"] = program["title"] + "[COLOR green] (S%sE%s)[/COLOR]" % (str(item["season"]), str(item["episodeNumber"]))
    except KeyError as e:
      # Supress
      pass
    program["url"] = "video/" + str(item["id"])
    program["thumbnail"] = helper.prepareThumb(item.get("thumbnail", ""), BASE_URL)
    info = {}
    info["plot"] = item.get("description", "")
    info["fanart"] = helper.prepareFanart(item.get("poster", ""), BASE_URL)
    program["info"] = info
    programs.append(program)
  return programs
Ejemplo n.º 5
0
def getSearchResultsForList(html, list_id):
  """
  Returns the items in the supplied list.

  Lists are the containers on a program page that contains clips or programs.
  """
  container = common.parseDOM(html, "div", attrs = { "id" : list_id })
  if not container:
    helper.errorMsg("No container found for list ID '"+list_id+"'")
    return None

  articles = common.parseDOM(container, "article")
  if not articles:
    helper.errorMsg("No articles found for list ID '"+list_id+"'")
    return None

  titles = common.parseDOM(container, "article", ret = "data-title")

  results = []
  for index, article in enumerate(articles):
    thumbnail = common.parseDOM(article, "img", attrs = { "class" : "[^\"']*play_videolist-element__thumbnail-image[^\"']*" }, ret = "src")[0]
    url = common.parseDOM(article, "a", ret = "href")[0]
    title = common.replaceHTMLCodes(titles[index])
    thumbnail = helper.prepareThumb(thumbnail, baseUrl=BASE_URL)

    item_type = "video"
    if list_id == SEARCH_LIST_TITLES:
      item_type = "program"
    results.append({"item": { "title" : title, "thumbnail" : thumbnail, "url" : url  }, "type" : item_type })

  return results
Ejemplo n.º 6
0
def getLatestNews():
  """
  Returns a list of latest news programs.
  """
  url = BASE_URL+API_URL+"cluster_latest;cluster=nyheter"
  r = requests.get(url)
  if r.status_code != 200:
    common.log("Could not get JSON for url: "+url)
    return None

  programs = []
  for item in r.json():
    live_str = ""
    thumbnail = item.get("poster", "")
    if not thumbnail:
      thumbnail = item.get("thumbnail", "")
    if item["broadcastedNow"]:
      live_str = " " + "[COLOR red](Live)[/COLOR]"
    program = {
        "title" : common.replaceHTMLCodes(item["programTitle"] + " " + item["title"] + live_str),
        "thumbnail" : helper.prepareThumb(thumbnail, baseUrl=BASE_URL),
        "url" : "video/" + str(item["versions"][0]["articleId"])
        }
    programs.append(program)
  return programs
Ejemplo n.º 7
0
def getSearchResult(query, page):
  """
  Returns the search results from a search query.

  Return result is a list of article objects.
  """
  page = getPage(URL_TO_SEARCH + query + URL_TO_SEARCH_PAGE + str(page))

  container = common.parseDOM(page, "div", attrs = { "class": "[^\"']*svtoa-js-searchlist[^\"']*" })
  if not container:
    common.log("Could not find svtjs-toogle-group!")
    return None

  articles = common.parseDOM(container, "article")
  if not articles:
    common.log("No articles could be found!")
    return None
  
  newarticles = []
  for article in articles:
    newarticle = {}
    newarticle["url"] = common.parseDOM(article, "a", ret = "href")[0]
    thumbnail = common.parseDOM(article, "img", ret = "src")[0]
    thumbnail = helper.prepareThumb(thumbnail)
    newarticle["thumbnail"] = thumbnail
    title = common.parseDOM(article, "h2")[0]
    title = common.parseDOM(title, "a")[0]
    title = common.replaceHTMLCodes(title)
    newarticle["title"] = title
    newarticle["info"] = { "title": title }
    newarticles.append(newarticle)

  return newarticles
Ejemplo n.º 8
0
def getSearchResults(search_term):
  """
  Returns a list of both clips and programs
  for the supplied search URL.
  """
  url = BASE_URL+API_URL+"search_page;q="+search_term
  r = requests.get(url)
  if r.status_code != 200:
    common.log("Did not get any response for: "+url)
    return None

  items = []
  contents = r.json()

  for program in contents["titles"]:
    item = {}
    item["title"] = common.replaceHTMLCodes(program["title"])
    item["url"] = program["contentUrl"]
    item["thumbnail"] = helper.prepareThumb(program.get("imageMedium", ""), baseUrl=BASE_URL)
    item["info"] = {}
    item["info"]["plot"] = program.get("description", "")
    items.append({"item": item, "type" : "program"})

  for video in contents["episodes"]:
    item = {}
    item["title"] = common.replaceHTMLCodes(video["title"])
    item["url"] = video["contentUrl"]
    item["thumbnail"] = helper.prepareThumb(video.get("imageMedium", ""), baseUrl=BASE_URL)
    item["info"] = {}
    item["info"]["plot"] = video.get("description", "")
    items.append({"item": item, "type": "video"})

  for clip in contents["clips"]:
    item = {}
    item["title"] = common.replaceHTMLCodes(clip["title"])
    item["url"] = clip["contentUrl"]
    item["thumbnail"] = helper.prepareThumb(clip.get("imageMedium", ""), baseUrl=BASE_URL)
    item["info"] = {}
    item["info"]["plot"] = clip.get("description", "")
    items.append({"item": item, "type": "video"})

  return items
Ejemplo n.º 9
0
def getProgramsForGenre(genre):
  """
  Returns a list of all programs for a genre.
  """
  url = BASE_URL+API_URL+"cluster_titles_and_episodes/?cluster="+genre
  r = requests.get(url)
  if r.status_code != 200:
    common.log("Could not get JSON for url: "+url)
    return None

  programs = []
  for item in r.json():
    url = item["contentUrl"]
    title = item["programTitle"]
    plot = item.get("description", "")
    thumbnail = helper.prepareThumb(item.get("thumbnail", ""), BASE_URL)
    if not thumbnail:
      thumbnail = helper.prepareThumb(item.get("poster", ""), BASE_URL)
    info = {"plot": plot, "thumbnail": thumbnail, "fanart": thumbnail}
    program = { "title": title, "url": url, "thumbnail": thumbnail, "info": info}
    programs.append(program)
  return programs
Ejemplo n.º 10
0
def getArticles(url,page,tabname=None):
  """
  Fetches all "article" DOM elements in a "svtGridBlock" or
  tab and returns a list of article 'objects'.  
  """
  if page:
    pageurl = url + "sida=" + page
  else:
    pageurl = url 
  
  common.log("pageurl: " , pageurl)
  html = getPage(pageurl)

  
  container = common.parseDOM(html,
                "div",
                attrs = { "class": "[^\"']*svtGridBlock[^\"']*" })[0]
  

  articles = common.parseDOM(container, "article")
  newarticles = []
 
  for index,article in enumerate(articles):
    info = {}
    newarticle = {}

    title = common.parseDOM(article,"h3")[0]
    title = common.parseDOM(title,"a")[0]
    newarticle["url"] = common.parseDOM(article, "a",
                            attrs = { "class": "[^\"']*[playLink|playAltLink][^\"']*" },
                            ret = "href")[0]
    thumbnail = common.parseDOM(article,
                                "img",
                                attrs = { "class": "oaImg" },
                                ret = "src")[0]
    newarticle["thumbnail"] = helper.prepareThumb(thumbnail)
    
    title = common.replaceHTMLCodes(title)
    newarticle["title"] = title
    info["title"] = title

    newarticle["info"] = info
    newarticles.append(newarticle)
 
  return newarticles
Ejemplo n.º 11
0
def getClips(title):
  """
  Returns the clips for a program URL.
  """
  url = BASE_URL+API_URL+"video_title_page;title="+title
  r = requests.get(url)
  if r.status_code != 200:
    common.log("Could not get JSON for "+url)
    return None
  clips = []
  for item in r.json()["relatedVideos"]["clipsResult"]["entries"]:
    clip = {}
    clip["title"] = item["title"]
    clip["url"] = "klipp/" + str(item["id"])
    clip["thumbnail"] = helper.prepareThumb(item.get("thumbnailMedium", ""), BASE_URL)
    info = {}
    info["plot"] = item.get("description", "")
    clip["info"] = info
    clips.append(clip)
  return clips
Ejemplo n.º 12
0
def getItems(section_name, page):
  if not page:
    page = 1
  url = BASE_URL+API_URL+section_name+"_page?page="+str(page)
  r = requests.get(url)
  if r.status_code != 200:
    common.log("Did not get any response for: "+url)
    return None

  returned_items = []
  contents = r.json()
  for video in contents["videos"]:
    item = {}
    item["title"] = video["programTitle"]
    item["url"] = video["contentUrl"]
    item["thumbnail"] = helper.prepareThumb(video.get("thumbnail", ""), baseUrl=BASE_URL)
    info = {}
    info["title"] = item["title"]
    try:
      info["plot"] = video["description"]
    except KeyError:
      # Some videos do not have description (Rapport etc)
      info["plot"] = ""
    info["aired"] = video["broadcastDate"]
    try:
      info["duration"] = video["materialLength"]
    except KeyError:
      # Some programs are missing duration, default to 0
      info["duration"] = 0
    try:
      info["fanart"] = helper.prepareFanart(video["poster"], baseUrl=BASE_URL)
    except KeyError:
      pass
    item["info"] = info
    returned_items.append(item)

  return (returned_items, contents["paginationData"]["totalPages"] > contents["paginationData"]["currentPage"])
Ejemplo n.º 13
0
def getProgramItems(section_name, url=None):
  """
  Returns a list of program items for a show.
  Program items have 'title', 'thumbnail', 'url' and 'info' keys.
  """
  if not url:
    url = "/"
  html = getPage(url + "?sida=2")

  video_list_class = "[^\"']*play_videolist[^\"']*"

  container = common.parseDOM(html, "div", attrs = { "id" : section_name })
  if not container:
    helper.errorMsg("No container found for section "+section_name+"!")
    return None
  container = container[0]

  item_class = "[^\"']*play_vertical-list__item[^\"']*"
  items = common.parseDOM(container, "li", attrs = { "class" : item_class })
  if not items:
    helper.errorMsg("No items found in container \""+section_name+"\"")
    return None
  new_articles = []


  for index, item in enumerate(items):
    live_item = False
    if "play_live-countdown" in item:
      live_item = True
      helper.infoMsg("Skipping live item!")
      continue
    info = {}
    new_article = {}
    title = common.parseDOM(item, "a",
                            attrs = { "class" : "[^\"']*play_vertical-list__header-link[^\"']*" })[0]
    plot = common.parseDOM(item, "p",
                            attrs = { "class" : "[^\"']*play_vertical-list__description-text[^\"']*" })[0]
    new_article["url"] = common.parseDOM(item, "a",
                            attrs = { "class": "[^\"']*play_vertical-list__header-link[^\"']*" },
                            ret = "href")[0]
    thumbnail = common.parseDOM(item,
                                "img",
                                attrs = { "class": "[^\"']*play_vertical-list__image[^\"']*" },
                                ret = "src")[0]
    new_article["thumbnail"] = helper.prepareThumb(thumbnail, baseUrl=BASE_URL)
    duration = common.parseDOM(item, "time", attrs = {}, )[0]
    aired = common.parseDOM(item, "p", attrs = { "class" : "[^\"']*play_vertical-list__meta-info[^\"']*" })
    if aired:
      aired = aired[0].replace("Publicerades ", "")
    else:
      # Some items does not contain this meta data
      aired = ""

    plot = common.replaceHTMLCodes(plot)
    new_article["title"] = title
    info["title"] = title
    info["plot"] = plot
    info["aired"] = helper.convertDate(aired) 
    info["duration"] = helper.convertDuration(duration)
    info["fanart"] = helper.prepareFanart(thumbnail, baseUrl=BASE_URL)
    new_article["info"] = info
    new_articles.append(new_article)

  return new_articles
Ejemplo n.º 14
0
def getArticles(section_name, url=None):
  """
  Returns a list of the articles in a section as program items.

  Program items have 'title', 'thumbnail', 'url' and 'info' keys.
  """
  if not url:
    url = "/"
  html = getPage(url)

  video_list_class = "[^\"']*play_videolist[^\"']*"

  container = common.parseDOM(html, "div", attrs = { "class" : video_list_class, "id" : section_name })
  if not container:
    helper.errorMsg("No container found for section "+section_name+"!")
    return None
  container = container[0]

  article_class = "[^\"']*play_videolist-element[^\"']*"
  articles = common.parseDOM(container, "article", attrs = { "class" : article_class })
  titles = common.parseDOM(container, "article", attrs = { "class" : article_class }, ret = "data-title")
  plots = common.parseDOM(container, "article", attrs = { "class" : article_class }, ret = "data-description")
  airtimes = common.parseDOM(container, "article", attrs = { "class" : article_class }, ret = "data-broadcasted")
  if section_name == SECTION_LATEST_CLIPS:
    airtimes = common.parseDOM(container, "article", attrs = { "class" : article_class }, ret = "data-published")
  durations = common.parseDOM(container, "article", attrs = { "class" : article_class }, ret = "data-length")
  new_articles = []

  if not articles:
    helper.errorMsg("No articles found for section '"+section_name+"' !")
    return None

  for index, article in enumerate(articles):
    info = {}
    new_article = {}
    plot = plots[index]
    aired = airtimes[index]
    duration = durations[index]
    title = titles[index]
    new_article["url"] = common.parseDOM(article, "a",
                            attrs = { "class": "[^\"']*play_videolist-element__link[^\"']*" },
                            ret = "href")[0]
    thumbnail = common.parseDOM(article,
                                "img",
                                attrs = { "class": "[^\"']*play_videolist-element__thumbnail-image[^\"']*" },
                                ret = "src")[0]
    new_article["thumbnail"] = helper.prepareThumb(thumbnail, baseUrl=BASE_URL)
    if section_name == SECTION_LIVE_PROGRAMS:
      notlive = common.parseDOM(article, "span", attrs = {"class": "[^\"']*play_graphics-live[^\"']*is-inactive[^\"']*"})
      if notlive:
        new_article["live"] = False
      else:
        new_article["live"] = True
    title = common.replaceHTMLCodes(title)
    plot = common.replaceHTMLCodes(plot)
    new_article["title"] = title
    info["title"] = title
    info["plot"] = plot
    info["aired"] = helper.convertDate(aired)
    info["duration"] = helper.convertDuration(duration)
    info["fanart"] = helper.prepareFanart(thumbnail, baseUrl=BASE_URL)
    new_article["info"] = info
    new_articles.append(new_article)

  return new_articles