from datetime import datetime, timezone from typing import List from sqlalchemy.orm import Session import models import schemas from database import SessionLocal from logger import module_logger logger = module_logger("spider") def update_platform(platform_name: str, contests: List[schemas.Contest]): db: Session = SessionLocal() platform = ( db.query(models.Platform) .filter(models.Platform.source == platform_name) .first() ) for item in contests: contest = ( db.query(models.Contest) .filter(models.Contest.contest_id == item.contest_id) .first() ) if contest is None: contest = models.Contest() contest.platform = platform contest.contest_id = item.contest_id contest.link = item.link
from concurrent import futures import grpc from logger import module_logger from proto.health_pb2_grpc import add_HealthServicer_to_server from servicers.health import HealthServicer from proto.system_info_pb2_grpc import add_SystemInfoServicer_to_server from servicers.system_info import SystemInfoServicer GRPC_MAX_WORKERS = 10 GRPC_INSECURE_PORT = 50050 GRPC_SECURE_PORT = 50051 logger = module_logger(__name__) class Server(object): def __init__(self): self._create_server() self._add_servicers() def _create_server(self): logger.info("Creating server") logger.debug("ThreadPoolExecutor.max_workers = %s", GRPC_MAX_WORKERS) self.server = grpc.server( futures.ThreadPoolExecutor(max_workers=GRPC_MAX_WORKERS), options=self._server_options(), )
from datetime import datetime, timezone from typing import List from sqlalchemy import Column, Integer, String, ForeignKey, BigInteger from sqlalchemy.orm import relationship, Session from sqlalchemy.types import TypeDecorator from config import platforms from database import Base, engine, SessionLocal from logger import module_logger logger = module_logger("models") class TZDateTime(TypeDecorator): impl = BigInteger() cache_ok = True def process_bind_param(self, value: datetime, dialect): # 传入的参数必须有 tz info 信息 if isinstance(value, datetime) and value.tzinfo is None: raise ValueError("{!r} must be TZ-aware".format(value)) # 转换为 utc 时间存储 date = value.astimezone(tz=timezone.utc) return int(date.timestamp()) def process_result_value(self, value: int, dialect): return datetime.fromtimestamp(value, tz=timezone.utc) class Platform(Base):
def __init__(self): self.logger = module_logger(__name__)