Skip to content

Commit

Permalink
only use youtube from now on
Browse files Browse the repository at this point in the history
  • Loading branch information
willforde committed Nov 9, 2019
1 parent 49a162f commit 04db712
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 276 deletions.
6 changes: 3 additions & 3 deletions plugin.video.watchmojo/addon.xml
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="plugin.video.watchmojo" name="Watchmojo" provider-name="willforde" version="3.0.3">
<addon id="plugin.video.watchmojo" name="Watchmojo" provider-name="willforde" version="3.1.0">
<requires>
<import addon="xbmc.python" version="2.25.0"/>
<import addon="script.module.codequick" version="0.9.2"/>
<import addon="script.module.codequick" version="0.9.10"/>
</requires>
<extension point="xbmc.python.pluginsource" library="addon.py">
<provides>video</provides>
Expand All @@ -24,7 +24,7 @@
<screenshot>resources/screenshots/screenshot001.jpg</screenshot>
<screenshot>resources/screenshots/screenshot002.jpg</screenshot>
</assets>
<news>[fix] Update after site changes.\n [add] Search option.</news>
<news>[upd] Only list the youtube channels from now on, as site now uses "sucuri" to block bots. :(</news>
<reuselanguageinvoker>true</reuselanguageinvoker>
</extension>
</addon>
213 changes: 2 additions & 211 deletions plugin.video.watchmojo/resources/lib/main.py
Expand Up @@ -2,225 +2,16 @@
from __future__ import unicode_literals

# noinspection PyUnresolvedReferences
from codequick import Route, Resolver, Listitem, utils, run
import urlquick
from codequick import Route, Listitem, run

# Localized string Constants
TAGS = 20459
FEATURED_VIDEO = 30001

# Base url constructor
BASE_URL = "https://www.watchmojo.com"
url_constructor = utils.urljoin_partial(BASE_URL)


# ###### Functions ###### #

def extract_videos(lbl_tags, elem, date_format):
item = Listitem()
item.label = elem.findtext(".//div[@class='hptitle']").replace("\t", " ").strip()
item.art["thumb"] = url_constructor(elem.find(".//img").get("src"))

duration = elem.find(".//img[@class='hpplay']")
if duration is not None and duration.tail:
item.info["duration"] = duration.tail.strip(";")

date = elem.findtext(".//div[@class='hpdate']").strip()
if date:
item.info.date(date, date_format)

url = elem.find("a").get("href")
item.context.container(tags, lbl_tags, url=url)
item.context.related(related, url=url)
item.set_callback(play_video, url=url)
return item


# ###### Callbacks ###### #

@Route.register
def root(_):
"""
Lists all categories and link's to 'Shows', 'MsMojo' and 'All videos'.
site: http://www.watchmojo.com
"""
# Add links to watchmojo youtube channels
yield Listitem.youtube("UCaWd5_7JhbQBe4dknZhsHJg") # WatchMojo
yield Listitem.search(search_results)
yield Listitem.youtube("UCaWd5_7JhbQBe4dknZhsHJg", label="WatchMojo")
yield Listitem.youtube("UCMm0YNfHOCA-bvHmOBSx-ZA", label="WatchMojo UK")
yield Listitem.youtube("UC9_eukrzdzY91jjDZm62FXQ", label="MojoTravels")
yield Listitem.youtube("UC4HnC-AS714lT2TCTJ-A1zQ", label="MojoPlays")
yield Listitem.youtube("UC88y_sxutS1mnoeBDroS74w", label="MojoTalks")
yield Listitem.youtube("UC3rLoj87ctEHCcS7BuvIzkQ", label="MsMojo")
yield Listitem.youtube("UCYJyrEdlwxUu7UwtFS6jA_Q", label="UnVeiled")

source = urlquick.get(BASE_URL)
root_elem = source.parse()

# Find each category and fetch the image for the first video in the list
for elem in root_elem.iterfind("./body/section/div[@class='line']"):
# If we have a 'link' tag with class=more, then we are in the right section
tag = elem.find("a[@class='more']")
if tag is not None:
url = tag.get("href")
# Only list category entrys
if url.startswith("/categories"):
item = Listitem()

item.label = elem.find("h2").text.strip().title()
item.art["thumb"] = url_constructor(elem.find(".//img").get("src"))
item.set_callback(video_list, url=url)
yield item

# Add link to exclusive watchmojo video
yield Listitem.from_dict(video_list, "Exclusive", params={"url": "/exclusive/1"},
art={"thumb": "https://www.watchmojo.com/uploads/blipthumbs/Fi-M-Top10-Well-"
"Regarded-Controversial-Films_R7D2Y7-720p30-C_480.jpg"})
# Add Featured Video
# yield Listitem.from_dict(play_featured_video, plugin.localize(FEATURED_VIDEO))
# This will be disabled for now, as watchmojo has disabled it on there site but may enable it again later on


@Route.register
def video_list(plugin, url):
"""
List all video for given url.
site: http://www.watchmojo.com/shows/Top%2010
:param Route plugin: Tools related to Route callbacks.
:param unicode url: The url to a list of videos.
"""
url = url_constructor(url)
source = urlquick.get(url)
lbl_tags = plugin.localize(TAGS)

# Parse all the video elements
root_elem = source.parse()
for line in root_elem.iterfind(".//div[@class='owl-carousel margin-bottom']"):
for elem in line.iterfind(".//div[@class='item']"):
yield extract_videos(lbl_tags, elem, "%b %d, %Y")

# Add link to next page if available
next_page = root_elem.find(".//div[@class='cat-next']")
if next_page is not None:
url = next_page.find("a").get("href")
yield Listitem.next_page(url=url)


@Route.register
def related(plugin, url):
"""
List all related videos to selected video.
site: http://www.watchmojo.com/video/id/19268/
:param Route plugin: Tools related to Route callbacks.
:param unicode url: The url to a video.
"""
url = url_constructor(url)
source = urlquick.get(url)
lbl_tags = plugin.localize(TAGS)

# Parse all the video elements
root_elem = source.parse("div", attrs={"id": "owl-demo1"})
for elem in root_elem.iterfind(".//div[@class='item']"):
yield extract_videos(lbl_tags, elem, "%B %d, %Y")


@Route.register
def tags(plugin, url):
"""
List tags for a video.
site: https://www.watchmojo.com/video/id/19268/
:param Route plugin: Tools related to Route callbacks.
:param unicode url: The url to a video.
"""
plugin.category = plugin.localize(TAGS)
url = url_constructor(url)
source = urlquick.get(url)

# Parse all video tags
root_elem = source.parse("div", attrs={"id": "tags"})
for elem in root_elem.iterfind("a"):
url = elem.get("href")
urlparams = utils.parse_qs(url)
if "q" in urlparams:
search_term = urlparams["q"]

item = Listitem()
item.label = elem.text.title()
item.set_callback(search_results, search_term)
yield item


@Route.register
def search_results(_, search_query):
"""
List search results
:param Route _: Tools related to Route callbacks.
:param search_query: The search term to find results for.
"""
url = url_constructor("/search/search_1018.php?q={}&query=like".format(search_query))
source = urlquick.get(url)
root_elem = source.parse()

for elem in root_elem.iterfind(".//li"):
item = Listitem()
atag = elem.find("a")
item.art["thumb"] = url_constructor(atag.find("img").get("src"))

# Extrac all title segments and join
title = [atag.text]
for i in atag:
title.append(i.text)
title.append(i.tail)
item.label = "".join(filter(None, title))

# Extract plot
plot_node = elem.find("div")
plots = [plot_node.text]
for node in plot_node:
plots.append(node.text)
plots.append(node.tail)
item.info["plot"] = "".join(text.strip() for text in plots if text is not None)

url = atag.get("href")
item.set_callback(play_video, url=url)
yield item


@Resolver.register
def play_video(plugin, url):
"""
Resolve video url.
site: https://www.watchmojo.com/video/id/19268/
:param Resolver plugin: Tools related to Resolver callbacks.
:param unicode url: The url to a video.
"""
url = url_constructor(url)
return plugin.extract_source(url)


# @Resolver.register
def play_featured_video(plugin):
"""
Resolve video url.
site: https://www.watchmojo.com/video/id/19268/
:param Resolver plugin: Tools related to Resolver callbacks.
"""
video_url = play_video(plugin, BASE_URL)
resp = urlquick.get(BASE_URL)
try:
elem = resp.parse("div", attrs={"class": "cal_title"})
except RuntimeError:
return None
else:
title = elem[0].text
if video_url:
# Using xbmcgui.ListItem just for setting plot info
item = __import__("xbmcgui").ListItem(title, path=video_url)
item.setInfo("video", {"title": title, "plot": title})
return item
77 changes: 15 additions & 62 deletions tests/test_addon.py
Expand Up @@ -2,18 +2,9 @@

# Testing specific imports
from codequick import youtube
import codequick
from addon import main as addon

# Check witch version of codequick we are running
framework_version = codequick.__dict__.get("__version__", (0, 9, 0))


class Tester(unittest.TestCase):
def test_root(self):
data = addon.root.test()
self.assertGreaterEqual(len(data), 17)

def test_youtube_channel_watchmojo(self):
data = youtube.Playlist.test("UCaWd5_7JhbQBe4dknZhsHJg")
self.assertGreaterEqual(len(data), 50)
Expand All @@ -22,60 +13,22 @@ def test_youtube_channel_watchmojo_uk(self):
data = youtube.Playlist.test("UCMm0YNfHOCA-bvHmOBSx-ZA")
self.assertGreaterEqual(len(data), 50)

def test_video_list_music(self):
data = addon.video_list.test("http://www.watchmojo.com/categories/Music")
self.assertGreaterEqual(len(data), 40)

def test_video_list_tv(self):
data = addon.video_list.test("http://www.watchmojo.com/categories/TV")
self.assertGreaterEqual(len(data), 40)

def test_video_list_facts(self):
data = addon.video_list.test("/shows/WMFacts")
self.assertGreaterEqual(len(data), 40)

def test_video_list_myths(self):
data = addon.video_list.test("/shows/WMMyths")
self.assertGreaterEqual(len(data), 40)

def test_video_list_news(self):
data = addon.video_list.test("/shows/WMNews")
self.assertGreaterEqual(len(data), 30)

def test_video_list_games(self):
data = addon.video_list.test("/categories/video games")
self.assertGreaterEqual(len(data), 40)

def test_video_list_mojo(self):
data = addon.video_list.test("/msmojo/")
self.assertGreaterEqual(len(data), 40)

def test_video_list_next(self):
data = addon.video_list.test("/categories/music/2")
self.assertGreaterEqual(len(data), 40)

def test_video_list_tags(self):
data = addon.video_list.test("/tag/Film/1")
self.assertGreaterEqual(len(data), 40)

def test_related(self):
data = addon.related.test("http://www.watchmojo.com/video/id/19541/")
self.assertGreaterEqual(len(data), 12)
def test_youtube_channel_mojotravels(self):
data = youtube.Playlist.test("UC9_eukrzdzY91jjDZm62FXQ")
self.assertGreaterEqual(len(data), 50)

def test_tags(self):
data = addon.tags.test("http://www.watchmojo.com/video/id/19541/")
self.assertGreaterEqual(len(data), 5)
def test_youtube_channel_mojoplays(self):
data = youtube.Playlist.test("UC4HnC-AS714lT2TCTJ-A1zQ")
self.assertGreaterEqual(len(data), 50)

def test_search(self):
data = addon.search_results.test(search_query="games")
self.assertGreaterEqual(len(data), 25)
def test_youtube_channel_MojoTalks(self):
data = youtube.Playlist.test("UC88y_sxutS1mnoeBDroS74w")
self.assertGreaterEqual(len(data), 50)

@unittest.skip
def test_play_video_type1(self):
ret = addon.play_video.test(u"https://www.watchmojo.com/video/id/19268/")
self.assertEqual(ret, u"plugin://plugin.video.youtube/play/?video_id=Fi2qpF2q5vk")
def test_youtube_channel_msmojo(self):
data = youtube.Playlist.test("UC3rLoj87ctEHCcS7BuvIzkQ")
self.assertGreaterEqual(len(data), 50)

@unittest.skip
def test_play_video_type2(self):
ret = addon.play_video.test(u"https://www.watchmojo.com/video/id/20838/")
self.assertEqual(ret, u"plugin://plugin.video.youtube/play/?video_id=P3PvFiCibts")
def test_youtube_channel_unveiled(self):
data = youtube.Playlist.test("UCYJyrEdlwxUu7UwtFS6jA_Q")
self.assertGreaterEqual(len(data), 50)

0 comments on commit 04db712

Please sign in to comment.