Initial commit with translated description
This commit is contained in:
84
scripts/add_stock.py
Normal file
84
scripts/add_stock.py
Normal file
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Add stock to watchlist
|
||||
Usage: python3 add_stock.py <stock_code> [stock_name]
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
from config import WATCHLIST_FILE
|
||||
|
||||
def get_stock_name_from_code(stock_code):
|
||||
"""Get stock name from 10jqka.com.cn using stock code"""
|
||||
try:
|
||||
url = f"https://stockpage.10jqka.com.cn/{stock_code}/"
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
||||
}
|
||||
response = requests.get(url, headers=headers, timeout=10)
|
||||
response.encoding = 'utf-8'
|
||||
|
||||
if response.status_code == 200:
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
title = soup.find('title')
|
||||
if title:
|
||||
title_text = title.get_text()
|
||||
if '(' in title_text and ')' in title_text:
|
||||
stock_name = title_text.split('(')[0].strip()
|
||||
return stock_name
|
||||
except Exception as e:
|
||||
pass
|
||||
return None
|
||||
|
||||
def add_stock(stock_code, stock_name=None):
|
||||
"""Add stock to watchlist.txt in the correct location"""
|
||||
# Ensure the directory exists
|
||||
os.makedirs(os.path.dirname(WATCHLIST_FILE), exist_ok=True)
|
||||
|
||||
# Get stock name if not provided
|
||||
if not stock_name:
|
||||
stock_name = get_stock_name_from_code(stock_code)
|
||||
if not stock_name:
|
||||
stock_name = stock_code # fallback to code if name cannot be fetched
|
||||
|
||||
# Read existing watchlist
|
||||
existing_stocks = []
|
||||
if os.path.exists(WATCHLIST_FILE):
|
||||
with open(WATCHLIST_FILE, 'r', encoding='utf-8') as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line:
|
||||
existing_stocks.append(line)
|
||||
|
||||
# Check if stock already exists
|
||||
stock_entry = f"{stock_code}|{stock_name}"
|
||||
stock_exists = False
|
||||
for existing in existing_stocks:
|
||||
if existing.startswith(f"{stock_code}|"):
|
||||
stock_exists = True
|
||||
break
|
||||
|
||||
if stock_exists:
|
||||
print(f"Stock {stock_code} already in watchlist")
|
||||
return False
|
||||
|
||||
# Add new stock
|
||||
existing_stocks.append(stock_entry)
|
||||
|
||||
# Write back to file with proper newlines
|
||||
with open(WATCHLIST_FILE, 'w', encoding='utf-8') as f:
|
||||
for stock in existing_stocks:
|
||||
f.write(stock + '\n')
|
||||
|
||||
print(f"Added stock {stock_code} ({stock_name}) to watchlist")
|
||||
return True
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python3 add_stock.py <stock_code> [stock_name]")
|
||||
sys.exit(1)
|
||||
|
||||
stock_code = sys.argv[1]
|
||||
stock_name = sys.argv[2] if len(sys.argv) > 2 else None
|
||||
add_stock(stock_code, stock_name)
|
||||
24
scripts/clear_watchlist.py
Normal file
24
scripts/clear_watchlist.py
Normal file
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Clear the entire watchlist.
|
||||
This script removes all stocks from the watchlist file.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
|
||||
# Define the watchlist file path directly
|
||||
WATCHLIST_FILE = os.path.expanduser("~/.clawdbot/stock_watcher/watchlist.txt")
|
||||
|
||||
def clear_watchlist():
|
||||
"""Clear the entire watchlist."""
|
||||
# Ensure the directory exists
|
||||
os.makedirs(os.path.dirname(WATCHLIST_FILE), exist_ok=True)
|
||||
|
||||
# Clear the file by opening in write mode and closing immediately
|
||||
with open(WATCHLIST_FILE, 'w', encoding='utf-8') as f:
|
||||
pass
|
||||
|
||||
print("Watchlist cleared successfully.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
clear_watchlist()
|
||||
13
scripts/config.py
Normal file
13
scripts/config.py
Normal file
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Configuration for stock-watcher skill.
|
||||
Centralized configuration to avoid path confusion.
|
||||
"""
|
||||
import os
|
||||
|
||||
# Standardized watchlist file path
|
||||
WATCHLIST_DIR = os.path.expanduser("~/.clawdbot/stock_watcher")
|
||||
WATCHLIST_FILE = os.path.join(WATCHLIST_DIR, "watchlist.txt")
|
||||
|
||||
# Ensure directory exists
|
||||
os.makedirs(WATCHLIST_DIR, exist_ok=True)
|
||||
26
scripts/install.sh
Normal file
26
scripts/install.sh
Normal file
@@ -0,0 +1,26 @@
|
||||
#!/bin/bash
|
||||
# Install stock-watcher skill dependencies and initialize watchlist directory
|
||||
|
||||
set -e
|
||||
|
||||
echo "Installing stock-watcher skill dependencies..."
|
||||
|
||||
# Create the watchlist directory
|
||||
mkdir -p ~/.clawdbot/stock_watcher
|
||||
|
||||
# Create empty watchlist file if it doesn't exist
|
||||
WATCHLIST_FILE="$HOME/.clawdbot/stock_watcher/watchlist.txt"
|
||||
if [ ! -f "$WATCHLIST_FILE" ]; then
|
||||
touch "$WATCHLIST_FILE"
|
||||
echo "Created empty watchlist file: $WATCHLIST_FILE"
|
||||
fi
|
||||
|
||||
# Check if required Python packages are available
|
||||
if ! python3 -c "import requests, bs4" 2>/dev/null; then
|
||||
echo "Warning: Required Python packages (requests, beautifulsoup4) not found."
|
||||
echo "You may need to install them with: pip install requests beautifulsoup4"
|
||||
fi
|
||||
|
||||
echo "Stock-watcher skill installed successfully!"
|
||||
echo "Watchlist directory: ~/.clawdbot/stock_watcher/"
|
||||
echo "Watchlist file: ~/.clawdbot/stock_watcher/watchlist.txt"
|
||||
36
scripts/list_stocks.py
Normal file
36
scripts/list_stocks.py
Normal file
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
List all stocks in the user's watchlist.
|
||||
This script reads from the standard watchlist file and displays the current watchlist.
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Standardized watchlist file path
|
||||
WATCHLIST_FILE = os.path.expanduser("~/.clawdbot/stock_watcher/watchlist.txt")
|
||||
|
||||
def list_stocks():
|
||||
"""List all stocks in the watchlist."""
|
||||
if not os.path.exists(WATCHLIST_FILE):
|
||||
print("Watchlist is empty.")
|
||||
return
|
||||
|
||||
with open(WATCHLIST_FILE, 'r', encoding='utf-8') as f:
|
||||
lines = [line.strip() for line in f if line.strip()]
|
||||
|
||||
if not lines:
|
||||
print("Watchlist is empty.")
|
||||
return
|
||||
|
||||
print("Your Stock Watchlist:")
|
||||
print("-" * 40)
|
||||
for i, line in enumerate(lines, 1):
|
||||
parts = line.split('|')
|
||||
if len(parts) == 2:
|
||||
code, name = parts
|
||||
print(f"{i}. {code} - {name}")
|
||||
else:
|
||||
print(f"{i}. {line}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
list_stocks()
|
||||
54
scripts/remove_stock.py
Normal file
54
scripts/remove_stock.py
Normal file
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Remove stock from watchlist
|
||||
Usage: python3 remove_stock.py <stock_code>
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Standardized watchlist file path
|
||||
WATCHLIST_DIR = os.path.expanduser("~/.clawdbot/stock_watcher")
|
||||
WATCHLIST_FILE = os.path.join(WATCHLIST_DIR, "watchlist.txt")
|
||||
|
||||
def remove_stock(stock_code):
|
||||
"""Remove stock from watchlist."""
|
||||
if not os.path.exists(WATCHLIST_FILE):
|
||||
print("Watchlist is empty.")
|
||||
return False
|
||||
|
||||
# Read existing watchlist
|
||||
existing_stocks = []
|
||||
with open(WATCHLIST_FILE, 'r', encoding='utf-8') as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line:
|
||||
existing_stocks.append(line)
|
||||
|
||||
# Find and remove the stock
|
||||
stock_found = False
|
||||
updated_stocks = []
|
||||
for stock in existing_stocks:
|
||||
if stock.startswith(f"{stock_code}|"):
|
||||
stock_found = True
|
||||
else:
|
||||
updated_stocks.append(stock)
|
||||
|
||||
if not stock_found:
|
||||
print(f"Stock {stock_code} not found in watchlist")
|
||||
return False
|
||||
|
||||
# Write back to file
|
||||
with open(WATCHLIST_FILE, 'w', encoding='utf-8') as f:
|
||||
for stock in updated_stocks:
|
||||
f.write(stock + '\n')
|
||||
|
||||
print(f"Removed stock {stock_code} from watchlist")
|
||||
return True
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python3 remove_stock.py <stock_code>")
|
||||
sys.exit(1)
|
||||
|
||||
stock_code = sys.argv[1]
|
||||
remove_stock(stock_code)
|
||||
100
scripts/summarize_performance.py
Normal file
100
scripts/summarize_performance.py
Normal file
@@ -0,0 +1,100 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Summarize the performance of all stocks in the watchlist.
|
||||
This script fetches current stock data from 10jqka.com.cn for each stock
|
||||
in the watchlist and provides a summary of their recent performance.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
import time
|
||||
|
||||
# Standardized watchlist file path
|
||||
WATCHLIST_FILE = os.path.expanduser("~/.clawdbot/stock_watcher/watchlist.txt")
|
||||
|
||||
def fetch_stock_data(stock_code):
|
||||
"""Fetch stock data from 10jqka.com.cn."""
|
||||
url = f"https://stockpage.10jqka.com.cn/{stock_code}/"
|
||||
|
||||
try:
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
||||
}
|
||||
response = requests.get(url, headers=headers, timeout=10)
|
||||
response.encoding = 'utf-8'
|
||||
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
|
||||
# Extract basic info
|
||||
title = soup.find('title')
|
||||
stock_name = ""
|
||||
if title:
|
||||
title_text = title.get_text()
|
||||
if '(' in title_text and ')' in title_text:
|
||||
stock_name = title_text.split('(')[0].strip()
|
||||
|
||||
# Look for performance data
|
||||
performance_data = {}
|
||||
|
||||
# Try to find recent performance indicators
|
||||
# This is a simplified version - in practice, you'd need more robust parsing
|
||||
text_content = soup.get_text()
|
||||
|
||||
# Look for key phrases
|
||||
if '涨跌幅' in text_content:
|
||||
# Extract percentage changes
|
||||
import re
|
||||
percentages = re.findall(r'[-+]?\d+\.?\d*%', text_content)
|
||||
if percentages:
|
||||
performance_data['recent_changes'] = percentages[:3] # Get first 3 percentages
|
||||
|
||||
return {
|
||||
'code': stock_code,
|
||||
'name': stock_name,
|
||||
'url': url,
|
||||
'performance': performance_data
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error fetching data for {stock_code}: {e}", file=sys.stderr)
|
||||
return None
|
||||
|
||||
def summarize_performance():
|
||||
"""Summarize performance of all stocks in watchlist."""
|
||||
if not os.path.exists(WATCHLIST_FILE):
|
||||
return
|
||||
|
||||
with open(WATCHLIST_FILE, 'r', encoding='utf-8') as f:
|
||||
lines = f.readlines()
|
||||
|
||||
if not lines or all(not line.strip() for line in lines):
|
||||
return
|
||||
|
||||
# Directly output performance summary without any command prompts
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
if line:
|
||||
parts = line.split('|')
|
||||
if len(parts) == 2:
|
||||
code, name = parts
|
||||
|
||||
# Fetch data
|
||||
stock_data = fetch_stock_data(code)
|
||||
if stock_data:
|
||||
if stock_data['performance']:
|
||||
for i, change in enumerate(stock_data['performance'].get('recent_changes', []), 1):
|
||||
print(f"{code} - {name} - 指标{i}: {change}")
|
||||
else:
|
||||
print(f"{code} - {name} - 行情数据暂不可用")
|
||||
else:
|
||||
print(f"{code} - {name} - 获取数据失败")
|
||||
|
||||
# Be respectful to the server
|
||||
time.sleep(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
summarize_performance()
|
||||
13
scripts/uninstall.sh
Normal file
13
scripts/uninstall.sh
Normal file
@@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
# Uninstall stock-watcher skill
|
||||
|
||||
set -e
|
||||
|
||||
# Remove watchlist directory and files
|
||||
WATCHLIST_DIR="$HOME/.clawdbot/stock_watcher"
|
||||
if [ -d "$WATCHLIST_DIR" ]; then
|
||||
rm -rf "$WATCHLIST_DIR"
|
||||
echo "Removed watchlist directory: $WATCHLIST_DIR"
|
||||
fi
|
||||
|
||||
echo "Stock watcher skill uninstalled successfully."
|
||||
Reference in New Issue
Block a user