Advertisement
alexdunlop81

ustvnow.py

Dec 9th, 2013
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.01 KB | None | 0 0
  1. '''
  2.    ustvnow XBMC Plugin
  3.    Copyright (C) 2011 t0mm0
  4.  
  5.    This program is free software: you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation, either version 3 of the License, or
  8.    (at your option) any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. '''
  18. import Addon
  19. import cookielib
  20. import os
  21. import re
  22. import urllib, urllib2
  23. from bs4 import BeautifulSoup, SoupStrainer
  24. from  itertools import izip
  25.  
  26.  
  27. class Ustvnow:
  28.     __BASE_URL = 'http://lv2.ustvnow.com'
  29.     def __init__(self, user, password):
  30.         self.user = user
  31.         self.password = password
  32.                    
  33.     def get_channels(self, quality=1, stream_type='rtmp'):
  34.         self._login()
  35.         html = self._get_html('iphone_ajax', {'tab': 'iphone_playingnow',
  36.                                               'token': self.token})
  37.         print html
  38.         channels = []
  39.         for channel in re.finditer('class="panel".+?title="(.+?)".+?src="' +
  40.                                    '(.+?)".+?class="nowplaying_item">(.+?)' +
  41.                                    '<\/td>.+?class="nowplaying_itemdesc".+?' +
  42.                                    '<\/a>(.+?)<\/td>.+?href="(.+?)"',
  43.                                    html, re.DOTALL):
  44.             name, icon, title, plot, url = channel.groups()
  45.             if not url.startswith('http'):
  46.                 now = {'title': title, 'plot': plot.strip()}
  47.                 url = '%s%s%d' % (stream_type, url[4:-1], quality + 1)
  48.                 channels.append({'name': name, 'url': url,
  49.                                'icon': icon, 'now': now})
  50.         return channels        
  51.  
  52.     def get_recordings(self, quality=1, stream_type='rtmp'):
  53.         self._login()
  54.         html = self._get_html('iphone_ajax', {'tab': 'iphone_viewdvrlist'})
  55.         print html
  56.         tree = BeautifulSoup(html)
  57.         recordings = []
  58.        # for r in re.finditer('class="panel".+?title="(.+?)".+?src="(.+?)".+?' +
  59.         #                     'class="nowplaying_item">(.+?)<\/td>.+?(?:<\/a>' +
  60.          #                    '(.+?)<\/td>.+?)?vertical-align:bottom.+?">(.+?)' +
  61.           #                   '<\/div>.+?_self" href="(rtsp[^"]+)".+href="(.+)".*?<\/div>',
  62.            #                  html, re.DOTALL):
  63.         descriptions = tree.find_all('td',class_='nowplaying_desc')
  64.         for r,d in izip(tree.find_all('div', class_='panel'), descriptions):
  65. #            print r
  66.             chan = r['title']
  67.             icon = r.find('img')['src']
  68.             title = r.find('td',class_='nowplaying_item').string
  69.             try:
  70.                 plot =  d.string
  71.                 print "%%%%%%%%%%%%PLOT",plot
  72.             except:
  73.                 plot = ''
  74.             try:
  75.                 rec_date = r.find('div', text = re.compile('Recorded')).string
  76.             except:
  77.                 continue
  78.             url = r.find('a',class_="grayButton")['href']
  79.             del_url = r.find('a',class_="redButton")['href']
  80.             #, icon, title, plot, rec_date, url, del_url = r.groups()
  81.  #           print chan, icon, title, plot, rec_date, url, del_url
  82.             if rec_date:
  83.                 url = '%s%s%s' % (stream_type, url[4:-7],
  84.                                   ['350', '650', '950'][quality])
  85.               #  if plot:
  86.                #     plot = plot.strip()
  87.                # else:
  88.                 #    plot = ''
  89.  
  90.                 recordings.append({'channel': chan,
  91.                                    'stream_url': url,
  92.                                    'title': title,
  93.                                    'plot': plot,
  94.                                    'rec_date': rec_date.strip(),
  95.                                    'icon': icon,
  96.                                    'del_url': del_url,
  97.                                    #'premiered' : episode_airdate
  98.                                    })
  99.         return recordings
  100.    
  101.     def delete_recording(self, del_url):
  102.         html = self._get_html(del_url)
  103.         print html
  104.    
  105.     def _build_url(self, path, queries={}):
  106.         if queries:
  107.             query = Addon.build_query(queries)
  108.             return '%s/%s?%s' % (self.__BASE_URL, path, query)
  109.         else:
  110.             return '%s/%s' % (self.__BASE_URL, path)
  111.  
  112.     def _fetch(self, url, form_data=False):
  113.         if form_data:
  114.             Addon.log('posting: %s %s' % (url, str(form_data)))
  115.             req = urllib2.Request(url, form_data)
  116.         else:
  117.             Addon.log('getting: ' + url)
  118.             req = url
  119.         try:
  120.             response = urllib2.urlopen(url)
  121.             return response
  122.         except urllib2.URLError, e:
  123.             Addon.log(str(e), True)
  124.             return False
  125.        
  126.     def _get_html(self, path, queries={}):
  127.         html = False
  128.         url = self._build_url(path, queries)
  129.  
  130.         response = self._fetch(url)
  131.         if response:
  132.             html = response.read()
  133.         else:
  134.             html = False
  135.        
  136.         return html
  137.  
  138.     def _login(self):
  139.         Addon.log('logging in')
  140.         self.token = None
  141.         self.cj = cookielib.CookieJar()
  142.         opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
  143.        
  144.         urllib2.install_opener(opener)
  145.         url = self._build_url('iphone_login', {'username': self.user,
  146.                                                'password': self.password})
  147.         response = self._fetch(url)
  148.         #response = opener.open(url)
  149.        
  150.         for cookie in self.cj:
  151.             print '%s: %s' % (cookie.name, cookie.value)
  152.             if cookie.name == 'token':
  153.                 self.token = cookie.value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement