def __init__(self, artist, title, cut_begining=0, cut_end=0, min_song_duration=60, byterate=1000, base_directory=".", first_song=False): self.artist = artist self.title = title self.cut_begining = cut_begining self.cut_end = cut_end self.min_song_duration = min_song_duration self.byterate = byterate self.base_directory = base_directory self.first_song = first_song if not self.artist and not self.title: print u"{} Skipping...".format(get_log_time()) else: self.filename = u"{} - {}.mp3".format(self.artist, self.title) self.full_path_song = os.path.join(self.base_directory, self.filename) print u"{} Ripping {}".format(get_log_time(), self.filename)
def run(self): time.sleep(self.secondsToSleep) id = int(self.getName()) ts = get_log_time() enchere = encheres[id] enchere.set_status(Enchere_Status.FINISHED) logger.info('[INFO] %s CRONJOB: Close bid with id %s ', ts, id)
def _is_song_valid(self): if not self.artist or not self.title: return False elif os.path.exists(self.full_path_song): print u"{} Skip song {} already exist.".format( get_log_time(), self.filename) return False elif self.first_song: print u"{} Skip song {} may be incomplete because it's the first recorded .".format( get_log_time(), self.filename) return False elif self.get_length_in_seconds() < self.min_song_duration: print u"{} Skip song {} is under {} seconds.".format( get_log_time(), self.filename, self.min_song_duration) return False return True
def start(): ## validation of args duration = request.args.get('duration') if request.args.get('duration') is None: return jsonify( message="", error="you must provide the duration of the bid(in seconds)") ## state mutation bid_begin_date = datetime.datetime.now() id = random.randint(os.getenv('RANDOMIZER_LOWER_VALUE', 0), os.getenv('RANDOMIZER_UPPER_VALUE', 100)) enchere = Enchere(bid_begin_date, request.args.get('duration')) ts = get_log_time() encheres[id] = enchere app.logger.info( '[INFO] %s %s start endpoint: A new bid with %s has begun with exact time=%s', ts, request.remote_addr, id, bid_begin_date) ## start the bid killer duration = int(duration) BidTerminator = CronJobSeconds(duration) BidTerminator.setName(id) BidTerminator.start() bid_duration = datetime.timedelta(seconds=duration) bid_end_date = bid_begin_date + bid_duration app.logger.info( '[INFO] %s %s start endpoint: Create a CRON job to stop bid %s at %s', ts, request.remote_addr, id, bid_end_date) return jsonify(message=id, error="")
def bid(id): ## validation of URI if id not in encheres: return jsonify(message="", error="the required bid doesn't exist") ## validation of args enchere = encheres[id] status = enchere.status if status == Enchere_Status.FINISHED: return jsonify(message="", error="The bid is already finished") amount = request.args.get('amount') if amount is None: return jsonify(message="", error="The amount of bid can not be null") client_name = request.args.get('name') if client_name is None: return jsonify(message="", error="The name of the client can not be null") ts = get_log_time() curr_price = str(enchere.price) curr_client = str(enchere.client) # for pretty log message: seller instead of "" if enchere.client == "": curr_client = "seller" if amount > curr_price: ## state mutation enchere.set_price(amount) enchere.set_client(client_name) amount = str(amount) client_name = str(client_name) app.logger.info( '[INFO] %s %s bid endpoint: bid of client %s accepted because %s is superior to current price %s from %s', ts, request.remote_addr, client_name, amount, curr_price, curr_client) return jsonify(message="success", error="") else: client_name = str(client_name) amount = str(amount) app.logger.info( '[INFO] %s %s bid endpoint: bid of client %s refused because %s is inferior to current price %s from %s', ts, request.remote_addr, id, client_name, amount, curr_price, curr_client) return jsonify( message="", error= "the proposed bid is lower than the current value. Use get Use /getactualprice to get the current price" )
def save(self): """ Save song in base_directory. Except : - If the song already exist. - If the song has no metadata (title and artist) - If this is the first recorded song, the begining may be skipped. - If the song is inferior to self.min_song_duration seconds. """ if self._is_song_valid(): with open(self.full_path_song, "w+") as mp3_file: print u"{} Song {} successfully saved.".format( get_log_time(), self.filename) self.cut_song() mp3_file.write(self.data) self.add_id3_tags()
print "Connecting..." stream = requests.get(args.url, stream=True, headers=build_header()) print_stream_info(stream) block_size = int(stream.headers["icy-metaint"]) byterate = int(stream.headers["icy-br"][:3]) * 1000 / 8 song = None is_first_song = False if args.record_first_song else True try: while 42: mp3_bytes = stream.raw.read(block_size) try: metadata_length = get_metadata_length(stream) except NoDataReceivedError: print "{} Request.raw.read returned an empty string. Process end.".format(get_log_time()) raise RuntimeError if metadata_length: ## we receive metadata from stream: a new song is coming. if song: save_song(song) ## save the current song. is_first_song = False artist, title = metadata_parsers[radio_parser_style]().parse(stream.raw.read(metadata_length)) song = Song(artist, title, args.cut_begining, args.cut_end, args.min_song_duration, byterate, args.base_directory, is_first_song) ## create a new song. song.data += mp3_bytes except KeyboardInterrupt: sys.exit()