summaryrefslogtreecommitdiff
path: root/plugin.video.johnlocker/addon.py
blob: ec446a2bd731d170c70c478925459421100a0553 (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
# -*- coding: utf-8 -*-
# Copyright: (c) 2016 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

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


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

    site: http://johnlocker.com

    :param Route plugin: Tools related to callback.
    :return: A generator of listitems.
    """
    url = url_constructor("/us")
    source = plugin.request.get(url)

    # Add link to recent videos
    yield Listitem.recent(recent)

    # Parse the categories
    root_elem = source.parse("ul", attrs={"id": "menu-category-items primary-menu"})
    for elem in root_elem.iterfind("./li/a"):
        item = Listitem()
        item.label = elem.text
        item.set_callback(video_list, url=elem.get("href"))
        yield item


@Route.register
def recent(plugin):
    """
    List all recent videos from the rss feed.

    :param Route plugin: Tools related to Route callbacks.
    :return: A generator of listitems.
    """
    url = "http://feeds.feedburner.com/johnlockerv3"
    source = plugin.request.get(url)

    # Pars all the videos
    root_elem = source.xml()
    for elem in root_elem.iterfind("./channel/item"):
        item = Listitem()

        date = elem.findtext("pubDate")
        item.info.date(date[:date.find("+") - 1], "%a, %d %b %Y %H:%M:%S")
        item.info["genre"] = elem.findall("category")[0].text

        # Split title into label and plot
        title, _, plot = elem.findtext("title").partition("\u2013")
        item.info["plot"] = plot.split("(", 1)[0].strip()
        item.label = title.split("(", 1)[0].strip()

        item.set_callback(play_video, url=elem.findtext("comments"))
        yield item


@Route.register
def video_list(plugin, url):
    """
    List all videos for given url.

    site: http://johnlocker.com/category/science-tech/

    :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)

    # Parse video content
    root_elem = source.parse("main", attrs={"id": "main"})
    for elem in root_elem.iterfind("./div/article"):
        a_tag = elem.find(".//a[@rel='bookmark']")
        item = Listitem()
        item.label = a_tag.text.split("(", 1)[0].strip()
        url = a_tag.get("href")

        date = elem.find(".//time[@datetime]").get("datetime")
        item.info.date(date[:date.find("+")], "%Y-%m-%dT%H:%M:%S")
        item.info["count"] = elem.find(".//span[@class='entry-view']").text.split(" ", 1)[0].replace(",", "")

        # Extract video source url using post id to help with search
        post_id = elem.get("id").split("-", 1)[-1]
        iframe_node = elem.find("./div[@id='entry-video-%s']//iframe" % post_id)
        if iframe_node is not None:
            url = iframe_node.get("src")

        thumb = elem.find("./a/img")
        if thumb is not None:
            item.art["thumb"] = thumb.get("src")

        item.set_callback(play_video, url=url)
        yield item

    next_page = root_elem.find(".//nav/a[@class='next page-numbers']")
    if next_page is not None:
        yield Listitem.next_page(url=next_page.get("href"))


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

    site: http://johnlocker.com/science-tech/bbc-horizon-secrets-of-the-solar-system-2015-dailymotion-com/

    :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()