#!/usr/bin/python
# -*- coding: utf-8 -*-
import xbmcplugin,xbmcaddon,xbmcgui,xbmc,urllib,urllib2,html5lib,os,sys,re
from bs4 import BeautifulSoup

addon = xbmcaddon.Addon()
addon_path = addon.getAddonInfo('path').decode('utf-8')
xbmcplugin.setContent(handle=int(sys.argv[1]), content='movies')

def addDir(name,url,mode,iconimage):
        u=sys.argv[0]+'?url='+str(url)+'&mode='+str(mode)+'&name='+str(name)
        liz=xbmcgui.ListItem(name, iconImage=os.path.join(addon_path,'folder.png'), thumbnailImage=iconimage)
        liz.setInfo( type='Video', infoLabels={ 'Title': name } )
        xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=1)

def addItem(url, infolabels, img):
    listitem = xbmcgui.ListItem(infolabels['title'], iconImage=os.path.join(addon_path,'icon.png'), thumbnailImage=img)
    listitem.setInfo('video', infolabels)
    listitem.setProperty('IsPlayable', 'true')
    xbmcplugin.addDirectoryItem(int(sys.argv[1]), url, listitem)

def open_url(url):
    req = urllib2.Request(url.replace('https:','http:'))
    req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
    response = urllib2.urlopen(req)
    r=response.read()
    response.close()
    return r

def play_video_url_regex(r_read):
    match = re.search(r'data-url="[\'"]?([^\'" >]+)', r_read)
    if match:
        return str(match.group(1))

def beautiful_soup_video_data(url,r_read):	
    soup = BeautifulSoup(r_read,"html5lib")
    for ml_item in soup.findAll('div', { 'class' : 'ml-item' }):

        if ml_item:
            a = ml_item.find('a')
            if a:
                video_url = a.get('href')

            img = ml_item.find('img')
            if img:
                video_pic = img.get('src')

            h6 = ml_item.find('div', class_='mtd-header')
            if h6:
                for header in h6.find_all('h6'):
                    titel = header.get_text()

            for li in ml_item.find_all('li'):
                if 'glyphicon glyphicon-calendar' in str(li.get_text):
                    datum = str(li.get_text).split('</span>')[1].strip().split('<')[0].strip()

            for li in ml_item.find_all('li'):
                if 'IMDb:' in str(li.get_text):
                    rating = str(li.get_text).split('</p>')[1].strip().split('<')[0].strip()

            video_titel = '[COLOR blue]' + titel.encode("iso-8859-1", "ignore").strip() + '[/COLOR]' + '[COLOR red] [ ' + datum + ' | ' + rating + ' ][/COLOR]'
            if ((not video_titel == None) and (not video_url == None) and (not video_pic == None)):
                    addDir('[COLOR blue]' + video_titel + '[/COLOR]',urllib.quote(video_url).encode('utf8'), 1, video_pic)

    next_page_number = beautiful_soup_page_number(r_read)
    if next_page_number:
        url = url.replace(str('/' + str(int(next_page_number)-1)),str('/' + next_page_number))
        addDir('[COLOR red]' + 'NEXT PAGE' + '[/COLOR]',urllib.quote(url).encode('utf8'), 1, os.path.join(addon_path,'next.png'))

def beautiful_soup_page_number(r_read):	
    soup = BeautifulSoup(r_read,"html5lib")
    for nav_page in soup.findAll('nav', { 'class' : 'page-nav' }):

        if nav_page:
            for a in nav_page.find_all('a'):
                if a:
                    if 'next' in str(a.get('rel')):
                        return a.get('data-ci-pagination-page')

if not sys.argv[2] == '' and 'url=' in sys.argv[2]:
    url = ''
    url = urllib.unquote(sys.argv[2].split('url=')[1]).decode('utf8')
	
    if 'suche' == url:
        keyboard = xbmc.Keyboard()
        keyboard.setHeading('Search Movies')
        keyboard.setDefault('')
        keyboard.doModal()
        if keyboard.isConfirmed() and not keyboard.getText() == '':
            search_text = keyboard.getText()
            beautiful_soup_video_data(url,open_url(str('https://www.tata.to/filme?suche=' + search_text)))
	
    elif 'genres' == url:
        addDir('[COLOR blue]' + 'Neu & Beliebt' + '[/COLOR]',urllib.quote('https://www.tata.to').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Filme' + '[/COLOR]',urllib.quote('https://www.tata.to/filme').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Abenteuer' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/abenteuer-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Action' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/action-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Animation' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/animation-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Dokumentarfilm' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/dokumentarfilm-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Drama' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/drama-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Familie' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/familie-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Fantasy' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/fantasy-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Foreign' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/foreign-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Historie' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/historie-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Horror' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/horror-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Komoedie' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/komoedie-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Kriegsfilm' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/kriegsfilm-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Krimi' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/krimi-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Lovestory' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/lovestory-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Musik' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/musik-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Mystery' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/mystery-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Science-Fiction' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/science-fiction-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'TV-Filme' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/tv-film-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Thriller' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/thriller-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Western' + '[/COLOR]',urllib.quote('https://www.tata.to/genre/western-filme/1').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
    elif 'land' == url:
        addDir('[COLOR blue]' + 'Algeria' + '[/COLOR]',urllib.quote('https://www.tata.to/land/algeria').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Angola' + '[/COLOR]',urllib.quote('https://www.tata.to/land/angola').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Argentina' + '[/COLOR]',urllib.quote('https://www.tata.to/land/argentina').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Australia' + '[/COLOR]',urllib.quote('https://www.tata.to/land/australia').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Austria' + '[/COLOR]',urllib.quote('https://www.tata.to/land/austria').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Bahamas' + '[/COLOR]',urllib.quote('https://www.tata.to/land/bahamas').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Belgium' + '[/COLOR]',urllib.quote('https://www.tata.to/land/belgium').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Brazil' + '[/COLOR]',urllib.quote('https://www.tata.to/land/brazil').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Bulgaria' + '[/COLOR]',urllib.quote('https://www.tata.to/land/bulgaria').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Canada' + '[/COLOR]',urllib.quote('https://www.tata.to/land/canada').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Chile' + '[/COLOR]',urllib.quote('https://www.tata.to/land/chile').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'China' + '[/COLOR]',urllib.quote('https://www.tata.to/land/china').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Colombia' + '[/COLOR]',urllib.quote('https://www.tata.to/land/colombia').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Czech Republic' + '[/COLOR]',urllib.quote('https://www.tata.to/land/czech-republic').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Czechoslovakia' + '[/COLOR]',urllib.quote('https://www.tata.to/land/czechoslovakia').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Denmark' + '[/COLOR]',urllib.quote('https://www.tata.to/land/denmark').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'East Germany' + '[/COLOR]',urllib.quote('https://www.tata.to/land/east-germany').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Estonia' + '[/COLOR]',urllib.quote('https://www.tata.to/land/estonia').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Ethiopia' + '[/COLOR]',urllib.quote('https://www.tata.to/land/ethiopia').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Finland' + '[/COLOR]',urllib.quote('https://www.tata.to/land/finland').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'France' + '[/COLOR]',urllib.quote('https://www.tata.to/land/france').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Germany' + '[/COLOR]',urllib.quote('https://www.tata.to/land/germany').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Greece' + '[/COLOR]',urllib.quote('https://www.tata.to/land/greece').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Hong Kong' + '[/COLOR]',urllib.quote('https://www.tata.to/land/hong-kong').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Hungary' + '[/COLOR]',urllib.quote('https://www.tata.to/land/hungary').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Iceland' + '[/COLOR]',urllib.quote('https://www.tata.to/land/iceland').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'India' + '[/COLOR]',urllib.quote('https://www.tata.to/land/india').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Indonesia' + '[/COLOR]',urllib.quote('https://www.tata.to/land/indonesia').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Iran' + '[/COLOR]',urllib.quote('https://www.tata.to/land/iran').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Ireland' + '[/COLOR]',urllib.quote('https://www.tata.to/land/ireland').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Israel' + '[/COLOR]',urllib.quote('https://www.tata.to/land/israel').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Italy' + '[/COLOR]',urllib.quote('https://www.tata.to/land/italy').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Japan' + '[/COLOR]',urllib.quote('https://www.tata.to/land/japan').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Lithuania' + '[/COLOR]',urllib.quote('https://www.tata.to/land/lithuania').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Luxembourg' + '[/COLOR]',urllib.quote('https://www.tata.to/land/luxembourg').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Malaysia' + '[/COLOR]',urllib.quote('https://www.tata.to/land/malaysia').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Malta' + '[/COLOR]',urllib.quote('https://www.tata.to/land/malta').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Mexico' + '[/COLOR]',urllib.quote('https://www.tata.to/land/mexico').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Morocco' + '[/COLOR]',urllib.quote('https://www.tata.to/land/morocco').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Netherlands' + '[/COLOR]',urllib.quote('https://www.tata.to/land/netherlands').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'New Zealand' + '[/COLOR]',urllib.quote('https://www.tata.to/land/new-zealand').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Norway' + '[/COLOR]',urllib.quote('https://www.tata.to/land/norway').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Panama' + '[/COLOR]',urllib.quote('https://www.tata.to/land/panama').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Peru' + '[/COLOR]',urllib.quote('https://www.tata.to/land/peru').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Poland' + '[/COLOR]',urllib.quote('https://www.tata.to/land/poland').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Portugal' + '[/COLOR]',urllib.quote('https://www.tata.to/land/portugal').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Puerto Rico' + '[/COLOR]',urllib.quote('https://www.tata.to/land/puerto-rico').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Qatar' + '[/COLOR]',urllib.quote('https://www.tata.to/land/qatar').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Romania' + '[/COLOR]',urllib.quote('https://www.tata.to/land/romania').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Russia' + '[/COLOR]',urllib.quote('https://www.tata.to/land/russia').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Serbia' + '[/COLOR]',urllib.quote('https://www.tata.to/land/serbia').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Singapore' + '[/COLOR]',urllib.quote('https://www.tata.to/land/singapore').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'South Africa' + '[/COLOR]',urllib.quote('https://www.tata.to/land/south-africa').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'South Korea' + '[/COLOR]',urllib.quote('https://www.tata.to/land/south-korea').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Spain' + '[/COLOR]',urllib.quote('https://www.tata.to/land/spain').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Sweden' + '[/COLOR]',urllib.quote('https://www.tata.to/land/sweden').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Switzerland' + '[/COLOR]',urllib.quote('https://www.tata.to/land/switzerland').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Taiwan' + '[/COLOR]',urllib.quote('https://www.tata.to/land/taiwan').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Thailand' + '[/COLOR]',urllib.quote('https://www.tata.to/land/thailand').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Trinidad and Tobago' + '[/COLOR]',urllib.quote('https://www.tata.to/land/trinidad-and-tobago').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Turkey' + '[/COLOR]',urllib.quote('https://www.tata.to/land/turkey').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'UK' + '[/COLOR]',urllib.quote('https://www.tata.to/land/uk').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'USA' + '[/COLOR]',urllib.quote('https://www.tata.to/land/usa').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'Ukraine' + '[/COLOR]',urllib.quote('https://www.tata.to/land/ukraine').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'United Arab Emirates' + '[/COLOR]',urllib.quote('https://www.tata.to/land/united-arab-emirates').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
        addDir('[COLOR blue]' + 'West Germany' + '[/COLOR]',urllib.quote('https://www.tata.to/land/west-germany').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))

    elif 'https://www.tata.to' == url:
        beautiful_soup_video_data(url,open_url(url))
    elif 'https://www.tata.to/filme' == url:
        beautiful_soup_video_data(url,open_url(url))
    elif 'https://www.tata.to/genre/' in url:
        beautiful_soup_video_data(url,open_url(url))
    elif 'https://www.tata.to/land/' in url:
        beautiful_soup_video_data(url,open_url(url))
    elif 'https://www.tata.to/film/' in url:
        addItem(play_video_url_regex(open_url(url)),{'title':'[COLOR red]PLAY MOVIE[/COLOR]'},os.path.join(addon_path,'play.png'))
else:
    addDir('[COLOR blue]' + 'Suche' + '[/COLOR]',urllib.quote('suche').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
    addDir('[COLOR blue]' + 'Genres' + '[/COLOR]',urllib.quote('genres').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))
    addDir('[COLOR blue]' + 'Land' + '[/COLOR]',urllib.quote('land').encode('utf8'), 1, os.path.join(addon_path,'folder.png'))

xbmcplugin.endOfDirectory(int(sys.argv[1]))
xbmc.executebuiltin('Container.SetViewMode(100)')
#TATA.TO BY LOKI1979 - (( 28.07.2016 -23:18 ))
