def download_subtitles(subtitles_list, pos, zip_subs, tmp_sub_dir, sub_folder,
                       session_id):  #standard input
    page_id = subtitles_list[pos]["page_id"]
    subtitle_id = subtitles_list[pos]["subtitle_id"]

    icon = os.path.join(__cwd__, "icon.png")
    delay = 20
    download_wait = delay
    downloader = TorecSubtitlesDownloader()
    # Wait the minimal time needed for retrieving the download link
    for i in range(int(download_wait)):
        downloadLink = downloader.getDownloadLink(page_id, subtitle_id, False)
        if (downloadLink != None):
            break
        line2 = "download will start in %i seconds" % (delay, )
        xbmc.executebuiltin("XBMC.Notification(%s,%s,1000,%s)" %
                            (__scriptname__, line2, icon))
        delay -= 1
        time.sleep(1)

    log(__name__, "Downloading subtitles from '%s'" % downloadLink)
    (subtitleData, subtitleName) = downloader.download(downloadLink)

    log(__name__, "Saving subtitles to '%s'" % zip_subs)
    downloader.saveData(zip_subs, subtitleData, False)

    return True, "Hebrew", ""  #standard output
def download_subtitles (subtitles_list, pos, zip_subs, tmp_sub_dir, sub_folder, session_id): #standard input
    page_id                 = subtitles_list[pos]["page_id"]  
    subtitle_id             = subtitles_list[pos]["subtitle_id"]  

    icon =  os.path.join(__cwd__,"icon.png")
    delay = 20
    download_wait = delay
    downloader = TorecSubtitlesDownloader()
    # Wait the minimal time needed for retrieving the download link
    for i in range (int(download_wait)):
        downloadLink =  downloader.getDownloadLink(page_id, subtitle_id, False)
        if (downloadLink != None):
            break
        line2 = "download will start in %i seconds" % (delay,)
        xbmc.executebuiltin("XBMC.Notification(%s,%s,1000,%s)" % (__scriptname__,line2,icon))
        delay -= 1
        time.sleep(1)
        
    log(__name__ ,"Downloading subtitles from '%s'" % downloadLink)
    (subtitleData, subtitleName) = downloader.download(downloadLink)
    
    log(__name__ ,"Saving subtitles to '%s'" % zip_subs)
    downloader.saveData(zip_subs, subtitleData, False)
        
    return True,"Hebrew", "" #standard output
Beispiel #3
0
def download(page_id, subtitle_id,filename, stack=False):
    files = glob.glob(os.path.join(__temp__, "*.srt"))
    for f in files:
      log(__name__, "deleting %s" % f)
      os.remove(f)

    subtitle_list = []
    exts = [".srt", ".sub"]
    
    delay         = 20
    download_wait = delay
    downloader    = TorecSubtitlesDownloader()
    start_time    = time.time()

    try:
        # Wait the minimal time needed for retrieving the download link
        for i in range (int(download_wait)):
            result =  downloader.getDownloadLink(page_id, subtitle_id, False)
            if (result != None):
                break
            log(__name__ ,"download will start in %i seconds" % (delay,))
            delay -= 1
            time.sleep(1)
    except:
        log( __name__, "failed to connect to service for subtitle download")
        return subtitle_list
        
    if result != None:
        log(__name__ ,"Downloading subtitles from '%s'" % result)
        
        (subtitleData, subtitleName) = downloader.download(result)
        zip = os.path.join(__temp__, "Torec.zip")
        with open(zip, "wb") as subFile:
            subFile.write(subtitleData)

        xbmc.executebuiltin(('XBMC.Extract("%s","%s")' % (zip,__temp__,)).encode('utf-8'), True)

        for file in xbmcvfs.listdir(__temp__)[1]:
            log(__name__, "file=%s" % file)
            file = os.path.join(__temp__, file)
            if (os.path.splitext(file)[1] in exts):
              convert_to_utf(file)
              subtitle_list.append(file)
      
    log(__name__, "Overall download took %f" % (time.time() - start_time))
    return subtitle_list
def download(sub_id, option_id, filename, stack=False):
    result = None
    subtitle_list = []
    exts = [".srt", ".sub"]
    downloader = TorecSubtitlesDownloader()
    start_time = time.time()

    delete_old_subs()

    try:
        result = downloader.get_download_link(sub_id, option_id)
    except Exception as e:
        log(__name__,"failed to connect to service for subtitle download %s" % e)
        return subtitle_list
        
    if result is not None:
        log(__name__, "Downloading subtitles from '%s'" % result)
        
        (subtitle_data, subtitle_name) = downloader.download(result)
        (file_name, file_ext) = os.path.splitext(subtitle_name)
        archive_file = os.path.join(__temp__, "Torec%s" % file_ext)
        with open(archive_file, "wb") as subFile:
            subFile.write(subtitle_data)

        xbmc.executebuiltin(
            ('XBMC.Extract("%s","%s")' % (archive_file, __temp__,)
             ).encode('utf-8'), True)

        for file_ in xbmcvfs.listdir(__temp__)[1]:
            ufile = file_.decode('utf-8')
            log(__name__, "file=%s" % ufile)
            file_ = os.path.join(__temp__, ufile)
            if os.path.splitext(ufile)[1] in exts:
                convert_to_utf(file_)
                subtitle_list.append(file_)
      
    log(__name__, "Overall download took %f" % (time.time() - start_time))
    return subtitle_list
def DownloadTorecSubtitle(search_string, release, dest_dir):
    subtitles_list = []

    downloader = TorecSubtitlesDownloader()
    metadata = downloader.getSubtitleMetaData(search_string)

    for option in metadata.options:
        if release in option.name:
            subtitles_list.append({'page_id'       : metadata.id,
                                   'filename'      : option.name,
                                   'subtitle_id'   : option.id
                                })

    # If there is one match, download it
    if len(subtitles_list) == 1:
        sub = 0
    else:
        print 'Select sub : '

        for i,sub in enumerate(subtitles_list):
          print '%s) %s' % (i, sub['filename'])

        sub = int(raw_input(' > '))

    page_id = subtitles_list[sub]["page_id"]
    subtitle_id = subtitles_list[sub]["subtitle_id"]
    filename = subtitles_list[sub]["filename"]

    downloader = TorecSubtitlesDownloader()
    downloadLink =  downloader.getDownloadLink(page_id, subtitle_id, False)
    (subtitleData, subtitleName) = downloader.download(downloadLink)
    temp_dir = tempfile.mkdtemp()
    file_name = downloader.saveData('%s/%s' % (temp_dir, filename) , subtitleData, True)
    src = os.path.join(temp_dir,file_name)
    dst = os.path.join(dest_dir,file_name)
    log(__name__, "Moving file to %s" % dst)
    shutil.move(src,dst)
def download(page_id, subtitle_id,filename, stack=False):
    subtitle_list = []
    exts = [".srt", ".sub"]
    
    delay = 20
    download_wait = delay
    downloader = TorecSubtitlesDownloader()
    
    try:
        # Wait the minimal time needed for retrieving the download link
        for i in range (int(download_wait)):
            result =  downloader.getDownloadLink(page_id, subtitle_id, False)
            if (result != None):
                break
            log(__name__ ,"download will start in %i seconds" % (delay,))
            delay -= 1
            time.sleep(1)
    except:
        log( __name__, "failed to connect to service for subtitle download")
        return subtitle_list
        
    if result != None:
        log(__name__ ,"Downloading subtitles from '%s'" % result)
        
        (subtitleData, subtitleName) = downloader.download(result)
        
        zip = os.path.join( __temp__, "Torec.zip")
        downloader.saveData(zip, subtitleData)

        for file in xbmcvfs.listdir(__temp__)[1]:
            log(__name__, "file=%s" % file)
            file = os.path.join(__temp__, file)
            if (os.path.splitext( file )[1] in exts):
                subtitle_list.append(file)
      
    return subtitle_list
Beispiel #7
0
def download(page_id, subtitle_id, filename, stack=False):
    subtitle_list = []
    exts = [".srt", ".sub"]

    delay = 20
    download_wait = delay
    downloader = TorecSubtitlesDownloader()

    try:
        # Wait the minimal time needed for retrieving the download link
        for i in range(int(download_wait)):
            result = downloader.getDownloadLink(page_id, subtitle_id, False)
            if (result != None):
                break
            log(__name__, "download will start in %i seconds" % (delay, ))
            delay -= 1
            time.sleep(1)
    except:
        log(__name__, "failed to connect to service for subtitle download")
        return subtitle_list

    if result != None:
        log(__name__, "Downloading subtitles from '%s'" % result)

        (subtitleData, subtitleName) = downloader.download(result)

        zip = os.path.join(__temp__, "Torec.zip")
        downloader.saveData(zip, subtitleData)

        for file in xbmcvfs.listdir(__temp__)[1]:
            log(__name__, "file=%s" % file)
            file = os.path.join(__temp__, file)
            if (os.path.splitext(file)[1] in exts):
                subtitle_list.append(file)

    return subtitle_list
Beispiel #8
0
class MovieTests(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.downloader = TorecSubtitlesDownloader()

    def test_search_movie_sanity(self):
        item = self._create_test_valid_item()
        options = self.downloader.search_movie(item['title'])
        self.assertIsNotNone(options)
        self.assertEqual(len(options), 22)

    def test_search_inexisting_movie(self):
        item = {
            'title': 'finding mori',
        }

        options = self.downloader.search_movie(item['title'])
        self.assertIsNone(options)

    def test_download_movie_sanity(self):
        item = self._create_test_valid_item()
        options = self.downloader.search_movie(item['title'])

        option = options[0]
        page_id = option.sub_id
        subtitle_id = option.option_id

        result = self.downloader.get_download_link(page_id, subtitle_id)
        subtitleData, fileName = self.downloader.download(result)
        self.assertIsNotNone(subtitleData)

        self._assert_subtitle_data(subtitleData, fileName)

    def _create_test_valid_item(self):
        return {
            'title': 'finding dory',
        }

    def _assert_subtitle_data(self, subtitleData, fileName):
        extension = os.path.splitext(fileName)[1]

        temp = tempfile.NamedTemporaryFile()
        temp.write(subtitleData)
        temp.flush()

        if (extension == ".zip"):
            rf = zipfile.ZipFile(temp.name)
        elif (extension == ".rar"):
            rf = rarfile.RarFile(temp.name)
        else:
            fail("unknown extension found %s", extension)

        for f in rf.infolist():
            data = rf.read(f.filename)

            temp = tempfile.NamedTemporaryFile()
            temp.write(data)
            temp.flush()

            self.assertIsNotNone(temp.read())
            break
class MovieTests(unittest.TestCase):
	@classmethod
	def setUpClass(self):
		self.downloader  = TorecSubtitlesDownloader()

	def test_search_movie_sanity(self):
		item    = self._create_test_valid_item()
		options = self.downloader.search_movie(item['title'])
		self.assertIsNotNone(options)
		self.assertEqual(len(options), 22)

	def test_search_inexisting_movie(self):
		item = {
			'title': 'finding mori',
		}

		options = self.downloader.search_movie(item['title'])
		self.assertIsNone(options)

	def test_download_movie_sanity(self):
		item    = self._create_test_valid_item()
		options = self.downloader.search_movie(item['title'])

		option      = options[0]
		page_id     = option.sub_id
		subtitle_id = option.option_id

		result                 = self.downloader.get_download_link(page_id, subtitle_id)
		subtitleData, fileName = self.downloader.download(result)
		self.assertIsNotNone(subtitleData)

		self._assert_subtitle_data(subtitleData, fileName)

	def _create_test_valid_item(self):
		return {
			'title': 'finding dory',
		}

	def _assert_subtitle_data(self, subtitleData, fileName):
		extension = os.path.splitext(fileName)[1]

		temp = tempfile.NamedTemporaryFile()
		temp.write(subtitleData)
		temp.flush()

		if (extension == ".zip"):
			rf = zipfile.ZipFile(temp.name)
		elif (extension == ".rar"):
			rf = rarfile.RarFile(temp.name)
		else:
			fail("unknown extension found %s", extension)

		for f in rf.infolist():
			data = rf.read(f.filename)

			temp = tempfile.NamedTemporaryFile()
			temp.write(data)
			temp.flush()

			self.assertIsNotNone(temp.read())
			break
class TVShowTests(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.downloader = TorecSubtitlesDownloader()

    def test_search_tvshow_sanity(self):
        item = self._create_test_valid_item()
        options = self.downloader.search_tvshow(item['tvshow'], item['season'],
                                                item['episode'])
        self.assertIsNotNone(options)
        self.assertGreaterEqual(len(options), 5)

    def test_search_tvshow_last_season_episode(self):
        item = {'tvshow': 'house of cards', 'season': '3', 'episode': '13'}

        options = self.downloader.search_tvshow(item['tvshow'], item['season'],
                                                item['episode'])
        self.assertIsNotNone(options)
        self.assertGreaterEqual(len(options), 1)

    def test_search_inexisting_tvshow(self):
        item = {'tvshow': 'house of lards', 'season': '3', 'episode': '1'}

        options = self.downloader.search_tvshow(item['tvshow'], item['season'],
                                                item['episode'])
        self.assertIsNone(options)

    def test_search_existing_tvshow_with_no_subtitles(self):
        item = {'tvshow': 'Cowboy Bebop', 'season': '1', 'episode': '1'}

        options = self.downloader.search_tvshow(item['tvshow'], item['season'],
                                                item['episode'])
        self.assertIsNone(options)

    def test_download_tvshow_sanity(self):
        item = self._create_test_valid_item()
        options = self.downloader.search_tvshow(item['tvshow'], item['season'],
                                                item['episode'])

        option = options[-1]
        page_id = option.sub_id
        subtitle_id = option.option_id

        download_link = self.downloader.get_download_link(page_id, subtitle_id)
        self.assertTrue(len(download_link) != 0)

        subtitleData, fileName = self.downloader.download(download_link)
        self.assertIsNotNone(subtitleData)

        self._assert_subtitle_data(subtitleData, fileName)

    def _create_test_valid_item(self):
        return {'tvshow': 'house of cards', 'season': '3', 'episode': '1'}

    def _assert_subtitle_data(self, subtitleData, fileName):
        extension = os.path.splitext(fileName)[1]

        temp = tempfile.NamedTemporaryFile()
        temp.write(subtitleData)
        temp.flush()

        if (extension == ".zip"):
            rf = zipfile.ZipFile(temp.name)
        elif (extension == ".rar"):
            rf = rarfile.RarFile(temp.name)
        else:
            fail("unknown extension found %s", extension)

        for f in rf.infolist():
            data = rf.read(f.filename)

            temp = tempfile.NamedTemporaryFile()
            temp.write(data)
            temp.flush()

            self.assertIsNotNone(temp.read())
            break