summaryrefslogtreecommitdiff
path: root/plugin.video.watchmojo/addon.py
blob: 29873e98fa2eb7e58223617e047725dc46174c2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# -*- coding: utf-8 -*-
# Copyright: (c) 2016 - 2017 William Forde (willforde+kodi@gmail.com)
#
# License: GPLv2, see LICENSE for more details
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

from __future__ import unicode_literals
from codequick import Route, Resolver, Listitem, run, utils

# Localized string Constants
TAGS = 20459

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


# ###### 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']").tail
    if duration:
        item.info["duration"] = duration.strip(";")

    url = elem.find("a").get("href")
    item.info.date(elem.findtext(".//div[@class='hpdate']").strip(), date_format)
    item.context.container(lbl_tags, tags, url=url)
    item.context.related(related, url=url)
    item.set_callback(play_video, url=url)
    return item


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

@Route.register
def root(plugin):
    """
    Lists all categories and link's to 'Shows', 'MsMojo' and 'All videos'.

    site: http://www.watchmojo.com

    :param Route plugin: Tools related to callback.
    :return: A generator of listitems.
    """
    # Item youtube link as a all videos option
    yield Listitem.youtube("UCaWd5_7JhbQBe4dknZhsHJg")

    url = url_constructor("/")
    source = plugin.request.get(url)

    # Parse only the main category elements
    root_elem = source.parse()
    for elem in root_elem.find(".//div[@id='owl-demo4']").iterfind("div"):
        # Image element contains image url and label as the alt attribute
        img_tag = elem.find("./a/img")

        item = Listitem()
        item.label = img_tag.get("alt")
        item.art["thumb"] = url_constructor(img_tag.get("src"))
        item.set_callback(video_list, url=elem.find("a").get("href").replace("/i/home/", "http://"))
        yield item

    # Parse only the show category elements
    menu_elem = root_elem.find(".//ul[@class='top-ul left']")
    for elem in menu_elem.iterfind(".//a"):
        url = elem.get("href")
        if url and elem.text and (url.startswith("/shows/") or url.startswith("/msmojo/")):
            item = Listitem()
            item.label = elem.text
            item.set_callback(video_list, url=url)
            yield item


@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.
    :return: A generator of listitems.
    """
    url = url_constructor(url)
    source = plugin.request.get(url)
    lbl_tags = plugin.localize(TAGS)

    # Parse all the video elements
    root_elem = source.parse()
    for elem in root_elem.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.
    :return: A generator of listitems.
    """
    url = url_constructor(url)
    source = plugin.request.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: http://www.watchmojo.com/video/id/19268/

    :param Route plugin: Tools related to Route callbacks.
    :param unicode url: The url to a video.
    :return: A generator of listitems.
    """
    url = url_constructor(url)
    source = plugin.request.get(url)

    # Parse all video tags
    root_elem = source.parse("div", attrs={"id": "tags"})
    for elem in root_elem.iterfind("a"):
        item = Listitem()
        item.label = elem.text.title()
        item.set_callback(video_list, url=elem.get("href"))
        yield item


@Resolver.register
def play_video(plugin, url):
    """
    Resolve video url.

    site: http://www.watchmojo.com/video/id/19268/

    :param Resolver plugin: Tools related to Resolver callbacks.
    :param unicode url: The url to a video.
    :return: A playable video url.
    """
    url = url_constructor(url)
    return plugin.extract_source(url)


if __name__ == "__main__":
    run()