<s> '''<STR_LIT>''' <EOL> from singleton import Singleton <EOL> import threading <EOL> import subprocess <EOL> from const import Constant <EOL> from config import Config <EOL> import os <EOL> import logger <EOL> import signal <EOL> log = logger . getLogger ( __name__ ) <EOL> class Cache ( Singleton ) : <EOL> def __init__ ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return <EOL> self . _init = True <EOL> self . const = Constant ( ) <EOL> self . config = Config ( ) <EOL> self . download_lock = threading . Lock ( ) <EOL> self . check_lock = threading . Lock ( ) <EOL> self . downloading = [ ] <EOL> self . aria2c = None <EOL> self . stop = False <EOL> self . enable = self . config . get_item ( "<STR_LIT>" ) <EOL> self . aria2c_parameters = self . config . get_item ( "<STR_LIT>" ) <EOL> def start_download ( self ) : <EOL> check = self . download_lock . acquire ( False ) <EOL> if not check : <EOL> return False <EOL> while True : <EOL> if self . stop : <EOL> break <EOL> if not self . enable : <EOL> break <EOL> self . check_lock . acquire ( ) <EOL> if len ( self . downloading ) <= <NUM_LIT:0> : <EOL> self . check_lock . release ( ) <EOL> break <EOL> data = self . downloading . pop ( ) <EOL> self . check_lock . release ( ) <EOL> song_id = data [ <NUM_LIT:0> ] <EOL> song_name = data [ <NUM_LIT:1> ] <EOL> artist = data [ <NUM_LIT:2> ] <EOL> url = data [ <NUM_LIT:3> ] <EOL> onExit = data [ <NUM_LIT:4> ] <EOL> output_path = Constant . download_dir <EOL> output_file = str ( artist ) + "<STR_LIT>" + str ( song_name ) + "<STR_LIT>" <EOL> try : <EOL> para = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , output_path , '<STR_LIT>' , <EOL> output_file , url ] <EOL> para [ <NUM_LIT:1> : <NUM_LIT:1> ] = self . aria2c_parameters <EOL> self . aria2c = subprocess . Popen ( para , <EOL> stdin = subprocess . PIPE , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE ) <EOL> self . aria2c . wait ( ) <EOL> except Exception : <EOL> log . debug ( str ( song_id ) + "<STR_LIT>" ) <EOL> if self . aria2c . returncode == <NUM_LIT:0> : <EOL> log . debug ( str ( song_id ) + "<STR_LIT>" ) <EOL> onExit ( song_id , output_path + "<STR_LIT:/>" + output_file ) <EOL> self . download_lock . release ( ) <EOL> def add ( self , song_id , song_name , artist , url , onExit ) : <EOL> self . check_lock . acquire ( ) <EOL> self . downloading . append ( [ song_id , song_name , artist , url , onExit ] ) <EOL> self . check_lock . release ( ) <EOL> def quit ( self ) : <EOL> self . stop = True <EOL> try : <EOL> os . kill ( self . aria2c . pid , signal . SIGKILL ) <EOL> except : <EOL> pass </s>
