Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- import threading
- import time
- user_session = ""
- threadid = 1354993084
- url = "https://nvapi.nicovideo.jp/v1/comment/keys/post?threadId={}".format(threadid)
- headers = {
- 'Accept': '*/*',
- 'Accept-Language': 'ja,en-US;q=0.9,en;q=0.8',
- 'Connection': 'keep-alive',
- 'Origin': 'https://www.nicovideo.jp',
- 'Referer': 'https://www.nicovideo.jp/',
- 'Sec-Fetch-Dest': 'empty',
- 'Sec-Fetch-Mode': 'cors',
- 'Sec-Fetch-Site': 'same-site',
- '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',
- 'X-Frontend-Id': '6',
- 'X-Frontend-Version': '0',
- 'X-Niconico-Language': 'ja-jp',
- }
- cookies = {
- 'user_session': user_session,
- }
- postkey = None
- postkey_lock = threading.Lock()
- def get_postkey():
- global postkey
- try:
- while True:
- res = requests.get(url, headers=headers, cookies=cookies).json()
- postkey = res["data"]["postKey"]
- if postkey is not None:
- print("新しいPostKeyの取得に成功しました:", postkey)
- else:
- print("postkeyの取得に失敗したので再試行します。")
- return
- time.sleep(60)
- except KeyError:
- return
- except requests.exceptions.RequestException:
- return
- except Exception:
- return
- def postcomment(thread_num):
- global postkey
- while True:
- with postkey_lock:
- if postkey is None:
- print("PostKeyの取得を待っています…")
- time.sleep(5)
- continue
- px = "http://"
- proxies = {
- "http": px,
- "https": px
- }
- headers = {
- "X-Frontend-Id": "6",
- "X-Frontend-Version": "0",
- "Content-Type": "application/json"
- }
- params = {
- "videoId": "sm19537579",
- "commands": [
- "184 naka medium purple"
- ],
- "body": "かわいい",
- "vposMs": 0,
- "postKey": postkey
- }
- url = "https://nvcomment.nicovideo.jp/v1/threads/{}/comments".format(threadid)
- try:
- res2 = requests.post(url, json.dumps(params), headers=headers, proxies=proxies,timeout=5).json()
- status = res2['meta']['status']
- comment_number = res2['data']['no']
- if status == 201:
- print(f"スレッド{thread_num} - コメント投稿成功\nStatus_code: {status} コメ番: {comment_number}")
- else:
- print(f"スレッド{thread_num} - コメント投稿失敗\nリクエストが多すぎるか、ニコニコ動画側のエラーです。\nStatus_code: {status}")
- except requests.exceptions.RequestException:
- print(f"スレッド{thread_num} - Proxyエラーが発生しました。")
- except KeyError:
- print(f"スレッド{thread_num} - KeyErrorが発生しました。")
- except Exception:
- pass
- postkey_thread = threading.Thread(target=get_postkey)
- postkey_thread.start()
- threads = []
- for i in range(1, 33):
- thread = threading.Thread(target=postcomment, args=(i,))
- threads.append(thread)
- thread.start()
- postkey_thread.join()
- for thread in threads:
- thread.join()
Advertisement
Add Comment
Please, Sign In to add comment