def start_barrage_service(request): barrage_thread = get_barrage_thread() try: if barrage_thread.get_stop_status(): barrage_thread.start() message = "start barrage service success" else: message = "start barrage service success, service have started" except Exception as e: message = "start barrage service fail:{}".format(e) get_logger().debug(message) return HttpResponse(message)
def run(self): now = datetime.now() try: bar = Barrage() bar.douyu_id = self.douyu_id bar.douyu_name = self.douyu_name bar.barrage_content = self.barrage_content bar.user_level = self.user_level bar.user_image = self.user_image bar.room_id = self.room_id bar.barrage_time = now.strftime("%Y-%m-%d %H:%M:%S") bar.barrage_date = now.strftime("%Y-%m-%d") bar.room_status = self.get_room_status() bar.save() except Exception as e: get_logger().error("Save Barrage Fail") get_logger().error(e)
def scheduler_close_task(): # Save the check-in data to the text (room_id_date_signin.txt), # and then the check-in bullet screen cache; # So does the barrage cache now = datetime.now() print(signin_cache.get_signin_cache()) print(barrage_cache.get_barrage_cache()) get_logger().debug(signin_cache.get_signin_cache()) get_logger().debug(barrage_cache.get_barrage_cache()) with open( '{2}/{1}_{0}_sign.txt'.format(now.strftime("%Y-%m-%d"), settings.APP_ROOM_ID, BASE_DIR), 'a+') as sign_f: for key, value in signin_cache.get_signin_cache().items(): sign_f.writelines("{0}~{1}\n".format(key, value)) signin_cache.reset_signin_cache() with open( '{2}/{1}_{0}_barrage_number.txt'.format(now.strftime("%Y-%m-%d"), settings.APP_ROOM_ID, BASE_DIR), 'w') as number_f: for key, value in barrage_cache.get_barrage_cache().items(): number_f.writelines("{0}~{1}\n".format(key, value)) barrage_cache.reset_barrage_cache() print(F"scheduler_close_task:{time.asctime(time.localtime(time.time()))}") if b'False' in check_service_status(): print("Service have stopped") return True elif b'True' in check_service_status(): i = 3 while i > 1: if b'success' in stop_service(): time.sleep(2) if b'False' in check_service_status(): print("Service Stop Success") return True i -= 1 print("Service Stop Fail") send_email( F"scheduler_close_task:{time.asctime(time.localtime(time.time()))} fail", "close fail") return False
def add_barrage_to_database(room_id, yesterday): file_name = "{2}/{0}_{1}_message.txt".format(room_id, yesterday, BASE_DIR) if not os.path.exists(file_name): message = F"Don't exist:{file_name}" print(message) get_logger().error(message) send_email( F"add_barrage_to_database:{time.asctime(time.localtime(time.time()))} fail, Don't exist:{file_name}", "write data to database fail") return False print("Start write barrage {0} data to database".format(file_name)) with open(file_name, "r+", encoding="UTF-8") as barrage_file: for info in barrage_file.readlines(): info = eval(info) try: bar = Barrage() bar.douyu_id = info["USERID"] bar.douyu_name = info["USERNAME"] bar.barrage_content = info["BARRAGEINFO"] bar.user_level = info["USERLEVEL"] bar.user_image = info["USERIMAGE"] bar.room_id = info["ROOMID"] bar.barrage_time = info["BARRAGETIME"] bar.barrage_date = info["BARRAGEDATE"] bar.room_status = info["ROOMSTATUS"] bar.save() except Exception as e: get_logger().error("Save Barrage Fail") get_logger().error(e) return False print("End write barrage {0} data to database".format(file_name)) return True
def add_signin_to_database(room_id, yesterday): file_name = "{2}/{0}_{1}_sign.txt".format(room_id, yesterday, BASE_DIR) if not os.path.exists(file_name): message = F"Don't exist:{file_name}" print(message) get_logger().error(message) send_email( F"add_signin_to_database:{time.asctime(time.localtime(time.time()))} fail, Don't exist:{file_name}", "write data to database fail") return False print("Start write signin {0} data to database".format(file_name)) with open(file_name, "r+", encoding="UTF-8") as signin_file: for line in signin_file.readlines(): data = line.split("~") douyu_id, room_id, sign_in_time, sign_in_date, douyu_name = eval( data[1].replace("\\", "").replace("\n", "")) try: signin = SignIn() signin.douyu_id = douyu_id signin.room_id = room_id signin.sign_in_time = sign_in_time signin.sign_in_date = sign_in_date signin.douyu_name = douyu_name signin.sign_in_score = 1 signin.is_continuous = False signin.continuous_day = 1 continuous_reward_score = 1 before_signin = SignIn.objects.filter( douyu_id=douyu_id).order_by('-sign_in_time').first() """ 1. Daily sign-in points +1 2. After signing in for seven consecutive days, continue to sign in for an extra +1 3. For every "continuous check-in for 30 days", you can get a "treasure box of points", with additional points of +5 ~ +10 """ if before_signin is None: pass elif (sign_in_date - datetime.strptime(before_signin.sign_in_date, "%Y-%m-%d %H:%M:%S")).days > 1: signin.sign_in_score = before_signin.sign_in_score + 1 signin.is_continuous = False signin.continuous_day = 1 elif (sign_in_date - datetime.strptime(before_signin.sign_in_date, "%Y-%m-%d %H:%M:%S")).days == 1: signin.is_continuous = True signin.continuous_day = before_signin.continuous_day + 1 if before_signin.continuous_day % 30 == 0: signin.sign_in_score = before_signin.sign_in_score + 1 + random.randint( 5, 10) elif before_signin.continuous_day > 7: signin.sign_in_score = before_signin.sign_in_score + 1 + continuous_reward_score else: signin.sign_in_score = before_signin.sign_in_score + 1 signin.save() except Exception as e: get_logger().error("Save SignIn Fail") get_logger().error(e) return False print("End write signin {0} data to database".format(file_name)) return True
def get_barrage_rank(request): get_logger().info(F"\nrun fuc:get_barrage_rank\t{request.GET}\n") douyu_name = request.GET.get("name", None) get_type = request.GET.get("type", 5) # 0:day, 1:week, 2:month, 3:year, 4:total get_time = request.GET.get("time", None) rank_length = request.GET.get("rank_length", None) get_time = datetime.strptime(get_time, "%Y-%m-%d") if get_time is None: get_time = datetime.now() start_time = get_time get_type = int(get_type) if get_type == 0: start_time = datetime(start_time.year, start_time.month, start_time.day, OPEN_TIME) end_time = (start_time + timedelta(days=1)) end_time = datetime(end_time.year, end_time.month, end_time.day, 6) elif get_type == 1: start_time = datetime.strptime(week_get(start_time.date())[0], "%Y-%m-%d") start_time = datetime(start_time.year, start_time.month, start_time.day, OPEN_TIME) end_time = datetime.strptime(week_get(start_time.date())[7], "%Y-%m-%d") end_time = datetime(end_time.year, end_time.month, end_time.day, 6) elif get_type == 2: start_time = datetime(start_time.year, start_time.month, 1, OPEN_TIME) end_time = start_time + relativedelta(months=+1) end_time = datetime(end_time.year, end_time.month, end_time.day, 6) elif get_type == 3: start_time = datetime(start_time.year, 1, 1) end_time = datetime(start_time.year + 1, 1, 1) start_time = datetime(start_time.year, start_time.month, start_time.day, OPEN_TIME) end_time = datetime(end_time.year, end_time.month, end_time.day, 6) elif get_type == 4: bar = Barrage.objects.filter(room_status=1).values_list('douyu_id', 'douyu_name') temp = list(bar) if rank_length is None: result = Counter(temp).most_common(rank_length) else: result = Counter(temp).most_common(int(rank_length)) return HttpResponse(json.dumps({"data": result}), content_type="application/json") else: return HttpResponse("Please input correctly type in 0~4") # bar = Barrage.objects.filter(barrage_time__gt=start_time).filter(barrage_time__lt=end_time).filter( # room_status=1).values_list('douyu_id', 'douyu_name') room_id = settings.APP_ROOM_ID te = Barrage.objects.raw(F""" SELECT `webServer_barrage`.`barrage_id`,`webServer_barrage`.`douyu_id`, `webServer_barrage`.`douyu_name` FROM `webServer_barrage` WHERE (`webServer_barrage`.`barrage_time` BETWEEN "{start_time}" AND "{end_time}" AND `webServer_barrage`.`room_status` = 1) AND `webServer_barrage`.`room_id` = {room_id} """) bar = [] for i in te: bar.append((i.douyu_id, i.douyu_name)) temp = list(bar) total_result = Counter(temp).most_common(None) data = {} if douyu_name is not None: i = 0 for key, value in total_result: i += 1 if douyu_name == key[1]: data[douyu_name] = (i, value) break if douyu_name not in data.keys(): data[douyu_name] = (i, 0) if rank_length is not None: result = Counter(temp).most_common(int(rank_length)) return HttpResponse(json.dumps({"data": result, "people": data}), content_type="application/json") return HttpResponse(json.dumps({"data": total_result, "people": data}), content_type="application/json")
def run(self): client = MyClient() self._is_stop = False try: client.connect() roomid = settings.APP_ROOM_ID msg = 'type@=loginreq/roomid@={}/\0'.format(roomid) client.sendmsg(msg) msg_more = 'type@=joingroup/rid@={}/gid@=-9999/\0'.format(roomid) client.sendmsg(msg_more) now = datetime.now() print('---------------welcome to live room of {}---------------'. format(client.get_name(roomid))) while True: if self._is_stop: get_logger().debug("Stop the barrage service") print( '---------------away from live room of {}---------------' .format(client.get_name(roomid))) break client.keeplive() data = client.client.recv(1024) danmu_more = danmu.findall(data) if not data: break else: with open( '{2}/{1}_{0}_message.txt'.format( now.strftime("%Y-%m-%d"), roomid, BASE_DIR), 'a+') as f: try: for i in danmu_more: nows = datetime.now() dmDict = { 'USERID': i[0].decode(encoding='utf-8', errors='ignore'), 'USERNAME': i[1].decode(encoding='utf-8', errors='ignore'), 'BARRAGEINFO': i[2].decode(encoding='utf-8', errors='ignore'), 'BARRAGEID': i[3].decode(encoding='utf-8', errors='ignore'), 'USERIMAGE': i[4].decode(encoding='utf-8', errors='ignore'), 'USERLEVEL': i[5].decode(encoding='utf-8', errors='ignore'), 'ROOMID': settings.APP_ROOM_ID, 'BARRAGETIME': nows.strftime("%Y-%m-%d %H:%M:%S"), 'BARRAGEDATE': nows.strftime("%Y-%m-%d"), 'ROOMSTATUS': get_room_status(), } dmJsonStr = json.dumps( dmDict, ensure_ascii=False) + '\n' get_logger().debug(dmJsonStr) f.write(dmJsonStr) # save barrage cache barrage_cache.update_cache( dmDict['USERNAME'], dmDict['USERID']) # save sign in cache if "#签到" == dmDict['BARRAGEINFO']: signin = SignIn() signin.douyu_id = dmDict['USERID'] signin.douyu_name = dmDict['USERNAME'] signin.room_id = settings.APP_ROOM_ID signin.sign_in_time = nows.strftime( "%Y-%m-%d %H:%M:%S") signin.sign_in_date = nows.strftime( "%Y-%m-%d") signin_cache.update_cache( dmDict['USERID'], signin.return_model()) except Exception as e: get_logger().warn( "An error occurred while write barrage info to file and database" ) get_logger().warn(e) continue client.cancel_connect() except Exception as e: error_msg = "catch barrages fail:{}".format(e) get_logger().error(error_msg) send_email(error_msg, "catch barrages fail") scheduler_close_task()