def get_comment(id):
    try:
        offset = 0
        url, data = crypt_api(id, offset)
        json_text = get_json(url, data)
        json_dict = json.loads(json_text.decode("utf-8"))
        comments_sum = json_dict['total']
        for i in range(0, comments_sum, 20):
            offset = i
            url, data = crypt_api(id, offset)
            json_text = get_json(url, data)
            json_dict = json.loads(json_text.decode("utf-8"))
            json_comment = json_dict['comments']
            for json_comment in json_comment:
                user_id = json_comment['user']['userId']
                user_name = json_comment['user']['nickname']
                comment = json_comment['content']
                #添加评论的ID,名字以及评论到数据库中
                music_mysql.insert_commnet(user_id, user_name, comment)
                print('id = ', end="")
                print(user_id, end="")
                print(':', end="")
                print(user_name, end="")
                print(':', end="")
                print(comment)
                print('已经添加到user_comment数据库中啦')
            # time.sleep(1)
    except Exception as e:
        print('出现错误啦~错误是:', e)
def get_20comment(id, pre_begin, begin, comments_sum, step):
    for i in range(pre_begin + begin, comments_sum, step):

        offset = i
        url, data = crypt_api(id, offset)
        json_text = get_json(url, data)
        json_dict = json.loads(json_text.decode("utf-8"))
        json_comment = json_dict['comments']
        for json_comment in json_comment:
            user_id = json_comment['user']['userId']
            user_name = json_comment['user']['nickname']
            comment = json_comment['content']
            # 添加评论的ID,名字以及评论到数据库中
            music_mysql.insert_commnet(user_id, id, user_name, comment)
            # print('song_id='+str(id))
            print('comment by:' + str(user_id))
            # print('user_comment:'+user_name).decode("utf8")
            # print(':', end="")
            # print(comment)
            # print '已经添加到user_comment数据库中啦'.decode("utf8")
        time.sleep(1)

        #线程运行完成标志 vm.comment_done = -1
        vm.comment_done += 20
        print vm.comment_done
        if abs(pre_begin + vm.comment_done - comments_sum) <= 20:
            vm.comment_done = -1
            print "===================================="
            print "comment_done=" + str(vm.comment_done)
            print "===================================="
def thread_get_comment(id,offset):
    global xsum
    global result
    mon = 0
    day = 0
    print("进入线程offset为:",offset)
    try:
        url, data = crypt_api(id, offset)
        json_text = get_json(url, data)
        json_dict = json.loads(json_text.decode("utf-8"))
        json_comment = json_dict['comments']
        for json_comment in json_comment:
            user_id = json_comment['user']['userId']
            user_name = json_comment['user']['nickname']
            comment_time = json_comment['time']
            comment = json_comment['content']
            trs_time = time.localtime(comment_time*0.001)
            year = trs_time.tm_year
            mon = trs_time.tm_mon
            day = trs_time.tm_mday
            limit_date = Config.Date
            get_date = str(year)+'-'+str(mon)+'-'+str(day)
            trs_limit_date = time.mktime(time.strptime(limit_date,'%Y-%m-%d'))
            trs_get_date = time.mktime(time.strptime(get_date,'%Y-%m-%d'))
            print(trs_limit_date,trs_get_date)
            #只获取Date之后的评论
            if trs_get_date < trs_limit_date:
                break
            # 添加评论的ID,名字以及评论到数据库中
            if user_id==Config.TestId or user_id==Config.TestId2:
                hour = trs_time.tm_hour
                minute = trs_time.tm_min
                sec = trs_time.tm_sec
                date = str(year)+'-'+str(mon)+'-'+str(day)+' '+str(hour)+':'+str(minute)+':'+str(sec)
                music_mysql.insert_commnet(user_id, user_name, comment,date,id)
                #print('time = ',trs_time.tm_year,'-',trs_time.tm_mon,'-',trs_time.tm_mday, end=" ")
                print('id = ',user_id,user_name,'已经添加到user_comment数据库中啦')
        lock.acquire()
        xsum = xsum + 20
        print('已检查',xsum,'个评论,时间为',year,mon,day)
        lock.release()

    except Exception as e:
        lock.acquire()
        print('出现错误啦~错误是:', e)
        tmp_ip = json_text['http:'].split('//')[1]
        result.remove(tmp_ip)
        lock.release()