Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import websocket
- import requests
- import json
- import ssl
- import threading
- import time
- VOTE_COUNT = 0
- class TokenManager:
- @staticmethod
- def extract_w3t(headers):
- set_cookie = headers.get('set-cookie', '')
- cookies = set_cookie.split(', ')
- for cookie in cookies:
- if cookie.startswith('w3t='):
- return cookie.split(';')[0].split('=', 1)[1]
- return None
- @staticmethod
- def extract_w3k(headers):
- set_cookie = headers.get('set-cookie', '')
- cookies = set_cookie.split(', ')
- for cookie in cookies:
- if cookie.startswith('w3k='):
- return cookie.split(';')[0].split('=', 1)[1]
- return None
- @staticmethod
- def get_token():
- headers = {
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0"
- }
- while True:
- req = requests.get("https://nsk.kp.ru/media/984186/", headers=headers)
- entity = TokenManager.extract_w3t(req.headers)
- cookie_w3k = TokenManager.extract_w3k(req.headers)
- if entity and cookie_w3k:
- headers.update({
- "Cookie": f"w3t={entity}; w3k={cookie_w3k}",
- "Referer": "https://www.nsk.kp.ru",
- })
- req = requests.get('https://identity.kp.house/identity/api/2/auth/token.json?callback=data&client_name=prod&sub=1', headers=headers)
- signature = req.json().get('data', '')
- auth_id = TokenManager.extract_w3k(req.headers)
- return entity, signature, auth_id, cookie_w3k
- else:
- print("Failed to retrieve valid w3t or w3k tokens, retrying in 3 seconds")
- time.sleep(3)
- class VoteBot:
- def __init__(self, entity, signature, auth_id, cookie_w3k):
- self.entity = entity
- self.signature = signature
- self.auth_id = auth_id
- self.cookie_w3k = cookie_w3k
- self.ws = None
- def on_message(self, ws, message):
- global VOTE_COUNT
- message_data = json.loads(message)
- if message_data[0] == 4:
- self.send_authentication()
- elif message_data[0] == 3:
- entity, signature, auth_id, cookie_w3k = TokenManager.get_token()
- self.entity = entity
- self.signature = signature
- self.auth_id = auth_id
- self.cookie_w3k = cookie_w3k
- self.send_hello()
- elif message_data[0] == 2:
- self.send_vote()
- elif message_data[0] == 50:
- VOTE_COUNT += 1
- print(f"Vote #{VOTE_COUNT} sent successfully")
- self.ws.close()
- def on_error(self, ws, error):
- print(f"Error: {error}")
- def on_close(self, ws, close_status_code, close_msg):
- print("Connection closed")
- def on_open(self, ws):
- self.send_hello()
- def send_hello(self):
- hello_message = [
- 1,
- "kpcdn",
- {
- "roles": {
- "caller": {"features": {"caller_identification": True, "call_canceling": True, "progressive_call_results": True}},
- "callee": {"features": {"caller_identification": True, "pattern_based_registration": True, "shared_registration": True, "progressive_call_results": True, "registration_revocation": True}},
- "publisher": {"features": {"publisher_identification": True, "subscriber_blackwhite_listing": True, "publisher_exclusion": True}},
- "subscriber": {"features": {"publisher_identification": True, "pattern_based_subscription": True, "subscription_revocation": True}}
- },
- "authmethods": ["ticket"],
- "authid": self.auth_id
- }
- ]
- self.ws.send(json.dumps(hello_message))
- def send_authentication(self):
- auth_message = [
- 5,
- self.signature,
- {}
- ]
- self.ws.send(json.dumps(auth_message))
- def send_vote(self):
- vote_message = [
- 48,
- 1,
- {"disclose_me": True},
- "net.kpcdn.online.send",
- [{
- "schema": "interactive",
- "method": "polls.raw",
- "target": {
- "spot": "interactive",
- "base": "polls.raw",
- "entity": self.entity
- },
- "@context": "message",
- "body": {
- "contest-id": "670f265afbee6d0007e94cfa",
- "vote-container-id": "67101efcfbee6d0007e94dd5",
- "competitor-id": "670febf0fbee6d0007e94d70",
- "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0",
- "referrer": "",
- "source": "kp.ru",
- "is-mobile": True
- }
- }],
- {}
- ]
- self.ws.send(json.dumps(vote_message))
- def run(self):
- self.ws = websocket.WebSocketApp("wss://www.kp.kg/ws",
- on_open=self.on_open,
- on_message=self.on_message,
- on_error=self.on_error,
- on_close=self.on_close,
- header=["Sec-WebSocket-Protocol: wamp.2.json", "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0", "Origin: https://www.nsk.kp.ru"],
- cookie=f"w3k={self.cookie_w3k}; w3t={self.entity}")
- self.ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
- def run_bot():
- entity, signature, auth_id, cookie_w3k = TokenManager.get_token()
- bot = VoteBot(entity, signature, auth_id, cookie_w3k)
- bot.run()
- def main(num_threads, max_concurrent_threads):
- semaphore = threading.Semaphore(max_concurrent_threads)
- threads = []
- def thread_target():
- with semaphore:
- run_bot()
- for _ in range(num_threads):
- thread = threading.Thread(target=thread_target)
- threads.append(thread)
- thread.start()
- for thread in threads:
- thread.join()
- if __name__ == "__main__":
- num_threads = 50000
- max_concurrent_threads = 50
- main(num_threads=num_threads, max_concurrent_threads=max_concurrent_threads)
Advertisement
Add Comment
Please, Sign In to add comment