def search_filename(filename, languages):
  title, year = xbmc.getCleanMovieTitle(filename)
  log(__name__, "clean title: \"%s\" (%s)" % (title, year))
  try:
    yearval = int(year)
  except ValueError:
    yearval = 0
  if title and yearval > 1900:
    query_Film(title, year, item['3let_language'], filename)
def Search(item):
  filename = os.path.splitext(os.path.basename(item['file_original_path']))[0]
  log(__name__, "Searching NLondertitels.com='%s', filename='%s', addon_version=%s" % (item, filename, __version__))

  if item['mansearch']:
    search_manual(item['mansearchstr'], filename)
  elif item['title']:
    query_Film(item['title'], item['imdb'], filename)
  else:
    search_filename(filename, item['3let_language'])
def rmdirRecursive(dir):
    """This is a replacement for shutil.rmtree that works better under
    windows. Thanks to Bear at the OSAF for the code."""
    if not os.path.exists(dir):
        return

    if os.path.islink(dir.encode('utf8')):
        os.remove(dir.encode('utf8'))
        return

    # Verify the directory is read/write/execute for the current user
    os.chmod(dir, 0700)

    # os.listdir below only returns a list of unicode filenames if the parameter is unicode
    # Thus, if a non-unicode-named dir contains a unicode filename, that filename will get garbled.
    # So force dir to be unicode.
    try:
        dir = dir.decode('utf8','ignore')
    except:
        log(__name__, "rmdirRecursive: decoding from UTF-8 failed: %s" % dir)
        return

    for name in os.listdir(dir):
        try:
            name = name.decode('utf8','ignore')
        except:
            log(__name__, "rmdirRecursive: decoding from UTF-8 failed: %s" % name)
            continue
        full_name = os.path.join(dir, name)
        # on Windows, if we don't have write permission we can't remove
        # the file/directory either, so turn that on
        if os.name == 'nt':
            if not os.access(full_name, os.W_OK):
                # I think this is now redundant, but I don't have an NT
                # machine to test on, so I'm going to leave it in place
                # -warner
                os.chmod(full_name, 0600)

        if os.path.islink(full_name):
            os.remove(full_name) # as suggested in bug #792
        elif os.path.isdir(full_name):
            rmdirRecursive(full_name)
        else:
            if os.path.isfile(full_name):
                os.chmod(full_name, 0700)
            os.remove(full_name)
    os.rmdir(dir)
def query(searchurl, file_original_path):
  sublinks = []
  socket.setdefaulttimeout(3)
  log(__name__, "search='%s', addon_version=%s" % (searchurl, __version__))
  request = urllib2.Request(searchurl, headers=req_headers)
  request.add_header('Pragma', 'no-cache')
  page = urllib2.build_opener().open(request)
  content = page.read()
  soup = BeautifulSoup(content)
  soup2 = soup.find(id="search")

  file_name = str(os.path.basename(file_original_path)).split("-")[-1].lower()

  if soup2 != None: 
    for subs in soup2("a"):

      try:
        description = subs.findNext("i").string

        sub_original_link = subs['href']

        sub_download_link = sub_original_link.rsplit('/', 1)[0].replace("subtitle", "download")

        link = "%s" % (self_host + sub_download_link)

        sublinks.append({'rating': '0', 'file_name': file_name, 'description': "%s - %s" %(subs.string, description), 'link': link, 'lang': "nl"})

      except:
        log(__name__, "Error in BeautifulSoup")
        pass

  else:
    xbmc.executebuiltin((u'Notification(%s,%s %s)' % (__scriptname__ , __language__(32004), file_name)).encode('utf-8'))

  log(__name__, "sub='%s'" % (sublinks))

  for s in sublinks:
    append_subtitle(s)
def download(link, search_string=""):
  exts = [".srt", ".sub", ".smi", ".ssa", ".ass"]
  subtitle_list = []
  response = urllib2.urlopen(link)

  if xbmcvfs.exists(__temp__):
      rmdirRecursive(__temp__)
  xbmcvfs.mkdirs(__temp__)

  local_tmp_file = os.path.join(__temp__, "nlondertitel.xxx")
  packed = False

  try:
      log(__name__, "Saving subtitles to '%s'" % local_tmp_file)
      local_file_handle = open(local_tmp_file, "wb")
      local_file_handle.write(response.read())
      local_file_handle.close()

      #Check archive type (rar/zip/else) through the file header (rar=Rar!, zip=PK)
      myfile = open(local_tmp_file, "rb")
      myfile.seek(0)
      if myfile.read(1) == 'R':
          typeid = "rar"
          packed = True
          log(__name__, "Discovered RAR Archive")
      else:
          myfile.seek(0)
          if myfile.read(1) == 'P':
              typeid = "zip"
              packed = True
              log(__name__, "Discovered ZIP Archive")
          else:
              typeid = "srt"
              packed = False
              log(__name__, "Discovered a non-archive file")
      myfile.close()
      local_tmp_file = os.path.join(__temp__, "nlondertitel." + typeid)
      os.rename(os.path.join(__temp__, "nlondertitel.xxx"), local_tmp_file)
      log(__name__, "Saving to %s" % local_tmp_file)
  except:
      log(__name__, "Failed to save subtitle to %s" % local_tmp_file)

  if packed:
      xbmc.executebuiltin(('XBMC.Extract("%s","%s")' % (local_tmp_file, __temp__,)).encode('utf-8'), True)

  dirs, files = xbmcvfs.listdir(__temp__)
  
  if dirs:
    path = os.path.join(__temp__, dirs[0].decode('utf-8'))
  else:
    path= __temp__

  for file in xbmcvfs.listdir(path)[1]:
      file = os.path.join(path, file)
      if os.path.splitext(file)[1] in exts:
          if search_string and string.find(string.lower(file), string.lower(search_string)) == -1:
              continue
          log(__name__, "=== returning subtitle file %s" % file)
          subtitle_list.append(file)

  if len(subtitle_list) == 0:
      if search_string:
          xbmc.executebuiltin((u'Notification(%s,%s)' % (__scriptname__ , __language__(32002))).encode('utf-8'))
      else:
          xbmc.executebuiltin((u'Notification(%s,%s)' % (__scriptname__ , __language__(32003))).encode('utf-8'))

  return subtitle_list