Skip to content

Commit

Permalink
feat: reomove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamhunter2333 committed Apr 18, 2024
1 parent aa6a10b commit 92123c3
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 528 deletions.
7 changes: 0 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,3 @@ db_url=mysql+mysqlconnector://xxx:xxx/xxx
origin=xxx
cdn=xxx
token=xxx
ios_in_review_path=in_review.json
ios_in_review_uids=[1,2]
ios_in_review_fake_path=x.json
enable_prometheus=true
prometheus_host=localhost:9091
instance_name=awsl
repeat_seconds=10
14 changes: 5 additions & 9 deletions docker/dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
FROM python:3.9-slim
FROM python:3.12-slim

COPY requirements.txt /app/requirements.txt
RUN python3 -m pip install -r requirements.txt

COPY . /app
WORKDIR /app
RUN apt-get update \
&& apt-get install -y git \
# Clean up
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/*
RUN python3 -m pip install -r /app/requirements.txt
RUN rm -rf /root/.cache

ENTRYPOINT ["python3", "-m", "uvicorn", "--host", "0.0.0.0", "main:app"]
21 changes: 1 addition & 20 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,15 @@

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi_prometheus_pusher import FastApiPusher

from router.awsl_producers import router as producer_router
from router.awsl_pic import router as pic_router
from router.awsl_blob import router as blob_router
from router.health_check import router as health_check_router
from router.moyuban import router as moyu_router
from router.ios_faker import router as ios_faker_router
from router.config import settings


app = FastAPI()


if settings.enable_prometheus:
@app.on_event("startup")
async def startup():
FastApiPusher(
excluded_handlers=["health_check", "docs"]
).start(
app, settings.prometheus_host,
settings.instance_name,
repeat_seconds=settings.repeat_seconds
)


class EndpointFilter(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
return all(
Expand All @@ -50,11 +33,9 @@ def filter(self, record: logging.LogRecord) -> bool:
)

app.include_router(producer_router, prefix="")
app.include_router(pic_router, prefix="")
app.include_router(blob_router, prefix="")
app.include_router(health_check_router, prefix="")
app.include_router(moyu_router, prefix="")
app.include_router(ios_faker_router, prefix="")


if __name__ == "__main__":
import uvicorn
Expand Down
39 changes: 7 additions & 32 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
amqp==5.0.9
asgiref==3.4.1
billiard==3.6.4.0
certifi==2022.12.7
charset-normalizer==2.0.10
click==8.0.3
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.2.0
fastapi==0.68.0
-e git+https://github.com/dreamhunter2333/fastapi_prometheus_pusher.git@8454ad8bfebaa1b381ef4332c086de33955f8b1d#egg=fastapi_prometheus_pusher
greenlet==1.1.2
h11==0.12.0
idna==3.3
kombu==5.2.3
mysql-connector-python==8.0.27
prometheus-client==0.15.0
prompt-toolkit==3.0.24
protobuf==3.19.5
pydantic==1.8.2
python-dateutil==2.8.2
python-dotenv==0.19.0
pytz==2021.3
requests==2.26.0
six==1.16.0
SQLAlchemy==1.4.27
starlette==0.14.2
typing_extensions==4.0.1
urllib3==1.26.8
uvicorn==0.15.0
vine==5.0.0
wcwidth==0.2.5
fastapi==0.110.1
mysql-connector-python==8.3.0
pydantic==2.7.0
pydantic-settings==2.2.1
requests==2.31.0
SQLAlchemy==2.0.29
uvicorn==0.29.0
31 changes: 8 additions & 23 deletions router/awsl_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ def producer_photos(uids: Optional[List[str]] = None, limit: Optional[int] = 5):
status_code=404,
content=Message(message="to large limit = {}".format(limit))
)
_logger.info("producer_photos get limi %s" % limit)
session = DBSession()
try:
_logger.info("producer_photos get limit %s" % limit)
with DBSession() as session:
q = session.query(
AwslProducer
).filter(
Expand All @@ -51,8 +50,6 @@ def producer_photos(uids: Optional[List[str]] = None, limit: Optional[int] = 5):
photos=awsl_list(uid=producer.uid, limit=limit)
) for producer in producers
]
finally:
session.close()


@router.get("/v2/list", response_model=List[BlobItem], responses={404: {"model": Message}}, tags=["AwslV2"])
Expand All @@ -63,8 +60,7 @@ def awsl_list(uid: Optional[str] = "", limit: Optional[int] = 10, offset: Option
content=Message(message="to large limit = {}".format(limit))
)
_logger.info("list get uid %s limit %s offest %s" % (uid, limit, offset))
session = DBSession()
try:
with DBSession() as session:
blobs = session.query(AwslBlob).join(Mblog, AwslBlob.awsl_id == Mblog.id).filter(
Mblog.uid == uid
).order_by(AwslBlob.awsl_id.desc()).limit(limit).offset(offset).all() if uid else session.query(AwslBlob).join(
Expand All @@ -81,40 +77,31 @@ def awsl_list(uid: Optional[str] = "", limit: Optional[int] = 10, offset: Option
for blob_key, blob_pic in Blobs.parse_raw(blob.pic_info).blobs.items()
}
) for blob in blobs if blob.awsl_mblog]
finally:
session.close()
return res
return res


@router.get("/v2/list_count", response_model=int, tags=["AwslV2"])
def awsl_list_count(uid: Optional[str] = "") -> int:
session = DBSession()
try:
with DBSession() as session:
res = session.query(func.count(AwslBlob.id)).join(Mblog, AwslBlob.awsl_id == Mblog.id).filter(
Mblog.uid == uid
).one() if uid else session.query(func.count(AwslBlob.id)).one()
finally:
session.close()
return int(res[0]) if res else 0
return int(res[0]) if res else 0


@router.get("/v2/random", response_model=str, tags=["AwslV2"])
def awsl_random() -> str:
session = DBSession()
try:
with DBSession() as session:
blob = session.query(AwslBlob).order_by(
func.rand()
).limit(1).one()
url_dict = Blobs.parse_raw(blob.pic_info).blobs
return url_dict["original"].url
finally:
session.close()


@router.get("/v2/random_json", response_model=BlobItem, tags=["AwslV2"])
def awsl_random_json() -> str:
session = DBSession()
try:
with DBSession() as session:
blob = session.query(AwslBlob).order_by(
func.rand()
).limit(1).one()
Expand All @@ -130,5 +117,3 @@ def awsl_random_json() -> str:
for blob_key, blob_pic in Blobs.parse_raw(blob.pic_info).blobs.items()
}
)
finally:
session.close()
68 changes: 0 additions & 68 deletions router/awsl_pic.py

This file was deleted.

27 changes: 8 additions & 19 deletions router/awsl_producers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

@router.get("/producers", response_model=List[ProducerRes], tags=["producers"])
def awsl_producers():
session = DBSession()
try:
with DBSession() as session:
producers = session.query(
AwslProducer
).filter(
Expand All @@ -31,9 +30,7 @@ def awsl_producers():
"uid": producer.uid,
"name": producer.name
} for producer in producers]
finally:
session.close()
return res
return res


@router.post("/producers", response_model=bool, responses={404: {"model": Message}}, tags=["producers"])
Expand All @@ -55,8 +52,8 @@ def add_awsl_producers(producer: ProducerItem):
status_code=status.HTTP_400_BAD_REQUEST,
content={"message": "No weibo user uid = {}".format(producer.uid)}
)
session = DBSession()
try:

with DBSession() as session:
res = session.query(AwslProducer).filter(
AwslProducer.uid == producer.uid).one_or_none()
if res:
Expand All @@ -75,15 +72,12 @@ def add_awsl_producers(producer: ProducerItem):
session.add(awsl_producer)
_logger.info("awsl add awsl_producer done %s" % awsl_producer.name)
session.commit()
finally:
session.close()
return True


@router.get("/in_verification_producers", response_model=List[ProducerRes], tags=["producers"])
def awsl_in_verification_producers():
session = DBSession()
try:
with DBSession() as session:
producers = session.query(
AwslProducer
).filter(
Expand All @@ -95,9 +89,7 @@ def awsl_in_verification_producers():
"uid": producer.uid,
"name": producer.name
} for producer in producers]
finally:
session.close()
return res
return res


@router.post("/approve_producers", response_model=bool, responses={404: {"model": Message}}, tags=["producers"])
Expand All @@ -113,8 +105,7 @@ def add_approve_producers(uid: str, token: str):
content={"message": "token is not correct"}
)

session = DBSession()
try:
with DBSession() as session:
awsl_producer = session.query(
AwslProducer
).filter(
Expand All @@ -134,6 +125,4 @@ def add_approve_producers(uid: str, token: str):
awsl_producer.in_verification = False
_logger.info("awsl approve awsl_producer done %s" % awsl_producer.name)
session.commit()
finally:
session.close()
return True
return True
14 changes: 5 additions & 9 deletions router/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from typing import List
import logging

from pydantic import BaseSettings
from pydantic_settings import BaseSettings


WB_DATA_URL = "https://weibo.com/ajax/statuses/mymblog?uid={}&page="
Expand All @@ -16,19 +16,15 @@
class Settings(BaseSettings):
cookie_sub: str
token: str
ios_in_review_path: str
ios_in_review_uids: List[str]
ios_in_review_fake_path: str
db_url: str
origin: str
cdn: str
enable_prometheus: bool
prometheus_host: str
instance_name: str
repeat_seconds: int

class Config:
env_file = os.environ.get("ENV_FILE", ".env")


settings = Settings()
logging.basicConfig(level=logging.INFO)
_logger = logging.getLogger(__name__)
_logger.info(settings.model_dump_json(indent=2))
3 changes: 0 additions & 3 deletions router/health_check.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import logging

from fastapi import APIRouter, status

router = APIRouter()
_logger = logging.getLogger(__name__)


@router.get("/health_check", tags=["health check"])
Expand Down
Loading

0 comments on commit 92123c3

Please sign in to comment.