Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie取得機能を実装 #2

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added aria2c.exe
Binary file not shown.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies = [
"customtkinter>=5.2.2",
"ctkmessagebox>=2.7",
"pycryptodome>=3.21.0",
"selenium>=4.27.1",
]
readme = "README.md"
requires-python = ">= 3.8"
Expand Down
33 changes: 33 additions & 0 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@
-e file:.
altgraph==0.17.4
# via pyinstaller
attrs==24.3.0
# via outcome
# via trio
beautifulsoup4==4.12.3
# via nicodlp
certifi==2024.12.14
# via requests
# via selenium
cffi==1.17.1
# via trio
charset-normalizer==3.4.1
# via requests
ctkmessagebox==2.7
Expand All @@ -25,8 +31,13 @@ customtkinter==5.2.2
# via nicodlp
darkdetect==0.8.0
# via customtkinter
h11==0.14.0
# via wsproto
idna==3.10
# via requests
# via trio
outcome==1.3.0.post0
# via trio
packaging==24.2
# via customtkinter
# via pyinstaller
Expand All @@ -35,21 +46,43 @@ pefile==2023.2.7
# via pyinstaller
pillow==11.1.0
# via ctkmessagebox
pycparser==2.22
# via cffi
pycryptodome==3.21.0
# via nicodlp
pyinstaller==6.11.1
pyinstaller-hooks-contrib==2025.0
# via pyinstaller
pysocks==1.7.1
# via urllib3
pywin32-ctypes==0.2.3
# via pyinstaller
requests==2.32.3
# via nicodlp
selenium==4.27.1
# via nicodlp
setuptools==75.8.0
# via pyinstaller
# via pyinstaller-hooks-contrib
sniffio==1.3.1
# via trio
sortedcontainers==2.4.0
# via trio
soupsieve==2.6
# via beautifulsoup4
trio==0.28.0
# via selenium
# via trio-websocket
trio-websocket==0.11.1
# via selenium
typing-extensions==4.12.2
# via selenium
urllib3==2.3.0
# via requests
# via selenium
websocket-client==1.8.0
# via selenium
wsproto==1.2.0
# via trio-websocket
yt-dlp==2025.1.15
# via nicodlp
33 changes: 33 additions & 0 deletions requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
# universal: false

-e file:.
attrs==24.3.0
# via outcome
# via trio
beautifulsoup4==4.12.3
# via nicodlp
certifi==2024.12.14
# via requests
# via selenium
cffi==1.17.1
# via trio
charset-normalizer==3.4.1
# via requests
ctkmessagebox==2.7
Expand All @@ -23,19 +29,46 @@ customtkinter==5.2.2
# via nicodlp
darkdetect==0.8.0
# via customtkinter
h11==0.14.0
# via wsproto
idna==3.10
# via requests
# via trio
outcome==1.3.0.post0
# via trio
packaging==24.2
# via customtkinter
pillow==11.1.0
# via ctkmessagebox
pycparser==2.22
# via cffi
pycryptodome==3.21.0
# via nicodlp
pysocks==1.7.1
# via urllib3
requests==2.32.3
# via nicodlp
selenium==4.27.1
# via nicodlp
sniffio==1.3.1
# via trio
sortedcontainers==2.4.0
# via trio
soupsieve==2.6
# via beautifulsoup4
trio==0.28.0
# via selenium
# via trio-websocket
trio-websocket==0.11.1
# via selenium
typing-extensions==4.12.2
# via selenium
urllib3==2.3.0
# via requests
# via selenium
websocket-client==1.8.0
# via selenium
wsproto==1.2.0
# via trio-websocket
yt-dlp==2025.1.15
# via nicodlp
88 changes: 88 additions & 0 deletions src/nicodlp/cookie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import os


def save_cookies_to_txt(driver, filename="cookier.txt"):
"""
SeleniumのWebDriverからCookieを取得し、Netscape HTTP Cookie File形式で保存します。

Args:
driver: Selenium WebDriverインスタンス
filename: 保存するファイル名 (デフォルトは"cookier.txt")
"""
with open(filename, "w") as f:
f.write("# Netscape HTTP Cookie File\n")
for cookie in driver.get_cookies():
domain = cookie["domain"]
flag = "TRUE" if domain.startswith(".") else "FALSE"
path = cookie["path"]
secure = "TRUE" if cookie["secure"] else "FALSE"
expiry = int(time.time()) + 3600 * 24 * 7
name = cookie["name"]
value = cookie["value"]

f.write(f"{domain}\t{flag}\t{path}\t{secure}\t{expiry}\t{name}\t{value}\n")


def login_nicovideo(driver, email, password):
"""
ニコニコ動画にログインします。

Args:
driver: Selenium WebDriverインスタンス
email: ニコニコ動画のメールアドレス
password: ニコニコ動画のパスワード
"""

driver.get("https://account.nicovideo.jp/login?site=niconicoq")

email_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "input__mailtel"))
)
email_field.send_keys(email)

password_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "input__password"))
)
password_field.send_keys(password)

login_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "login__submit"))
)

login_button.click()

WebDriverWait(driver, 60 * 10).until(EC.title_contains("ニコニコQ"))


def main():
"""
メイン関数
"""
driver = webdriver.Chrome()

email = os.environ.get("NICONICO_EMAIL")
password = os.environ.get("NICONICO_PASSWORD")

if not email or not password:
email = input("ニコニコ動画のメールアドレスを入力してください: ")
password = input("ニコニコ動画のパスワードを入力してください: ")

try:
login_nicovideo(driver, email, password)
save_cookies_to_txt(driver)
print("Cookieをcookier.txtに保存しました。")

except Exception as e:
print(f"エラーが発生しました: {e}")

finally:
driver.quit()


if __name__ == "__main__":
main()
48 changes: 48 additions & 0 deletions src/nicodlp/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import webbrowser

from nicodlp.cookie import login_nicovideo, save_cookies_to_txt
from selenium import webdriver

ctk.set_appearance_mode("System") # Modes: "System" (standard), "Dark", "Light"
ctk.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"

Expand Down Expand Up @@ -231,6 +234,35 @@ def create_widgets(self):
)
self.download_button.pack(pady=20)

# email/password
self.email_password_frame = ctk.CTkFrame(self)
self.email_password_frame.pack(pady=10, padx=20, fill="x")

self.email_label = ctk.CTkLabel(self.email_password_frame, text="mail/tel")
self.email_label.grid(row=0, column=0, sticky="w")
self.email_textbox = ctk.CTkEntry(
self.email_password_frame,
width=250,
textvariable=ctk.StringVar(
value="[email protected]",
),
)
self.email_textbox.grid(row=0, column=1, padx=5)

self.password_label = ctk.CTkLabel(self.email_password_frame, text="password")
self.password_label.grid(row=0, column=2, sticky="w")
self.password_textbox = ctk.CTkEntry(
self.email_password_frame,
width=250,
textvariable=ctk.StringVar(value="password"),
)
self.password_textbox.grid(row=0, column=3, padx=5)

self.cookie_runner_button = ctk.CTkButton(
self, text="Login to niconico", command=self.get_cookie
)
self.cookie_runner_button.pack(pady=20)

def save_config_click(self):
self.config["-o"] = self.output_entry.get()
self.config["-f"] = self.quality_entry.get()
Expand All @@ -248,6 +280,21 @@ def run_download(self):
p = Process(target=download_video, args=(url, self.config_file))
p.start()

def get_cookie(self):
email = self.email_textbox.get()
password = self.password_textbox.get()
driver = webdriver.Chrome()
try:
login_nicovideo(driver, email, password)
save_cookies_to_txt(driver)
print("Cookieをcookier.txtに保存しました。")

except Exception as e:
print(f"エラーが発生しました: {e}")

finally:
driver.quit()


def download_video(url, config_file):
ydl_opts = {
Expand All @@ -267,6 +314,7 @@ def download_video(url, config_file):
"ignoreerrors": True,
"continue": True,
"nooverwrites": True,
"cookiefile": "cookier.txt",
}
print(f"Downloading {url}")
print(f"Config file: {config_file}")
Expand Down
Loading