Skip to content

Commit

Permalink
配置文件实现仓库托管
Browse files Browse the repository at this point in the history
  • Loading branch information
Samiya committed Mar 5, 2024
1 parent e538a92 commit 3ae6b36
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 119 deletions.
12 changes: 11 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ FROM python:3.10-slim AS runner
COPY --from=builder /etc/localtime /etc/localtime
COPY --from=builder /etc/timezone /etc/timezone

# 安装运行时依赖
RUN apt-get update && \
apt-get install -y --no-install-recommends jq curl && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# 设置工作目录
WORKDIR /root/VintageVigil

Expand All @@ -39,6 +45,9 @@ COPY --from=builder /root/VintageVigil/dependencies /usr/local/lib/python3.10/si
# 从构建阶段复制项目文件
COPY --from=builder /root/VintageVigil .

# 确保启动脚本具有执行权限
RUN chmod +x /root/VintageVigil/start.sh

# 设置容器启动时执行的命令(根据你的需要选择一个)
# 自动监控
# CMD ["bash", "./run_checker.sh"]
Expand All @@ -47,4 +56,5 @@ COPY --from=builder /root/VintageVigil .
# CMD ["sh", "-c", "python ./main.py > /dev/null 2>&1"]

# 在控制台输出日志
CMD ["python", "./main.py"]
# CMD ["python", "./main.py"]
CMD ["sh", "-c", "./start.sh"]
9 changes: 8 additions & 1 deletion Dockerfile-Alpine
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ FROM python:3.10-alpine AS runner
COPY --from=builder /etc/localtime /etc/localtime
COPY --from=builder /etc/timezone /etc/timezone

# 安装运行时依赖
RUN apk add --no-cache jq curl

# 设置工作目录
WORKDIR /root/VintageVigil

Expand All @@ -44,5 +47,9 @@ COPY --from=builder /root/VintageVigil/dependencies /usr/local/lib/python3.10/si
# 从构建阶段复制项目文件
COPY --from=builder /root/VintageVigil .

# 确保启动脚本具有执行权限
RUN chmod +x /root/VintageVigil/start.sh

# 设置容器启动时执行的命令
CMD ["python", "./main.py"]
# CMD ["python", "./main.py"]
CMD ["sh", "-c", "./start.sh"]
10 changes: 1 addition & 9 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ services:
stdin_open: true
tty: true
command: /data -A
networks:
- vintagevigil
# 爬虫
VintageVigil:
image: samiya777/vintagevigil:latest
Expand All @@ -26,10 +24,4 @@ services:
max-file: "3"
volumes:
- /root/VintageUser/.env:/root/VintageVigil/.env # 环境变量
- /root/VintageUser/lx:/root/VintageVigil/user/lx # 配置文件
networks:
- vintagevigil

networks:
vintagevigil:
driver: bridge
- /root/VintageUser/lx:/root/VintageVigil/user/lx # 配置文件
108 changes: 0 additions & 108 deletions mercari_buyer_bot.py

This file was deleted.

95 changes: 95 additions & 0 deletions mercari_get_buyer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import os
import re
import requests
from uuid import uuid4
import ecdsa
from datetime import datetime
from jose import jws
from jose.constants import ALGORITHMS
from telebot import TeleBot
from typing import Optional, Dict

# Use an environment variable for the API token
TG_TOKEN = ""
API_TOKEN = os.getenv("MERCARI_BOT_API_TOKEN") | TG_TOKEN
bot = TeleBot(API_TOKEN)

# Constants
MERCARI_API_URL = "https://api.mercari.jp/items/get?id={}"
USER_PROFILE_URL = "https://jp.mercari.com/user/profile/{}"
ALGORITHM = ALGORITHMS.ES256


def generate_dpop(
url: str,
method: str,
key: ecdsa.SigningKey,
extra_payload: Optional[Dict[str, str]] = None,
) -> str:
payload = {
"iat": int(datetime.now().timestamp()),
"jti": str(uuid4()),
"htu": url,
"htm": method,
**(extra_payload or {}),
}

headers = {
"typ": "dpop+jwt",
"alg": ALGORITHM,
"jwk": key.get_verifying_key().to_jwk(),
}

return jws.sign(payload, key, headers, ALGORITHM)


def create_headers(method: str, url: str) -> Dict[str, str]:
key = ecdsa.SigningKey.generate(curve=ecdsa.NIST256p)
dpop_token = generate_dpop(url, method, key, extra_payload={"uuid": str(uuid4())})
return {
"DPoP": dpop_token,
"X-Platform": "web",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Type": "application/json; charset=utf-8",
"User-Agent": "python-mercari",
}


def extract_item_id(text: str) -> Optional[str]:
match = re.search(r"(?<=item/)(m\d+)", text)
return match.group(1) if match else None


def timestamp_to_datetime(timestamp: int) -> str:
return datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")


@bot.message_handler(func=lambda message: True)
def process_message(message):
item_id = extract_item_id(message.text)
if item_id:
try:
url = MERCARI_API_URL.format(item_id)
headers = create_headers("GET", url)
response = requests.get(url, headers=headers)
response.raise_for_status() # This will raise an exception for HTTP errors
data = response.json()
buyer_id = data.get("data", {}).get("buyer", {}).get("id")
if buyer_id:
profile_url = USER_PROFILE_URL.format(buyer_id)
updated = timestamp_to_datetime(data.get("data", {}).get("updated"))
created = timestamp_to_datetime(data.get("data", {}).get("created"))
reply_message = f"Buyer profile: {profile_url}\nUpdated at: {updated}\nCreated at: {created}"
bot.reply_to(message, reply_message)
except requests.RequestException as e:
bot.reply_to(
message,
"Failed to retrieve item information. Please check the item ID or try again later.",
)
else:
bot.reply_to(message, "Please send a valid Mercari item ID or link.")


if __name__ == "__main__":
bot.polling(none_stop=True)
Loading

0 comments on commit 3ae6b36

Please sign in to comment.