Guest User

Untitled

a guest
Feb 8th, 2025
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | Source Code | 0 0
  1. import requests
  2. import json
  3. import threading
  4. import time
  5.  
  6. user_session = ""
  7.    
  8. threadid = 1354993084
  9.  
  10. url = "https://nvapi.nicovideo.jp/v1/comment/keys/post?threadId={}".format(threadid)
  11.  
  12. headers = {
  13.     'Accept': '*/*',
  14.     'Accept-Language': 'ja,en-US;q=0.9,en;q=0.8',
  15.     'Connection': 'keep-alive',
  16.     'Origin': 'https://www.nicovideo.jp',
  17.     'Referer': 'https://www.nicovideo.jp/',
  18.     'Sec-Fetch-Dest': 'empty',
  19.     'Sec-Fetch-Mode': 'cors',
  20.     'Sec-Fetch-Site': 'same-site',
  21.     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.199 Safari/537.36',
  22.     'X-Frontend-Id': '6',
  23.     'X-Frontend-Version': '0',
  24.     'X-Niconico-Language': 'ja-jp',
  25. }
  26.  
  27. cookies = {
  28.     'user_session': user_session,
  29. }
  30.  
  31. postkey = None
  32. postkey_lock = threading.Lock()
  33.  
  34. def get_postkey():
  35.     global postkey
  36.     try:
  37.         while True:
  38.             res = requests.get(url, headers=headers, cookies=cookies).json()
  39.             postkey = res["data"]["postKey"]
  40.             if postkey is not None:
  41.                 print("新しいPostKeyの取得に成功しました:", postkey)
  42.             else:
  43.                 print("postkeyの取得に失敗したので再試行します。")
  44.                 return
  45.             time.sleep(60)
  46.     except KeyError:
  47.         return
  48.     except requests.exceptions.RequestException:
  49.         return
  50.     except Exception:
  51.         return
  52.  
  53. def postcomment(thread_num):
  54.     global postkey
  55.     while True:
  56.         with postkey_lock:
  57.             if postkey is None:
  58.                 print("PostKeyの取得を待っています…")
  59.                 time.sleep(5)
  60.                 continue
  61.         px = "http://"
  62.         proxies = {
  63.             "http": px,
  64.             "https": px
  65.         }
  66.  
  67.         headers = {
  68.             "X-Frontend-Id": "6",
  69.             "X-Frontend-Version": "0",
  70.             "Content-Type": "application/json"
  71.         }
  72.         params = {
  73.             "videoId": "sm19537579",
  74.             "commands": [
  75.                 "184 naka medium purple"
  76.             ],
  77.             "body": "かわいい",
  78.             "vposMs": 0,
  79.             "postKey": postkey
  80.         }
  81.  
  82.         url = "https://nvcomment.nicovideo.jp/v1/threads/{}/comments".format(threadid)
  83.  
  84.         try:
  85.             res2 = requests.post(url, json.dumps(params), headers=headers, proxies=proxies,timeout=5).json()
  86.             status = res2['meta']['status']
  87.             comment_number = res2['data']['no']
  88.            
  89.             if status == 201:
  90.                 print(f"スレッド{thread_num} - コメント投稿成功\nStatus_code: {status} コメ番: {comment_number}")
  91.             else:
  92.                 print(f"スレッド{thread_num} - コメント投稿失敗\nリクエストが多すぎるか、ニコニコ動画側のエラーです。\nStatus_code: {status}")
  93.  
  94.         except requests.exceptions.RequestException:
  95.             print(f"スレッド{thread_num} - Proxyエラーが発生しました。")
  96.  
  97.         except KeyError:
  98.             print(f"スレッド{thread_num} - KeyErrorが発生しました。")
  99.        
  100.         except Exception:
  101.             pass
  102.  
  103. postkey_thread = threading.Thread(target=get_postkey)
  104. postkey_thread.start()
  105.  
  106. threads = []
  107. for i in range(1, 33):
  108.     thread = threading.Thread(target=postcomment, args=(i,))
  109.     threads.append(thread)
  110.     thread.start()
  111.  
  112. postkey_thread.join()
  113.  
  114. for thread in threads:
  115.     thread.join()
Advertisement
Add Comment
Please, Sign In to add comment