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

'''
    GreekFun Addon
    Author Laconian
    
    2026-08-06-05
'''

import datetime
import re
import sys
import time
import urllib
import urlparse
import urllib2
import xbmcgui
import xbmcplugin

base_url = "https://greeksubsmovies.me"

# Global Session / Cookie Handler
COOKIE_JAR = urllib2.HTTPCookieProcessor()
OPENER = urllib2.build_opener(COOKIE_JAR)

USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/143.0.0.0 Safari/537.36"
HEADERS = {
    'User-Agent': USER_AGENT,
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language': 'el-GR,el;q=0.9,en-US;q=0.8,en;q=0.7',
    'Connection': 'keep-alive'
}

# In-Memory Cache for Pages (URL -> (timestamp, html_content))
SHOW_CACHE = {}
CACHE_TTL = 300  # 5 minutes Time-To-Live

def get_cached_html(url):
    now = time.time()
    if url in SHOW_CACHE:
        timestamp, html = SHOW_CACHE[url]
        if now - timestamp < CACHE_TTL:
            return html
            
    req = urllib2.Request(url, headers=HEADERS)
    response = OPENER.open(req)
    html = response.read()
    
    SHOW_CACHE[url] = (now, html)
    return html

def decode_html_entities(text):
    if not text:
        return ""
    text = text.replace('&#039;', "'").replace('&apos;', "'")
    text = text.replace('&quot;', '"').replace('&amp;', '&')
    text = text.replace('&lt;', '<').replace('&gt;', '>')
    return text

def build_url(query):
    return addon_url + '?' + urllib.urlencode(query)

addon_url = sys.argv[0]
addon_handle = int(sys.argv[1])

# Parse arguments passed via Kodi plugin routing
args = urlparse.parse_qs(sys.argv[2][1:])
mode = args.get('mode', None)

if mode is None:
    # Root level: Add main navigation categories including Foreign Movies Years
    url_all = build_url({'mode': 'list_series', 'page': '1'})
    li_all = xbmcgui.ListItem(label="Ελληνικές Σειρές : [B]Όλες[/B]")
    xbmcplugin.addDirectoryItem(handle=addon_handle, url=url_all, listitem=li_all, isFolder=True)

    url_years = build_url({'mode': 'list_years'})
    li_years = xbmcgui.ListItem(label="Ελληνικές Σειρές : [B]Ετος[/B]")
    xbmcplugin.addDirectoryItem(handle=addon_handle, url=url_years, listitem=li_years, isFolder=True)

    url_movies_all = build_url({'mode': 'list_movies', 'page': '1'})
    li_movies_all = xbmcgui.ListItem(label="Ελληνικές Ταινίες : [B]Όλες[/B]")
    xbmcplugin.addDirectoryItem(handle=addon_handle, url=url_movies_all, listitem=li_movies_all, isFolder=True)

    url_movies_years = build_url({'mode': 'list_movies_years'})
    li_movies_years = xbmcgui.ListItem(label="Ελληνικές Ταινίες : [B]Ετος[/B]")
    xbmcplugin.addDirectoryItem(handle=addon_handle, url=url_movies_years, listitem=li_movies_years, isFolder=True)

    url_foreign_movies_years = build_url({'mode': 'list_foreign_movies_years'})
    li_foreign_movies_years = xbmcgui.ListItem(label="Ξένες Ταινίες : [B]Ετος[/B]")
    xbmcplugin.addDirectoryItem(handle=addon_handle, url=url_foreign_movies_years, listitem=li_foreign_movies_years, isFolder=True)

    xbmcplugin.endOfDirectory(addon_handle)

elif mode[0] == 'list_years':
    current_year = datetime.datetime.now().year
    for y_val in range(current_year, 1899, -1):
        year_label = str(y_val)
        url = build_url({'mode': 'list_series', 'page': '1', 'year': year_label})
        li = xbmcgui.ListItem(label=year_label)
        
        if hasattr(li, 'setInfo'):
            li.setInfo('video', {'mediatype': 'video', 'year': y_val})
            
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li, isFolder=True)
    
    xbmcplugin.setContent(addon_handle, 'tvshows')
    xbmcplugin.endOfDirectory(addon_handle)

elif mode[0] == 'list_movies_years':
    current_year = datetime.datetime.now().year
    for y_val in range(current_year, 1899, -1):
        year_label = str(y_val)
        url = build_url({'mode': 'list_movies', 'page': '1', 'year': year_label})
        li = xbmcgui.ListItem(label=year_label)
        
        if hasattr(li, 'setInfo'):
            li.setInfo('video', {'mediatype': 'video', 'year': y_val})
            
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li, isFolder=True)
    
    xbmcplugin.setContent(addon_handle, 'movies')
    xbmcplugin.endOfDirectory(addon_handle)

elif mode[0] == 'list_foreign_movies_years':
    current_year = datetime.datetime.now().year
    for y_val in range(current_year, 1899, -1):
        year_label = str(y_val)
        url = build_url({'mode': 'list_foreign_movies', 'page': '1', 'year': year_label})
        li = xbmcgui.ListItem(label=year_label)
        
        if hasattr(li, 'setInfo'):
            li.setInfo('video', {'mediatype': 'video', 'year': y_val})
            
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li, isFolder=True)
    
    xbmcplugin.setContent(addon_handle, 'movies')
    xbmcplugin.endOfDirectory(addon_handle)

elif mode[0] == 'list_series':
    page = args.get('page', ['1'])[0]
    year_filter = args.get('year', [''])[0]
    
    target_url = base_url + "/?filter=greek_series&sort=recent"
    if year_filter:
        target_url += "&f_year=" + year_filter
    target_url += "&page=" + page

    req = urllib2.Request(target_url, headers=HEADERS)
    response = OPENER.open(req)
    html = response.read()

    card_pattern = re.compile(
        r'<a class="card" href="([^"]+)">\s*<img src="([^"]+)" alt="([^"]+)"[^>]*>\s*<div class="card-meta">\s*<div class="card-title">[^<]+</div>\s*<div class="card-sub">\s*([^\s·]+)\s*· <span class="star-rating">★ ([^<]+)</span>',
        re.DOTALL
    )

    matches = card_pattern.findall(html)
    for href, img_src, alt_title, year, rating in matches:
        item_url = base_url + href
        cleaned_title = decode_html_entities(alt_title)
        
        li = xbmcgui.ListItem(label=cleaned_title)
        li.setArt({'thumb': img_src, 'poster': img_src})
        
        if hasattr(li, 'setInfo'):
            li.setInfo('video', {
                'title': cleaned_title,
                'year': int(year) if year.isdigit() else 0,
                'rating': float(rating) if rating.replace('.', '', 1).isdigit() else 0.0,
                'mediatype': 'tvshow'
            })
        
        route_url = build_url({'mode': 'list_seasons', 'url': item_url, 'poster': img_src})
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=route_url, listitem=li, isFolder=True)

    total_pages = "1"
    pager_total_match = re.search(r'page=(\d+)">\d+</a>\s*</nav>', html)
    if pager_total_match:
        total_pages = pager_total_match.group(1)
    else:
        all_pager_pages = re.findall(r'page=(\d+)[^"]*">(\d+)</a>', html)
        if all_pager_pages:
            total_pages = all_pager_pages[-1][1]
        else:
            page_nums = re.findall(r'page=(\d+)', html)
            if page_nums:
                total_pages = str(max(int(p) for p in page_nums))

    pager_next = re.compile(r'<a href="[^"]*page=(\d+)[^"]*">Επόμενη →</a>')
    next_match = pager_next.search(html)
    if next_match:
        next_page = next_match.group(1)
        next_params = {'mode': 'list_series', 'page': next_page}
        if year_filter:
            next_params['year'] = year_filter
            
        next_url = build_url(next_params)
        next_li = xbmcgui.ListItem(label="Επόμενη Σελίδα → (%s/%s)" % (next_page, total_pages))
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=next_url, listitem=next_li, isFolder=True)

    xbmcplugin.setContent(addon_handle, 'tvshows')
    xbmcplugin.endOfDirectory(addon_handle)

elif mode[0] == 'list_movies':
    page = args.get('page', ['1'])[0]
    year_filter = args.get('year', [''])[0]
    
    target_url = base_url + "/?filter=greek_movies&sort=recent"
    if year_filter:
        target_url += "&f_year=" + year_filter
    target_url += "&page=" + page

    req = urllib2.Request(target_url, headers=HEADERS)
    response = OPENER.open(req)
    html = response.read()

    card_pattern = re.compile(
        r'<a class="card" href="([^"]+)">\s*<img src="([^"]+)" alt="([^"]+)"[^>]*>\s*<div class="card-meta">\s*<div class="card-title">[^<]+</div>\s*<div class="card-sub">\s*([^\s·]+)\s*· <span class="star-rating">★ ([^<]+)</span>',
        re.DOTALL
    )

    matches = card_pattern.findall(html)
    for href, img_src, alt_title, year, rating in matches:
        item_url = base_url + href
        cleaned_title = decode_html_entities(alt_title)
        
        li = xbmcgui.ListItem(label=cleaned_title)
        li.setArt({'thumb': img_src, 'poster': img_src})
        
        if hasattr(li, 'setInfo'):
            li.setInfo('video', {
                'title': cleaned_title,
                'year': int(year) if year.isdigit() else 0,
                'rating': float(rating) if rating.replace('.', '', 1).isdigit() else 0.0,
                'mediatype': 'movie'
            })
        
        play_route_url = build_url({'mode': 'play_movie', 'url': item_url})
        li.setProperty('IsPlayable', 'true')
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=play_route_url, listitem=li, isFolder=False)

    total_pages = "1"
    pager_total_match = re.search(r'page=(\d+)">\d+</a>\s*</nav>', html)
    if pager_total_match:
        total_pages = pager_total_match.group(1)
    else:
        all_pager_pages = re.findall(r'page=(\d+)[^"]*">(\d+)</a>', html)
        if all_pager_pages:
            total_pages = all_pager_pages[-1][1]
        else:
            page_nums = re.findall(r'page=(\d+)', html)
            if page_nums:
                total_pages = str(max(int(p) for p in page_nums))

    pager_next = re.compile(r'<a href="[^"]*page=(\d+)[^"]*">Επόμενη →</a>')
    next_match = pager_next.search(html)
    if next_match:
        next_page = next_match.group(1)
        next_params = {'mode': 'list_movies', 'page': next_page}
        if year_filter:
            next_params['year'] = year_filter
            
        next_url = build_url(next_params)
        next_li = xbmcgui.ListItem(label="Επόμενη Σελίδα → (%s/%s)" % (next_page, total_pages))
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=next_url, listitem=next_li, isFolder=True)

    xbmcplugin.setContent(addon_handle, 'movies')
    xbmcplugin.endOfDirectory(addon_handle)

elif mode[0] == 'list_foreign_movies':
    page = args.get('page', ['1'])[0]
    year_filter = args.get('year', [''])[0]
    
    target_url = base_url + "/?filter=movie&sort=recent"
    if year_filter:
        target_url += "&f_year=" + year_filter
    target_url += "&f_language=en&page=" + page

    req = urllib2.Request(target_url, headers=HEADERS)
    response = OPENER.open(req)
    html = response.read()

    card_pattern = re.compile(
        r'<a class="card" href="([^"]+)">\s*<img src="([^"]+)" alt="([^"]+)"[^>]*>\s*<div class="card-meta">\s*<div class="card-title">[^<]+</div>\s*<div class="card-sub">\s*([^\s·]+)\s*· <span class="star-rating">★ ([^<]+)</span>',
        re.DOTALL
    )

    matches = card_pattern.findall(html)
    for href, img_src, alt_title, year, rating in matches:
        item_url = base_url + href
        cleaned_title = decode_html_entities(alt_title)
        
        li = xbmcgui.ListItem(label=cleaned_title)
        li.setArt({'thumb': img_src, 'poster': img_src})
        
        if hasattr(li, 'setInfo'):
            li.setInfo('video', {
                'title': cleaned_title,
                'year': int(year) if year.isdigit() else 0,
                'rating': float(rating) if rating.replace('.', '', 1).isdigit() else 0.0,
                'mediatype': 'movie'
            })
        
        play_route_url = build_url({'mode': 'play_movie', 'url': item_url})
        li.setProperty('IsPlayable', 'true')
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=play_route_url, listitem=li, isFolder=False)

    total_pages = "1"
    pager_total_match = re.search(r'page=(\d+)">\d+</a>\s*</nav>', html)
    if pager_total_match:
        total_pages = pager_total_match.group(1)
    else:
        all_pager_pages = re.findall(r'page=(\d+)[^"]*">(\d+)</a>', html)
        if all_pager_pages:
            total_pages = all_pager_pages[-1][1]
        else:
            page_nums = re.findall(r'page=(\d+)', html)
            if page_nums:
                total_pages = str(max(int(p) for p in page_nums))

    pager_next = re.compile(r'<a href="[^"]*page=(\d+)[^"]*">Επόμενη →</a>')
    next_match = pager_next.search(html)
    if next_match:
        next_page = next_match.group(1)
        next_params = {'mode': 'list_foreign_movies', 'page': next_page}
        if year_filter:
            next_params['year'] = year_filter
            
        next_url = build_url(next_params)
        next_li = xbmcgui.ListItem(label="Επόμενη Σελίδα → (%s/%s)" % (next_page, total_pages))
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=next_url, listitem=next_li, isFolder=True)

    xbmcplugin.setContent(addon_handle, 'movies')
    xbmcplugin.endOfDirectory(addon_handle)

elif mode[0] == 'play_movie':
    movie_page_url = args.get('url', [''])[0]
    
    if movie_page_url:
        try:
            req = urllib2.Request(movie_page_url, headers=HEADERS)
            response = OPENER.open(req)
            movie_html = response.read()

            links = re.findall(r'<a class="btn" href="(.*?)"', movie_html)
            if not links:
                xbmcgui.Dialog().notification('Notice', 'No sources found', xbmcgui.NOTIFICATION_INFO, 3000)
                xbmcplugin.setResolvedUrl(addon_handle, False, xbmcgui.ListItem())
            else:
                labels = ["Play Source %s" % (i + 1) for i in range(len(links))]
                dialog = xbmcgui.Dialog()
                selected = dialog.select("Select Source", labels)
                
                if selected == -1:
                    xbmcplugin.setResolvedUrl(addon_handle, False, xbmcgui.ListItem())
                else:
                    player_url = base_url + links[selected]
                    req_player = urllib2.Request(player_url, headers=HEADERS)
                    p_html = OPENER.open(req_player).read()
                    
                    tok = re.search(r'const\s+_tok\s*=\s*"([^"]+)"', p_html)
                    vid = re.search(r'const\s+_vid\s*=\s*([^\s;]+)', p_html)
                    
                    if not tok or not vid:
                        xbmcgui.Dialog().ok("Σφάλμα", "Δεν βρέθηκαν τα απαραίτητα στοιχεία αναπαραγωγής (_tok / _vid).")
                        xbmcplugin.setResolvedUrl(addon_handle, False, xbmcgui.ListItem())
                    else:
                        api_url = "%s/api/video-src.php?t=%s&v=%s" % (base_url, tok.group(1), vid.group(1).strip())
                        api_headers = dict(HEADERS)
                        api_headers['Referer'] = player_url
                        
                        req_api = urllib2.Request(api_url, headers=api_headers)
                        res = OPENER.open(req_api).read()
                        
                        stream = re.search(r'"(http.*?)"', res)
                        if stream:
                            raw_stream_url = stream.group(1)
                            final_stream_url = raw_stream_url.replace('\\/', '/') + '|User-Agent=' + USER_AGENT
                            
                            item = xbmcgui.ListItem(path=final_stream_url)
                            xbmcplugin.setResolvedUrl(addon_handle, True, item)
                        else:
                            xbmcgui.Dialog().ok("Σφάλμα", "Δεν βρέθηκε σύνδεσμος βίντεο από το API.")
                            xbmcplugin.setResolvedUrl(addon_handle, False, xbmcgui.ListItem())
        except Exception as e:
            xbmcgui.Dialog().notification('Error', str(e), xbmcgui.NOTIFICATION_INFO, 3000)
            xbmcplugin.setResolvedUrl(addon_handle, False, xbmcgui.ListItem())
    else:
        xbmcplugin.setResolvedUrl(addon_handle, False, xbmcgui.ListItem())

elif mode[0] == 'list_seasons':
    show_url = args.get('url', [''])[0]
    poster_src = args.get('poster', [''])[0]
    
    html = get_cached_html(show_url)

    if not poster_src:
        poster_match = re.search(r'<div class="poster-wrap">\s*<img src="([^"]+)"', html)
        if poster_match:
            poster_src = poster_match.group(1)

    season_panel_pattern = re.compile(r'<div class="season-panel[^"]*" id="season-panel-(\d+)">.*?</div>\s*</div>\s*(?=<div class="season-panel|</section>|<div style="order:|\Z)', re.DOTALL)
    panels = season_panel_pattern.findall(html)
    
    if not panels:
        if 'seasons-panel' in html or 'season-tab' in html:
            season_tabs = re.findall(r'<button class="season-tab[^"]*"[^>]*>\s*([^\s<]+)\s*</button>', html)
            if season_tabs:
                for idx, season_num in enumerate(season_tabs):
                    season_label = "Κύκλος %s" % season_num
                    li = xbmcgui.ListItem(label=season_label)
                    if poster_src:
                        li.setArt({'thumb': poster_src, 'poster': poster_src})
                    li.setInfo('video', {'mediatype': 'season', 'season': int(season_num) if season_num.isdigit() else (idx + 1)})
                    
                    route_url = build_url({'mode': 'list_episodes', 'url': show_url, 'panel': str(idx), 'season': season_num, 'poster': poster_src})
                    xbmcplugin.addDirectoryItem(handle=addon_handle, url=route_url, listitem=li, isFolder=True)
            else:
                li = xbmcgui.ListItem(label="Επεισόδια")
                if poster_src:
                    li.setArt({'thumb': poster_src, 'poster': poster_src})
                route_url = build_url({'mode': 'list_episodes', 'url': show_url, 'panel': 'all', 'season': '1', 'poster': poster_src})
                xbmcplugin.addDirectoryItem(handle=addon_handle, url=route_url, listitem=li, isFolder=True)
        else:
            li = xbmcgui.ListItem(label="Επεισόδια")
            if poster_src:
                li.setArt({'thumb': poster_src, 'poster': poster_src})
            route_url = build_url({'mode': 'list_episodes', 'url': show_url, 'panel': 'all', 'season': '1', 'poster': poster_src})
            xbmcplugin.addDirectoryItem(handle=addon_handle, url=route_url, listitem=li, isFolder=True)
    else:
        season_tabs = re.findall(r'<button class="season-tab[^"]*"[^>]*>\s*([^<]+)\s*</button>', html)
        for idx, panel_id in enumerate(panels):
            season_num = season_tabs[idx].strip() if idx < len(season_tabs) else str(idx + 1)
            season_label = "Κύκλος %s" % season_num
            
            li = xbmcgui.ListItem(label=season_label)
            if poster_src:
                li.setArt({'thumb': poster_src, 'poster': poster_src})
            li.setInfo('video', {'mediatype': 'season', 'season': int(season_num) if season_num.isdigit() else (idx + 1)})
            
            route_url = build_url({'mode': 'list_episodes', 'url': show_url, 'panel': str(idx), 'season': season_num, 'poster': poster_src})
            xbmcplugin.addDirectoryItem(handle=addon_handle, url=route_url, listitem=li, isFolder=True)

    xbmcplugin.setContent(addon_handle, 'seasons')
    xbmcplugin.endOfDirectory(addon_handle)

elif mode[0] == 'list_episodes':
    show_url = args.get('url', [''])[0]
    panel_param = args.get('panel', ['0'])[0]
    season_num = int(args.get('season', ['1'])[0] if args.get('season', ['1'])[0].isdigit() else 1)
    poster_src = args.get('poster', [''])[0]

    html = get_cached_html(show_url)

    if not poster_src:
        poster_match = re.search(r'<div class="poster-wrap">\s*<img src="([^"]+)"', html)
        if poster_match:
            poster_src = poster_match.group(1)

    target_panel_html = ""
    if panel_param == 'all':
        target_panel_html = html
    else:
        panel_idx = int(panel_param) if panel_param.isdigit() else 0
        season_panel_pattern = re.compile(r'<div class="season-panel[^"]*" id="season-panel-\d+">(.*?)</div>\s*</div>\s*(?=<div class="season-panel|</section>|<div style="order:|\Z)', re.DOTALL)
        panels = season_panel_pattern.findall(html)
        
        if panels and len(panels) > panel_idx:
            target_panel_html = panels[panel_idx]
        else:
            target_panel_html = html

    ep_rows = target_panel_html.split('<div class="ep-row">')
    if len(ep_rows) <= 1:
        ep_rows = re.split(r'<div[^>]*class="[^"]*ep-row[^"]*"[^>]*>', target_panel_html)

    for row in ep_rows[1:]:
        code_match = re.search(r'<span class="ep-code">(s\d+e\d+)</span>', row, re.IGNORECASE)
        if not code_match:
            continue

        ep_code = code_match.group(1)
        href_match = re.search(r'href="([^"]+)"', row)
        name_match = re.search(r'<div class="ep-name">([^<]+)</div>', row)
        meta_spans = re.findall(r'<span>([^<]+)</span>', row)

        ep_page_url = base_url + href_match.group(1) if href_match else show_url
        ep_name = decode_html_entities(name_match.group(1).strip() if name_match else "")

        s_num = season_num
        e_num = 1
        code_parse = re.search(r's(\d+)e(\d+)', ep_code, re.IGNORECASE)
        if code_parse:
            s_num = int(code_parse.group(1))
            e_num = int(code_parse.group(2))

        display_title = "Κύκλος %d : Επεισόδιο %03d" % (s_num, e_num)

        li = xbmcgui.ListItem(label=display_title)
        if poster_src:
            li.setArt({'thumb': poster_src, 'poster': poster_src})

        ep_date = meta_spans[0] if len(meta_spans) > 0 else ""

        if hasattr(li, 'setInfo'):
            li.setInfo('video', {
                'title': ep_name,
                'season': s_num,
                'episode': e_num,
                'aired': ep_date,
                'mediatype': 'episode'
            })

        play_route_url = build_url({'mode': 'play_episode', 'url': ep_page_url})
        li.setProperty('IsPlayable', 'true' if href_match else 'false')
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=play_route_url, listitem=li, isFolder=False)

    xbmcplugin.setContent(addon_handle, 'episodes')
    xbmcplugin.endOfDirectory(addon_handle)

elif mode[0] == 'play_episode':
    ep_page_url = args.get('url', [''])[0]
    
    if ep_page_url:
        req = urllib2.Request(ep_page_url, headers=HEADERS)
        response = OPENER.open(req)
        ep_html = response.read()

        tok_match = re.search(r'const\s+_tok\s*=\s*"([^"]+)"', ep_html)
        vid_match = re.search(r'const\s+_vid\s*=\s*([^\s;]+)', ep_html)

        if tok_match and vid_match:
            tok_val = tok_match.group(1)
            vid_val = vid_match.group(1).strip()
            
            api_url = "%s/api/video-src.php?t=%s&v=%s" % (base_url, tok_val, vid_val)
            
            # Request the API endpoint to retrieve the media stream link
            api_headers = HEADERS.copy()
            api_headers['Referer'] = ep_page_url
            api_req = urllib2.Request(api_url, headers=api_headers)
            api_response = OPENER.open(api_req)
            api_content = api_response.read()

            stream_match = re.search(r'"(http.*?)"', api_content)
            if stream_match:
                raw_stream_url = stream_match.group(1)
                stream_url = raw_stream_url.replace('\\/', '/')
                
                # Append User-Agent header via Kodi pipe syntax
                final_stream_url = stream_url + '|User-Agent=' + USER_AGENT

                item = xbmcgui.ListItem(path=final_stream_url)
                xbmcplugin.setResolvedUrl(addon_handle, True, item)
            else:
                xbmcgui.Dialog().ok("Σφάλμα", "Δεν βρέθηκε σύνδεσμος βίντεο από το API.")
                xbmcplugin.setResolvedUrl(addon_handle, False, xbmcgui.ListItem())
        else:
            xbmcgui.Dialog().ok("Σφάλμα", "Δεν βρέθηκαν τα απαραίτητα στοιχεία αναπαραγωγής (_tok / _vid).")
            xbmcplugin.setResolvedUrl(addon_handle, False, xbmcgui.ListItem())
    else:
        xbmcplugin.setResolvedUrl(addon_handle, False, xbmcgui.ListItem())