PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/plugin.video.teledunet/resources/lib/teledunet/scraper.py

https://github.com/araber14/repository.arabic.xbmc-addons
Python | 131 lines | 112 code | 14 blank | 5 comment | 4 complexity | bd68debac0bd295b97891a80d0b3ffb2 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  1. import cookielib
  2. import re
  3. import urllib2,urllib
  4. from BeautifulSoup import BeautifulSoup
  5. from models import ChannelItem
  6. from hardcode import HARDCODED_STREAMS
  7. import xbmcaddon
  8. #addon_id = 'plugin.video.shahidmbcnet'
  9. selfAddon = xbmcaddon.Addon()
  10. #HEADER_REFERER = 'http://www.teledunet.com/'
  11. HEADER_REFERER = 'http://www.teledunet.com/list_chaines.php'
  12. HEADER_HOST = 'www.teledunet.com'
  13. HEADER_USER_AGENT = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
  14. TELEDUNET_TIMEPLAYER_URL = 'http://www.teledunet.com/mobile/?con'
  15. PPV_CHANNEL_URL='rtmp://5.135.134.110:1935/teledunet/'
  16. cj = cookielib.CookieJar()
  17. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  18. def _get(request,post=None):
  19. """Performs a GET request for the given url and returns the response"""
  20. return opener.open(request,post).read()
  21. def _html(url):
  22. """Downloads the resource at the given url and parses via BeautifulSoup"""
  23. headers = { "User-Agent": HEADER_USER_AGENT }
  24. request = urllib2.Request (url , headers = headers)
  25. return BeautifulSoup(_get(request), convertEntities=BeautifulSoup.HTML_ENTITIES)
  26. def __get_cookie_session():
  27. # Fetch the main Teledunet website to be given a Session ID
  28. _html('http://www.teledunet.com/')
  29. for cookie in cj:
  30. if cookie.name == 'PHPSESSID':
  31. return 'PHPSESSID=%s' % cookie.value
  32. raise Exception('Cannot find PHP session from Teledunet')
  33. def performLogin():
  34. print 'performing login'
  35. userName=selfAddon.getSetting( "teledunetTvLogin" )
  36. password=selfAddon.getSetting( "teledunetTvPassword" )
  37. req = urllib2.Request('http://www.teledunet.com/boutique/connexion.php')
  38. req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36')
  39. post={'login_user':userName,'pass_user':password}
  40. post = urllib.urlencode(post)
  41. link = _get(req,post)
  42. req = urllib2.Request('http://www.teledunet.com/')#access main page too
  43. req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36')
  44. _get(req,post)
  45. def __get_channel_time_player(channel_name):
  46. loginname=selfAddon.getSetting( "teledunetTvLogin" )
  47. if not (loginname==None or loginname==""):
  48. performLogin()
  49. url = TELEDUNET_TIMEPLAYER_URL# % channel_name
  50. print url
  51. # Set custom header parameters to simulate request is coming from website
  52. req = urllib2.Request(url)
  53. req.add_header('Referer', HEADER_REFERER)
  54. req.add_header('Host', HEADER_HOST)
  55. req.add_header('User-agent', HEADER_USER_AGENT)
  56. req.add_header('Cookie', __get_cookie_session())
  57. html = _get(req)
  58. m = re.search('aut=\'\?id0=(.*?)\'', html, re.M | re.I)
  59. time_player_str = eval(m.group(1))
  60. #print 'set_favoris\(\''+channel_name+'\'.*?rtmp://(.*?)\''
  61. m = re.search('rtmp://(.*?)/%s\''%channel_name, html, re.M | re.I)
  62. if m ==None:
  63. print 'geting from backup file'
  64. req = urllib2.Request("https://dl.dropboxusercontent.com/s/ku3n4n53qphqnmn/Frame-code.txt")
  65. html = _get(req)
  66. m = re.search('rtmp://(.*?)/%s\''%channel_name, html, re.M | re.I)
  67. if m ==None:
  68. rtmp_url=PPV_CHANNEL_URL+channel_name
  69. else:
  70. rtmp_url = m.group(1)
  71. rtmp_url='rtmp://%s/%s'%(rtmp_url,channel_name)
  72. play_path = rtmp_url[rtmp_url.rfind("/") + 1:]
  73. return rtmp_url, play_path, repr(time_player_str).rstrip('0').rstrip('.')
  74. def get_rtmp_params(channel_name):
  75. rtmp_url, play_path, time_player_id = __get_channel_time_player(channel_name)
  76. return {
  77. 'rtmp_url': rtmp_url,
  78. 'playpath': play_path,
  79. 'app': 'teledunet',
  80. 'swf_url': ('http://www.teledunet.com/player.swf?'
  81. 'id0=%(time_player)s&'
  82. ) % {'time_player': time_player_id, 'channel_name': play_path, 'rtmp_url': rtmp_url},
  83. 'video_page_url': 'http://www.teledunet.com/player/?channel=%s conn=N:1 flashVer=WIN\2013,0,0,214' % play_path,
  84. 'live': '1'
  85. }
  86. def get_channels():
  87. html = _html(HEADER_REFERER)
  88. channel_divs = lambda soup : soup.findAll("div", { "class" : re.compile("div_channel") })
  89. channels = [ChannelItem(el=el) for el in channel_divs(html)]
  90. # Extend Teledunet list with custom hardcoded list created by community
  91. channels.extend(__get_hardcoded_streams())
  92. return channels
  93. def __get_hardcoded_streams():
  94. return [ChannelItem(json=json) for json in HARDCODED_STREAMS]
  95. def debug():
  96. print len(get_channels())
  97. #print __get_channel_time_player('2m')
  98. #print get_rtmp_params('2m')
  99. pass
  100. if __name__ == '__main__':
  101. debug()