def __add_twitter_rules(rule_raw_value): response = requests.post(url=TWITTER_RULES_URL, headers=HEADERS, json={"add": [{ "value": rule_raw_value }]}) return response
def delete_rule_by_id(ids, delete_all=False, delete_related_usernames=True): if not ids: ids = list(map(lambda x: x.decode(), redis_cli.hkeys('rules'))) payload = {"delete": {"ids": ids}} response = requests.post(TWITTER_RULES_URL, headers=HEADERS, json=payload) if response.status_code == 200: if delete_all: redis_cli.delete('rules') redis_cli.delete('username_rid') else: for rid in ids: rule = redis_cli.hget('rules', rid) if delete_related_usernames: parsed_rules = list( map(lambda x: x.replace('from:', ''), re.findall(r'from:\w+\b', rule.decode()))) for rule in parsed_rules: redis_cli.hdel('username_rid', rule) redis_cli.hdel("rules", rid) logger.info("delete rules from twitter successfully") return 200 [redis_cli.hset('garbage', rid, 1) for rid in ids] logger.error("delete rules from twitter failed: {} \n\t {}".format( response.status_code, response.json())) return response.status_code
def __tg_send_video(**kwargs): _type = kwargs.get("type", "video") url = TELEGRAM_BOT_URL.format(PATH.get(_type)) video_info = kwargs.get("video_info") response = requests.post( url=url, data={ "chat_id": TARGET_TELEGRAM_ENDPOINT, "caption": kwargs.get("caption"), "parse_mode": "HTML", _type: get_video_url(video_info.get("variants")) if video_info else kwargs.get("preview_image_url"), "duration": video_info.get("duration_millis") if video_info else kwargs.get("duration_ms"), "thumb": kwargs.get("media_url") if not video_info else kwargs.get("preview_image_url"), "with": kwargs.get("width") if not video_info else None, "height": kwargs.get("height") if not video_info else None, }) return response
def __tg_send_photo(**kwargs): url = TELEGRAM_BOT_URL.format("sendPhoto") response = requests.post(url=url, data={ "chat_id": TARGET_TELEGRAM_ENDPOINT, "photo": kwargs.get("url"), "caption": kwargs.get("caption"), "parse_mode": "HTML", }) return response
def __tg_send_message(**kwargs): url = TELEGRAM_BOT_URL.format("sendMessage") response = requests.post(url=url, data={ "chat_id": TARGET_TELEGRAM_ENDPOINT, "text": kwargs.get("text"), "parse_mode": "html", "disable_web_page_preview": kwargs.get("disable_web_page_preview", True), }) return response
def __tg_send_group_media(media_list): url = TELEGRAM_BOT_URL.format("sendMediaGroup") input_media = [] for media in media_list: if media.get("type") == "photo": _m = InputMediaPhoto.get_instance(**media) else: _m = InputMediaVideo.get_instance(**media) input_media.append(_m) response = requests.post(url=url, data={ "chat_id": TARGET_TELEGRAM_ENDPOINT, "media": json.dumps(input_media), }) return response