collect-data-pipeline/examples/example_sns_scraper.py
2025-09-17 07:03:06 +09:00

57 lines
1.6 KiB
Python

# pip install requests requests-oauthlib
import sys
import os
import asyncio
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))
from dotenv import load_dotenv
load_dotenv(".env")
from lib.custom_logger import get_logger
logger = get_logger(level=10)
from providers.sns.x_sns_scraper import XScraper
async def first_time_login():
bot = XScraper(storage_state="x_cookies.json", headless=False, slow_mo=50)
await bot.start()
await bot.login_manual()
input("ログイン完了後に Enter を押してください...")
ok = await bot.is_logged_in()
print("Logged in?", ok)
await bot.save_state()
await bot.stop()
# asyncio.run(first_time_login())
async def run_headless():
bot = XScraper(storage_state="x_cookies.json", headless=True)
await bot.start()
print("already logged in?", await bot.is_logged_in())
# ここに処理を書く(検索/会話取得など、次のステップで実装)
items = await bot.search_tweets("OpenAI lang:ja -is:retweet", 30)
logger.info(f"Found {len(items)} tweets")
for tweet in items :
logger.info(f"- {tweet['id']}: {tweet['text']}")
await bot.stop()
# asyncio.run(run_headless())
async def example_get_trand():
bot = XScraper(storage_state="x_cookies.json", headless=True)
await bot.start()
try:
trends = await bot.get_trends(limit=10)
for t in trends:
print(t["rank"], t["name"], t["tweet_count"], t["url"])
finally:
await bot.stop()
asyncio.run(example_get_trand())