Files
reddit_stock_analyzer/main.py
2025-07-21 11:43:27 +02:00

66 lines
2.0 KiB
Python

# main.py
import argparse
import json
import yfinance as yf
def load_subreddits(filepath):
"""Loads a list of subreddits from a JSON file."""
try:
with open(filepath, 'r') as f:
data = json.load(f)
return data.get("subreddits", [])
except FileNotFoundError:
print(f"Error: The file '{filepath}' was not found.")
return None
except json.JSONDecodeError:
print(f"Error: Could not decode JSON from '{filepath}'.")
return None
def get_market_cap(ticker_symbol):
"""Fetches the market capitalization for a given stock ticker."""
try:
ticker = yf.Ticker(ticker_symbol)
market_cap = ticker.info.get('marketCap')
if market_cap:
# Formatting for better readability
return f"${market_cap:,}"
return "N/A"
except Exception as e:
# yfinance can sometimes fail for various reasons (e.g., invalid ticker)
return "N/A"
def main():
"""Main function to run the Reddit stock analysis tool."""
parser = argparse.ArgumentParser(description="Analyze stock ticker mentions on Reddit.")
parser.add_argument(
"config_file",
help="Path to the JSON file containing the list of subreddits."
)
args = parser.parse_args()
# --- Part 1: Load Configuration ---
print("Loading configuration...")
subreddits = load_subreddits(args.config_file)
if not subreddits:
print("No subreddits found in the configuration file. Exiting.")
return
print(f"Successfully loaded {len(subreddits)} subreddits: {', '.join(subreddits)}")
print("-" * 30)
# --- Part 2: Test Market Data Fetching (Example) ---
print("Testing market data functionality...")
example_ticker = "AAPL"
market_cap = get_market_cap(example_ticker)
print(f"Market Cap for {example_ticker}: {market_cap}")
print("-" * 30)
# In the next steps, we will add the Reddit scanning logic here.
print("Next up: Integrating the Reddit API to find tickers...")
if __name__ == "__main__":
main()