Skip to content

Commit

Permalink
Merge pull request #57 from adamantike/generic-create-item
Browse files Browse the repository at this point in the history
Generic implementation for items creation
  • Loading branch information
markop159 authored Jan 23, 2017
2 parents 0a493ef + 8214e31 commit 2b32bb1
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ class Anime(BaseContentWithSeasons):
request_path = 'tv/anime'
search_path = 'tv/animes'

@classmethod
def _get_item_info(cls, data):
return {
"title": data[0]['title'],
"season": int(data[0].get('season') or 0),
"episode": int(data[0].get('episode') or 0),
"tvshowtitle": data[-1]['tvshow'],
"genre": u" / ".join(genre for genre in data[0].get("genres", [])) or None,
"code": data[0].get("tvdb_id"),
"plot": data[0]['overview'],
"plotoutline": data[0]['overview']
}


def _folders(action, **kwargs):
if action == 'cat_Anime':
Expand Down Expand Up @@ -229,68 +242,4 @@ def _seasons(dom, **kwargs):


def _create_item(data):
label = 'Episode %s: %s' % (data[0]['episode'], data[0]['title'])

# seasondata0 has all the data from show
seasondata0 = int(data[0]['season'])

# seasondata_1 carries additional user data not included in show data
seasondata_1 = int(data[-1]['seasons'])
if not seasondata0 == seasondata_1:
return {}

torrents = {}
for quality, torrent_info in data[0].get('torrents', {}).items():
torrent_url = torrent_info.get('url')
if quality in settings.QUALITIES and torrent_url is not None:
torrents[quality] = torrent_url
torrents['%ssize' % quality] = 1000000000*60

# Do not return Shows without torrents
if not torrents:
return {}

# Set video width and hight
width = 640
height = 480
if torrents.get('1080p'):
width = 1920
height = 1080
elif torrents.get('720p'):
width = 1280
height = 720

return {
"label": label,
"icon": data[-1]['image'],
"thumbnail": data[-1]['image'],
"info": {
"title": data[0]['title'],
"season": int(data[0].get('season') or 0),
"episode": int(data[0].get('episode') or 0),
"tvshowtitle": data[-1]['tvshow'],
"genre": u" / ".join(genre for genre in data[0].get("genres", [])) or None,
#"duration": int(0),
"code": data[0].get("tvdb_id"),
"plot": data[0]['overview'],
"plotoutline": data[0]['overview']
},
"properties": {
"fanart_image": data[-1]['image2'],
"tvshowthumb": data[-1]['image2']
},
"stream_info": {
"video": {
"codec": u"h264",
#"duration": int(0),
"width": width,
"height": height
},
"audio": {
"codec": u"aac",
"language": u"en",
"channels": 2
}
},
"params": torrents
}
return Anime._create_item(data)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import re
import sys
import urllib2

Expand All @@ -13,8 +14,51 @@


class BaseContent(object):
# TODO: WIP for common functionality
pass
@classmethod
def _create_item(cls, data):
if not cls._is_item_valid_for_data(data):
return {}

torrents = cls._get_torrents_information(data)
# Do not show content without torrents
if not torrents:
return {}

return {
'label': cls._get_item_label(data),
'icon': cls._get_item_icon(data),
'thumbnail': cls._get_item_icon(data),
'info': cls._get_item_info(data),
'properties': cls._get_item_properties(data),
'stream_info': cls._get_item_stream_info(torrents),
'params': torrents,
}

@classmethod
def _get_item_stream_info(cls, torrents):
# Set video width and hight
width = 640
height = 480
if torrents.get('1080p'):
width = 1920
height = 1080
elif torrents.get('720p'):
width = 1280
height = 720

return {
'video': {
'codec': u'h264',
'duration': int(0),
'width': width,
'height': height,
},
'audio': {
'codec': u'aac',
'language': u'en',
'channels': 2,
},
}


class BaseContentWithSeasons(BaseContent):
Expand Down Expand Up @@ -152,3 +196,42 @@ def get_seasons(cls, dom, **kwargs):
}
for season in season_list
]

@staticmethod
def _is_item_valid_for_data(data):
# seasondata0 has all the data from show
seasondata0 = int(data[0]['season'])
# seasondata_1 carries additional user data not included in show data
seasondata_1 = int(data[-1]['seasons'])

return (seasondata0 == seasondata_1)

@staticmethod
def _get_item_icon(data):
return data[-1]['image']

@staticmethod
def _get_item_label(data):
return 'Episode {number}: {title}'.format(
number=data[0]['episode'],
title=data[0]['title'],
)

@staticmethod
def _get_item_properties(data):
return {
'fanart_image': data[-1]['image2'],
'tvshowthumb': data[-1]['image2'],
}

@staticmethod
def _get_torrents_information(data):
torrents = {}
for quality, torrent_info in data[0].get('torrents', {}).items():
torrent_url = torrent_info.get('url')
if quality in settings.QUALITIES and torrent_url is not None:
torrents.update({
quality: torrent_url,
'{0}size'.format(quality): 1000000000*60,
})
return torrents
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
import re
import sys
import urllib2
import xbmc

from kodipopcorntime import settings

from .base import BaseContent


__addon__ = sys.modules['__main__'].__addon__

_genres = {
Expand Down Expand Up @@ -38,73 +41,66 @@
'30428': 'Western'
}

def _create_item(data):
if not data.get("title"): # Title is require
return {}

torrents = {}
for quality, torrent_info in data.get('torrents').get('en', {}).items():
if quality in settings.QUALITIES:
torrents[quality] = torrent_info.get('url')
torrents['%ssize' % quality] = torrent_info.get('size')
class Movie(BaseContent):
@staticmethod
def _is_item_valid_for_data(data):
# Title is required
return bool(data.get('title'))

# Do not show Movies without torrents
if not torrents:
return {}
@staticmethod
def _get_item_icon(data):
return data.get('images').get('poster')

# Set video width and height
width = 640
height = 480
if torrents.get('1080p'):
width = 1920
height = 1080
elif torrents.get('720p'):
width = 1280
height = 720

title = data["title"]

trailer = ''
if data.get("trailer"):
trailer_regex = re.match('^[^v]+v=(.{11}).*', data.get("trailer"))
try:
trailer_id = trailer_regex.group(1)
trailer = "plugin://plugin.video.youtube/?action=play_video&videoid=%s" %trailer_id
except:
pass
@classmethod
def _get_item_info(cls, data):
return {
'title': data['title'],
'year': int(data.get('year') or 0),
'genre': u' / '.join(genre for genre in data.get('genres', [])) or None,
'code': data.get('imdb_id'),
'plot': data.get('synopsis') or None,
'plotoutline': data.get('synopsis') or None,
'trailer': cls._get_item_trailer(data)
}

@staticmethod
def _get_item_label(data):
return data['title']

@staticmethod
def _get_item_properties(data):
return {
'fanart_image': data.get('images').get('fanart'),
}

@staticmethod
def _get_item_trailer(data):
trailer = ''
if data.get('trailer'):
trailer_regex = re.match('^[^v]+v=(.{11}).*', data.get('trailer'))
try:
trailer_id = trailer_regex.group(1)
trailer = 'plugin://plugin.video.youtube/?action=play_video&videoid=%s' % trailer_id
except:
pass
return trailer

@staticmethod
def _get_torrents_information(data):
torrents = {}
for quality, torrent_info in data.get('torrents').get('en', {}).items():
if quality in settings.QUALITIES:
torrents.update({
quality: torrent_info.get('url'),
'{0}size'.format(quality): torrent_info.get('size'),
})
return torrents


def _create_item(data):
return Movie._create_item(data)

return {
"label": title,
"icon": data.get('images').get('poster'),
"thumbnail": data.get('images').get('poster'),
"info": {
"title": title,
"year": int(data.get("year") or 0),
"genre": u" / ".join(genre for genre in data.get("genres", [])) or None,
#"duration": int(0),
"code": data.get("imdb_id"),
"plot": data.get('synopsis') or None,
"plotoutline": data.get('synopsis') or None,
"trailer": trailer
},
"properties": {
"fanart_image": data.get('images').get('fanart')
},
"stream_info": {
"video": {
"codec": u"h264",
"duration": int(0),
"width": width,
"height": height
},
"audio": {
"codec": u"aac",
"language": u"en",
"channels": 2
}
},
"params": torrents
}

def _folders(action, **kwargs):
if action == 'cat_Movies':
Expand Down
Loading

0 comments on commit 2b32bb1

Please sign in to comment.