def change_config_file(ip, port): home_dir = get_home_dir() config_file = os.path.join(home_dir, 'torrent_config.txt') new_ip = 'TORRENT_STREAM_IP=' + ip + ':' + str(port) content = open(config_file, 'r').read() content = re.sub('TORRENT_STREAM_IP=[^\n]*', new_ip, content) f = open(config_file, 'w') f.write(content) f.close()
def change_config_file(ip, port): home_dir = get_home_dir() config_file = os.path.join(home_dir, "torrent_config.txt") new_ip = "TORRENT_STREAM_IP=" + ip + ":" + str(port) content = open(config_file, "r").read() content = re.sub("TORRENT_STREAM_IP=[^\n]*", new_ip, content) f = open(config_file, "w") f.write(content) f.close()
def setUp(self): global home, test_file BASEDIR, BASEFILE = os.path.split(os.path.abspath(__file__)) parent_basedir, __ = os.path.split(BASEDIR) sys.path.insert(0, parent_basedir) k_dir = os.path.join(parent_basedir, 'kawaii_player') sys.path.insert(0, k_dir) home = get_home_dir(mode='test') if not os.path.exists(home): os.makedirs(home) tmp_dir = os.path.join(home, 'tmp') if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) test_file = os.path.join(tmp_dir, 'write_test.txt') print(test_file)
def get_yt_url(url, quality, ytdl_path, logger, mode=None): final_url = '' url = url.replace('"', '') m = [] home_dir = get_home_dir() ytdl_stamp = os.path.join(home_dir, 'tmp', 'ytdl_update_stamp.txt') if ytdl_path: if ytdl_path == 'default': youtube_dl = 'youtube-dl' else: if os.path.exists(ytdl_path): youtube_dl = ytdl_path else: if ytdl_path.endswith('ytdl') or ytdl_path.endswith('ytdl.exe'): send_notification('Please Wait! Getting Latest youtube-dl') youtube_dl = ytdl_path if ytdl_path.endswith('ytdl'): ccurl('https://yt-dl.org/downloads/latest/youtube-dl'+'#-o#'+ytdl_path) subprocess.Popen(['chmod', '+x', ytdl_path]) else: ccurl('https://yt-dl.org/latest/youtube-dl.exe'+'#-o#'+ytdl_path) update_time = str(int(time.time())) if not os.path.exists(ytdl_stamp): f = open(ytdl_stamp, 'w') f.write(update_time) f.close() else: send_notification('youtube-dl path does not exists!') youtube_dl = 'youtube-dl' else: youtube_dl = 'youtube-dl' logger.info(youtube_dl) if '/watch?' in url: a = url.split('?')[-1] b = a.split('&') if b: for i in b: j = i.split('=') k = (j[0], j[1]) m.append(k) else: j = a.split('=') k = (j[0], j[1]) m.append(k) d = dict(m) logger.info('----dict--arguments---generated---{0}'.format(d)) try: url = 'https://m.youtube.com/watch?v='+d['v'] except: pass try: if mode == 'TITLE': if os.name == 'posix': final_url = subprocess.check_output([youtube_dl, '-e', url]) else: final_url = subprocess.check_output([youtube_dl, '-e', url], shell=True) final_url = str(final_url, 'utf-8') final_url = final_url.replace(' - YouTube', '', 1) else: if mode == 'music': quality = 'best' if os.name == 'posix': if quality == 'sd480p': if mode: if mode == 'offline': final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', 'best', '-g', '--playlist-end', '1', url]) final_url = str(final_url, 'utf-8') elif mode == 'music' or (mode == 'a+v' and ytdl_path != 'default'): final_url = subprocess.check_output( [youtube_dl, '-f', 'bestvideo[height<=480]+bestaudio', '-g', url]) final_url = str(final_url, 'utf-8') final_url = final_url.strip() logger.info(final_url) if '\n' in final_url: vid, aud = final_url.split('\n') final_url = vid+'::'+aud else: final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', '18', '-g', '--playlist-end', '1', url]) final_url = str(final_url, 'utf-8') else: final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', '18', '-g', '--playlist-end', '1', url]) final_url = str(final_url, 'utf-8') elif quality == 'sd': final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', '18', '-g', '--playlist-end', '1', url]) final_url = str(final_url, 'utf-8') elif quality == 'best': if mode: if mode == 'offline': final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', 'best', '-g', '--playlist-end', '1', url]) final_url = str(final_url, 'utf-8') elif mode == 'music' or (mode == 'a+v' and ytdl_path != 'default'): final_url = subprocess.check_output( [youtube_dl, '-g', url]) final_url = str(final_url, 'utf-8') final_url = final_url.strip() logger.info(final_url) if '\n' in final_url: vid, aud = final_url.split('\n') final_url = vid+'::'+aud else: if ytdl_path == 'default': final_url = url else: final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', 'best', '-g', '--playlist-end', '1', url]) final_url = str(final_url, 'utf-8') else: if ytdl_path == 'default': final_url = url else: final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', 'best', '-g', '--playlist-end', '1', url]) final_url = str(final_url, 'utf-8') elif quality == 'hd': try: final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', '22', '-g', '--playlist-end', '1', url]) final_url = str(final_url, 'utf-8') except: final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', '18', '-g', '--playlist-end', '1', url]) final_url = str(final_url, 'utf-8') else: if quality == 'sd480p': if mode: if mode == 'offline': final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', 'best', '-g', '--playlist-end', '1', url], shell=True) final_url = str(final_url, 'utf-8') elif mode == 'music' or (mode == 'a+v' and ytdl_path != 'default'): final_url = subprocess.check_output( [youtube_dl, '-f', 'bestvideo[height<=480]+bestaudio', '-g', url], shell=True) final_url = str(final_url, 'utf-8') final_url = final_url.strip() logger.info(final_url) if '\n' in final_url: vid, aud = final_url.split('\n') final_url = vid+'::'+aud else: final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', '18', '-g', '--playlist-end', '1', url], shell=True) final_url = str(final_url, 'utf-8') else: final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', '18', '-g', '--playlist-end', '1', url], shell=True) final_url = str(final_url, 'utf-8') elif quality == 'sd': final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', '18', '-g', '--playlist-end', '1', url], shell=True) final_url = str(final_url, 'utf-8') elif quality == 'best': if mode: if mode == 'offline': final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', 'best', '-g', '--playlist-end', '1', url], shell=True) final_url = str(final_url, 'utf-8') elif mode == 'music' or (mode == 'a+v' and ytdl_path != 'default'): final_url = subprocess.check_output( [youtube_dl, '-g', url], shell=True) final_url = str(final_url, 'utf-8') final_url = final_url.strip() logger.info(final_url) if '\n' in final_url: vid, aud = final_url.split('\n') final_url = vid+'::'+aud else: final_url = url else: final_url = url elif quality == 'hd': try: final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', '22', '-g', '--playlist-end', '1', url], shell=True) final_url = str(final_url, 'utf-8') except: final_url = subprocess.check_output( [youtube_dl, '--youtube-skip-dash-manifest', '-f', '18', '-g', '--playlist-end', '1', url], shell=True) final_url = str(final_url, 'utf-8') except Exception as e: logger.info('--error in processing youtube url--{0}'.format(e)) txt = 'Please Update youtube-dl' send_notification(txt) final_url = '' updated_already = False if ytdl_path.endswith('ytdl') or ytdl_path.endswith('ytdl.exe'): if os.path.exists(ytdl_stamp): content = open(ytdl_stamp, 'r').read() try: old_time = int(content) except Exception as e: log.info(e) old_time = int(time.time()) - 3600 cur_time = int(time.time()) if (cur_time - old_time < 24*3600): updated_already = True if not updated_already: send_notification('Please Wait! Getting Latest youtube-dl') if ytdl_path.endswith('ytdl'): ccurl('https://yt-dl.org/downloads/latest/youtube-dl'+'#-o#'+ytdl_path) subprocess.Popen(['chmod', '+x', ytdl_path]) else: ccurl('https://yt-dl.org/latest/youtube-dl.exe'+'#-o#'+ytdl_path) send_notification('Updated youtube-dl, Now Try Playing Video Again!') update_time = str(int(time.time())) if os.path.exists(ytdl_stamp): os.remove(ytdl_stamp) if not os.path.exists(ytdl_stamp): f = open(ytdl_stamp, 'w') f.write(update_time) f.close() else: send_notification('youtube-dl is already newest version') logger.debug('yt-link:>>{0}'.format(final_url)) return final_url
write_files(test_file, content, line_by_line=True, mode='test')) def test_write_append(self): content = 'append this line' self.assertIsNone( write_files(test_file, content, line_by_line=True, mode='test')) def test_write_block(self): content = 'write this line' self.assertIsNone( write_files(test_file, content, line_by_line=False, mode='test')) if __name__ == '__main__': global home, test_file BASEDIR, BASEFILE = os.path.split(os.path.abspath(__file__)) parent_basedir, __ = os.path.split(BASEDIR) sys.path.insert(0, parent_basedir) k_dir = os.path.join(parent_basedir, 'kawaii_player') sys.path.insert(0, k_dir) from player_functions import write_files, get_home_dir home = get_home_dir(mode='test') if not os.path.exists(home): os.makedirs(home) tmp_dir = os.path.join(home, 'tmp') if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) test_file = os.path.join(tmp_dir, 'write_test.txt') print(test_file) unittest.main()
def get_yt_url(url, quality, ytdl_path, logger, mode=None): final_url = '' url = url.replace('"', '') m = [] home_dir = get_home_dir() ytdl_stamp = os.path.join(home_dir, 'tmp', 'ytdl_update_stamp.txt') yt_sub_folder = os.path.join(home_dir, 'External-Subtitle') if ytdl_path: if ytdl_path == 'default': youtube_dl = 'youtube-dl' else: if os.path.exists(ytdl_path): youtube_dl = ytdl_path else: if ytdl_path.endswith('ytdl') or ytdl_path.endswith( 'ytdl.exe'): send_notification('Please Wait! Getting Latest youtube-dl') youtube_dl = ytdl_path if ytdl_path.endswith('ytdl'): ccurl('https://yt-dl.org/downloads/latest/youtube-dl' + '#-o#' + ytdl_path) subprocess.Popen(['chmod', '+x', ytdl_path]) else: ccurl('https://yt-dl.org/latest/youtube-dl.exe' + '#-o#' + ytdl_path) update_time = str(int(time.time())) if not os.path.exists(ytdl_stamp): f = open(ytdl_stamp, 'w') f.write(update_time) f.close() else: send_notification('youtube-dl path does not exists!') youtube_dl = 'youtube-dl' else: youtube_dl = 'youtube-dl' logger.info(youtube_dl) ytdl_extra = False if '/watch?' in url and 'youtube.com' in url: a = url.split('?')[-1] b = a.split('&') if b: for i in b: j = i.split('=') k = (j[0], j[1]) m.append(k) else: j = a.split('=') k = (j[0], j[1]) m.append(k) d = dict(m) logger.info('----dict--arguments---generated---{0}'.format(d)) try: url = 'https://www.youtube.com/watch?v=' + d['v'] except: pass elif url.startswith('ytdl:'): url = url.replace('ytdl:', '', 1) ytdl_extra = True else: ytdl_extra = True try: if mode == 'TITLE': if os.name == 'posix': final_url = subprocess.check_output([youtube_dl, '-e', url]) else: final_url = subprocess.check_output([youtube_dl, '-e', url], shell=True) final_url = str(final_url, 'utf-8') final_url = final_url.replace(' - YouTube', '', 1) else: if quality == 'sd': res = 360 elif quality == 'hd': res = 720 elif quality == 'sd480p': res = 480 else: res = 4000 if quality == 'best' and ytdl_path == 'default': if ytdl_extra: final_url = 'ytdl:' + url else: final_url = url time.sleep(1) else: final_url = get_final_for_resolution(url, youtube_dl, logger, ytdl_extra, resolution=res, mode=mode, sub_folder=yt_sub_folder) except Exception as e: logger.info('--error in processing youtube url--{0}'.format(e)) txt = 'Please Update youtube-dl' send_notification(txt) final_url = '' updated_already = False if ytdl_path.endswith('ytdl') or ytdl_path.endswith('ytdl.exe'): if os.path.exists(ytdl_stamp): content = open(ytdl_stamp, 'r').read() try: old_time = int(content) except Exception as e: logger.info(e) old_time = int(time.time()) - 3600 cur_time = int(time.time()) if (cur_time - old_time < 24 * 3600): updated_already = True if not updated_already: send_notification('Please Wait! Getting Latest youtube-dl') if ytdl_path.endswith('ytdl'): ccurl('https://yt-dl.org/downloads/latest/youtube-dl' + '#-o#' + ytdl_path) subprocess.Popen(['chmod', '+x', ytdl_path]) else: ccurl('https://yt-dl.org/latest/youtube-dl.exe' + '#-o#' + ytdl_path) send_notification( 'Updated youtube-dl, Now Try Playing Video Again!') update_time = str(int(time.time())) if os.path.exists(ytdl_stamp): os.remove(ytdl_stamp) if not os.path.exists(ytdl_stamp): f = open(ytdl_stamp, 'w') f.write(update_time) f.close() else: send_notification('youtube-dl is already newest version') logger.debug('yt-link:>>{0}'.format(final_url)) if mode == 'TITLE' and not final_url: final_url = url.split('/')[-1] return final_url