def get_top_artist(self): cmd = "mkdir -p {}".format(self.root_dir) log.info("cmd={}".format(cmd)) os.system(cmd) url = "{}/top/artists".format(self.server) a_num = 0 try: resp = requests.get(url, timeout=120) response = resp.json() # response except BaseException as e: print '{}获取artist fail!'.format(url) log.fatal("err={} url={}".format(e, url)) print e print("uri={}".format(url)) artist_list = [] if "artists" in response: for one_artist in response["artists"]: a_id = str(one_artist["id"]).strip() a_name = str(one_artist["name"]).strip() ar = Artist(a_id, a_name) artist_list.append(ar) self.download_one_artist(ar) a_num += 1 time.sleep(1) print("down ok a_num={}".format(a_num)) log.info("down ok a_num={}".format(a_num)) if len(artist_list) < 1: log.fatal("too few top artists len={}".format(len(artist_list))) return log.info("write artist into top list") ar_str_list = [str(a.id) + "$$" + str(a.name) for a in artist_list] ar_str_list_str = ";".join(ar_str_list) dbops.write_top_artist(ar_str_list_str) log.info('write artist into top list success')
def query(self, sql): """query """ self.lock.acquire() results = [] try: self.cursor.execute(sql) results = self.cursor.fetchall() except Exception as e: errormsg = 'query db ERROR(%s):%s' % (e.args[0], e.args[1]) print(errormsg) log.fatal("err={} sql={}".format(errormsg, sql)) finally: self.lock.release() return results
def exec_write(self, sql): """exec dml,ddl""" log.info("mysql write") self.lock.acquire() try: self.cursor.execute(sql) self.conn.commit() except Exception as e: errormsg = 'write db ERROR(%s):%s' % (e.args[0], e.args[1]) print(errormsg) log.fatal("err={} sql={}".format(errormsg, sql)) self.conn.rollback() finally: self.lock.release()
def query(self, sql): """query """ results = [] conn = self.pool.connection() cursor = conn.cursor() try: cursor.execute(sql) results = cursor.fetchall() except Exception as e: errormsg = 'query db ERROR(%s):%s' % (e.args[0], e.args[1]) log.fatal("err={} sql={}".format(errormsg, sql)) finally: cursor.close() conn.close() return results
def exec_write(self, sql): """exec dml,ddl""" log.info("mysql write") conn = self.pool.connection() cursor = conn.cursor() try: cursor.execute(sql) conn.commit() except Exception as e: errormsg = 'write db ERROR(%s):%s' % (e.args[0], e.args[1]) log.fatal("err={} sql={}".format(errormsg, sql)) conn.rollback() finally: cursor.close() conn.close()
def download_top_list(self): url = "{}/top/list?idx=".format(self.server) ids = [i for i in range(34)] for id in ids: if id != 1: continue try: uri = url + "{}".format(id) resp = requests.get(uri) response = resp.json() print response except Exception as e: print 'top list!' log.fatal("toplist fail err={} uri={}".format(e, uri)) print e return None song_list = [] if "playlist" in response and "tracks" in response["playlist"]: i = 0 for track in response["playlist"]["tracks"]: song_id = str(track["id"]) song_name = track["name"] m = Music(song_id, song_name) song_list.append(m) print("music name={} id={}".format(song_name, song_id)) self.thread_num += 1 self.download_one_song(m) i += 1 # break log.info("down ok a_num={}".format(len(song_list))) if len(song_list) < 5: log.fatal("too few top song len={}".format(len(song_list))) return log.info("write song into top list") song_str_list = [ str(a.id) + "$$" + str(a.name) for a in song_list[:200] ] song_str_list_str = ";".join(song_str_list) dbops.write_top_song(song_str_list_str) log.info('write song into top list success')