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

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

 

# Licensed under the MIT license 

# http://opensource.org/licenses/mit-license.php 

 

# Copyright 2008, Benjamin Kampmann <ben.kampmann@googlemail.com> 

 

""" 

This is a Media Backend that allows you to access the Trailers from Apple.com 

""" 

from functools import cmp_to_key 

 

from lxml import etree 

from twisted.internet import task, reactor 

from twisted.web import client 

 

from coherence.backend import BackendItem, BackendStore 

from coherence.upnp.core import DIDLLite 

from coherence.upnp.core.utils import ReverseProxyUriResource 

 

XML_URL = "http://www.apple.com/trailers/home/xml/current.xml" 

 

ROOT_ID = 0 

 

 

class AppleTrailerProxy(ReverseProxyUriResource): 

 

    def __init__(self, uri): 

        ReverseProxyUriResource.__init__(self, uri) 

 

    def render(self, request): 

        request.received_headers[ 

            'user-agent'] = \ 

            'QuickTime/7.6.2 (qtver=7.6.2;os=Windows NT 5.1Service Pack 3)' 

        return ReverseProxyUriResource.render(self, request) 

 

 

class Trailer(BackendItem): 

 

    def __init__(self, parent_id, urlbase, id=None, name=None, cover=None, 

                 url=None): 

        BackendItem.__init__(self) 

        self.parentid = parent_id 

        self.id = id 

        self.name = name 

        self.cover = cover 

        if len(urlbase) and urlbase[-1] != '/': 

            urlbase += '/' 

        self.url = urlbase + str(self.id) 

        self.location = AppleTrailerProxy(url) 

        self.item = DIDLLite.VideoItem(id, parent_id, self.name) 

        self.item.albumArtURI = self.cover 

 

    def get_path(self): 

        return self.url 

 

 

class Container(BackendItem): 

    logCategory = 'apple_trailers' 

 

    def __init__(self, id, parent_id, name, store=None, 

                 children_callback=None): 

        BackendItem.__init__(self) 

        self.id = id 

        self.parent_id = parent_id 

        self.name = name 

        self.mimetype = 'directory' 

        self.update_id = 0 

        self.children = [] 

 

        self.item = DIDLLite.Container(id, parent_id, self.name) 

        self.item.childCount = None  # self.get_child_count() 

 

    def get_children(self, start=0, end=0): 

        if (end - start > 25 or 

                start - end == start or 

                end - start == 0): 

            end = start + 25 

        if end != 0: 

            return self.children[start:end] 

        return self.children[start:] 

 

    def get_child_count(self): 

        return len(self.children) 

 

    def get_item(self): 

        return self.item 

 

    def get_name(self): 

        return self.name 

 

    def get_id(self): 

        return self.id 

 

 

class AppleTrailersStore(BackendStore): 

    logCategory = 'apple_trailers' 

    implements = ['MediaServer'] 

 

    def __init__(self, server, *args, **kwargs): 

        BackendStore.__init__(self, server, **kwargs) 

        self.next_id = 1000 

        self.name = kwargs.get('name', 'Apple Trailers') 

        self.refresh = int(kwargs.get('refresh', 8)) * (60 * 60) 

 

        self.trailers = {} 

 

        self.wmc_mapping = {'15': 0} 

 

        dfr = self.update_data() 

        # first get the first bunch of data before sending init_completed 

        dfr.addCallback(lambda x: self.init_completed()) 

 

    def queue_update(self, result): 

        reactor.callLater(self.refresh, self.update_data) 

        return result 

 

    def update_data(self): 

        dfr = client.getPage(XML_URL) 

        dfr.addCallback(etree.fromstring) 

        dfr.addCallback(self.parse_data) 

        dfr.addCallback(self.queue_update) 

        return dfr 

 

    def parse_data(self, root): 

 

        def iterate(root): 

            for item in root.findall('./movieinfo'): 

                trailer = self._parse_into_trailer(item) 

                yield trailer 

 

        return task.coiterate(iterate(root)) 

 

    def _parse_into_trailer(self, item): 

        """ 

        info = item.find('info') 

 

        for attr in ('title', 'runtime', 'rating', 'studio', 'postdate', 

                     'releasedate', 'copyright', 'director', 'description'): 

            setattr(trailer, attr, info.find(attr).text) 

        """ 

 

        data = {} 

        data['id'] = item.get('id') 

        data['name'] = item.find('./info/title').text 

        data['cover'] = item.find('./poster/location').text 

        data['url'] = item.find('./preview/large').text 

 

        trailer = Trailer(ROOT_ID, self.urlbase, **data) 

        duration = None 

        try: 

            hours = 0 

            minutes = 0 

            seconds = 0 

            duration = item.find('./info/runtime').text 

            try: 

                hours, minutes, seconds = duration.split(':') 

            except ValueError: 

                try: 

                    minutes, seconds = duration.split(':') 

                except ValueError: 

                    seconds = duration 

            duration = "%d:%02d:%02d" % ( 

                int(hours), int(minutes), int(seconds)) 

        except Exception: 

            pass 

 

        try: 

            trailer.item.director = item.find('./info/director').text 

        except Exception: 

            pass 

 

        try: 

            trailer.item.description = item.find('./info/description').text 

        except Exception: 

            pass 

 

        res = DIDLLite.Resource(trailer.get_path(), 

                                'http-get:*:video/quicktime:*') 

        res.duration = duration 

        try: 

            res.size = item.find('./preview/large').get('filesize', None) 

        except Exception: 

            pass 

        trailer.item.res.append(res) 

 

        if self.server.coherence.config.get('transcoding', 'no') == 'yes': 

            dlna_pn = 'DLNA.ORG_PN=AVC_TS_BL_CIF15_AAC' 

            dlna_tags = DIDLLite.simple_dlna_tags[:] 

            dlna_tags[2] = 'DLNA.ORG_CI=1' 

            url = self.urlbase + str(trailer.id) + '?transcoded=mp4' 

            new_res = DIDLLite.Resource( 

                url, 

                'http-get:*:%s:%s' % ( 

                    'video/mp4', ';'.join([dlna_pn] + dlna_tags))) 

            new_res.size = None 

            res.duration = duration 

            trailer.item.res.append(new_res) 

 

            dlna_pn = 'DLNA.ORG_PN=JPEG_TN' 

            dlna_tags = DIDLLite.simple_dlna_tags[:] 

            dlna_tags[2] = 'DLNA.ORG_CI=1' 

            dlna_tags[3] = 'DLNA.ORG_FLAGS=00f00000000000000000000000000000' 

            url = self.urlbase + str( 

                trailer.id) + '?attachment=poster&transcoded=thumb&type=jpeg' 

            new_res = DIDLLite.Resource( 

                url, 'http-get:*:%s:%s' % ( 

                    'image/jpeg', 

                    ';'.join([dlna_pn] + dlna_tags))) 

            new_res.size = None 

            # new_res.resolution = "160x160" 

            trailer.item.res.append(new_res) 

            if not hasattr(trailer.item, 'attachments'): 

                trailer.item.attachments = {} 

            trailer.item.attachments['poster'] = data['cover'] 

 

        self.trailers[trailer.id] = trailer 

        return trailer 

 

    def get_by_id(self, id): 

        try: 

            if int(id) == 0: 

                return self.container 

            else: 

                return self.trailers.get(id, None) 

        except Exception: 

            return None 

 

    def upnp_init(self): 

        if self.server: 

            self.server.connection_manager_server.set_variable( 

                0, 'SourceProtocolInfo', 

                ['http-get:*:video/quicktime:*', 'http-get:*:video/mp4:*']) 

        self.container = Container(ROOT_ID, -1, self.name) 

        trailers = list(self.trailers.values()) 

        # trailers.sort(cmp=lambda x, y: cmp( 

        #     x.get_name().lower(), y.get_name().lower())) 

        trailers = sorted( 

            trailers, 

            key=lambda x, y: cmp_to_key( 

                x.get_name().lower(), 

                y.get_name().lower())) 

        self.container.children = trailers 

 

    def __repr__(self): 

        return self.__class__.__name__