Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

# -*- coding: utf-8 -*- 

# Module: ItemHelper 

# Author: asciidisco 

# Created on: 24.07.2017 

# License: MIT https://goo.gl/WA1kby 

 

"""Interface for matching API data with the Kodi item interface""" 

 

from datetime import datetime 

 

 

class ItemHelper(object): 

"""Interface for matching API data with the Kodi item interface""" 

 

def __init__(self, constants, utils): 

""" 

Injects instances 

 

:param constants: Constants instance 

:type constants: resources.lib.Constants 

:param utils: Utils instance 

:type utils: resources.lib.Utils 

""" 

self.constants = constants 

self.utils = utils 

 

def build_description(self, item): 

""" 

Generates an item description 

 

:param item: Item to be described 

:type item: dict 

:returns: string -- Item description 

""" 

desc = '' 

if item.get('metadata', {}).get('description_bold'): 

desc += item.get('metadata', {}).get('description_bold') + ' ' 

if item.get('metadata', {}).get('description_regular'): 

if desc != '': 

desc += '- ' 

desc += item.get('metadata', {}).get('description_regular') + '' 

if desc != '': 

desc += ':\n' 

desc += self.build_title(item) + ' ' 

return desc 

 

def set_art(self, list_item, sport, item=None): 

""" 

Sets art for the given item 

 

:param list_item: Kodi list item 

:type list_item: xbmcgui.ListItem 

:param sport: Chosen sport 

:type sport: string 

:param item: Item to set art for 

:type item: dict 

:returns: xbmcgui.ListItem -- Kodi list item 

""" 

sports = self.constants.get_sports_list() 

base_url = self.constants.get_base_url() 

list_item = self.__get_sports_art( 

list_item=list_item, 

sport=sport, 

sports=sports) 

if item is not None: 

metatdata = item.get('metadata', {}) 

if item.get('images'): 

images = item.get('images', {}) 

list_item = self.__get_editorial_art( 

list_item=list_item, 

base_url=base_url, 

images=images) 

if metatdata.get('images'): 

images = metatdata.get('images') 

list_item = self.__get_editorial_art( 

list_item=list_item, 

base_url=base_url, 

images=images) 

return list_item 

 

def build_page_leave(self, target_url, details, match_time, shorts=None): 

""" 

Builds the data for an Kodi folder item 

 

:param target_url: Plugin target url 

:type target_url: string 

:param details: EPG element details 

:type details: dict 

:param match_time: Events match time 

:type match_time: string 

:param shorts: Add shorts desc 

:type shorts: dict 

:returns: dict -- List item info properties 

""" 

return { 

'hash': self.utils.generate_hash(target_url), 

'url': self.constants.get_base_url() + target_url, 

'title': self.build_epg_title( 

details=details, 

match_time=match_time), 

'shorts': shorts, 

} 

 

def build_title(self, item): 

""" 

Generates an title for an item 

 

:param item: Item to be described 

:type item: dict 

:returns: string -- Item title 

""" 

title = '' 

metadata = item.get('metadata', {}) 

114 ↛ 115line 114 didn't jump to line 115, because the condition on line 114 was never true if metadata.get('details') is not None: 

details = metadata.get('details') 

home = details.get('home', {}) 

name_full = home.get('name_full') 

if name_full is not None: 

title += self.__build_match_title_full(details=details) 

elif name_full is None and home.get('name_short') is not None: 

title += self.__build_match_title_short(details=details) 

return self.__build_fallback_title(title=title, metadata=metadata) 

 

def __get_editorial_art(self, list_item, base_url, images): 

""" 

Sets editorial art for the given item 

 

:param list_item: Kodi list item 

:type list_item: xbmcgui.ListItem 

:param base_url: Image base url 

:type base_url: string 

:param images: Map of usable images 

:type images: dict 

:returns: xbmcgui.ListItem -- Kodi list item 

""" 

image = '' 

if images.get('fallback'): 

image = base_url + '/' + images.get('fallback') 

if images.get('editorial'): 

image = base_url + '/' + images.get('editorial') 

if image != '': 

try: 

list_item.setArt({ 

'poster': image, 

'landscape': image, 

'thumb': image, 

'fanart': image 

}) 

except RuntimeError: 

self.utils.log('`setArt` not available') 

return list_item 

 

def __get_sports_art(self, list_item, sport, sports): 

""" 

Sets editorial art for static sport item 

 

:param list_item: Kodi list item 

:type list_item: xbmcgui.ListItem 

:param sport: Chosen sport 

:type sport: string 

:param sports: Map of available sports 

:type sports: dict 

:returns: xbmcgui.ListItem -- Kodi list item 

""" 

try: 

list_item.setArt({ 

'poster': sports.get(sport).get('fanart'), 

'landscape': sports.get(sport).get('fanart'), 

'thumb': sports.get(sport).get('image'), 

'fanart': sports.get(sport).get('fanart') 

}) 

except RuntimeError: 

self.utils.log('`setArt` not available') 

return list_item 

 

@classmethod 

def __build_fallback_title(cls, title, metadata): 

""" 

Generates a fallback title 

 

:param title: Original title 

:type title: string 

:param metadata: Item metadata 

:type metadata: dict 

:returns: string -- Fallback title 

""" 

fallback_title = '' 

if title != '': 

return title 

fallback_title += metadata.get('title', '') 

if fallback_title == '': 

fallback_title += metadata.get('description_regular', '') 

if fallback_title == '': 

fallback_title += metadata.get('description_bold', '') 

return fallback_title 

 

@classmethod 

def __build_match_title_short(cls, details): 

""" 

Generates a short match title 

 

:param details: Item details 

:type details: dict 

:returns: string -- Match title (short) 

""" 

title = details.get('home', {}).get('name_short') 

title += ' - ' 

title += details.get('away', {}).get('name_short') 

return title 

 

@classmethod 

def __build_match_title_full(cls, details): 

""" 

Generates a long match title 

 

:param details: Item details 

:type details: dict 

:returns: string -- Match title (long) 

""" 

title = details.get('home', {}).get('name_full') 

title += ' - ' 

title += details.get('away', {}).get('name_full') 

return title 

 

@classmethod 

def build_epg_title(cls, details, match_time): 

""" 

Generates a epg title 

 

:param details: Item details 

:type details: dict 

:param match_time: Events match time 

:type match_time: string 

:returns: string -- EPG item title 

""" 

title = details.get('home', {}).get('name_full', '') 

title += ' - ' 

title += details.get('away', {}).get('name_full', '') 

title += ' (' + match_time + ' Uhr)' 

return title 

 

@classmethod 

def datetime_from_utc(cls, metadata, element=None): 

""" 

Generates a homan readable time from an items UTC timestamp 

 

:param metadata: Item metadata 

:type metadata: dict 

:param details: Item details 

:type details: dict 

:returns: tuple -- Match date & match time 

""" 

date_container = None 

if metadata.get('scheduled_start'): 

date_container = metadata.get('scheduled_start', {}) 

elif element is not None: 

date_container = element.get('scheduled_start', {}) 

if date_container is None: 

return (None, None) 

timestamp = float(date_container.get('date')) 

match_datetime = datetime.fromtimestamp(timestamp) 

match_date = match_datetime.strftime('%d.%m.%Y') 

match_time = match_datetime.strftime('%H:%M') 

return (match_date, match_time)