Skip to content

Commit

Permalink
api finished (no user prefs for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
suchencjusz committed Jan 5, 2024
1 parent 16af6f5 commit d1e7197
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 122 deletions.
3 changes: 3 additions & 0 deletions FILMAN-CRAWLER/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
FROM python:3.11-slim-bullseye

RUN apt update
RUN apt install -y pkg-config default-libmysqlclient-dev build-essential

WORKDIR /FILMAN-CRAWLER

COPY requirements.txt requirements.txt
Expand Down
3 changes: 2 additions & 1 deletion FILMAN-CRAWLER/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def do_task(task: Task):
def main():
logging.info("Program started")

min_wait = 3 # Minimum wait time in seconds
min_wait = 2 # Minimum wait time in seconds
max_wait = 60 # Maximum wait time in seconds
wait_time = min_wait

Expand All @@ -186,6 +186,7 @@ def main():
logging.info("No tasks found")
wait_time = min_wait

time.sleep(wait_time)

if __name__ == "__main__":
main()
47 changes: 0 additions & 47 deletions FILMAN-CRAWLER/requirements.txt

This file was deleted.

84 changes: 36 additions & 48 deletions FILMAN-CRAWLER/tasks/scrap_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import requests
import ujson

from .utils import Task, TaskTypes, TaskStatus
from .utils import Tasks, FilmWeb

logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
Expand All @@ -10,23 +12,6 @@
)


class Task:
def __init__(
self, id_task, status, type, job, unix_timestamp, unix_timestamp_last_update
):
self.id_task = id_task
self.status = status
self.type = type
self.job = job
self.unix_timestamp = unix_timestamp
self.unix_timestamp_last_update = unix_timestamp_last_update

def __str__(self):
return (
f"{self.id_task} {self.status} {self.type} {self.job} {self.unix_timestamp}"
)


class Scraper:
def __init__(self, headers=None, series_id=None, endpoint_url=None):
self.headers = headers
Expand All @@ -41,8 +26,8 @@ def fetch(self, url):
return response.text

def scrap(self, task):
info_url = f"https://www.filmweb.pl/api/v1/title/{task.job}/info"
rating_url = f"https://www.filmweb.pl/api/v1/film/{task.job}/rating"
info_url = f"https://www.filmweb.pl/api/v1/title/{task.task_job}/info"
rating_url = f"https://www.filmweb.pl/api/v1/film/{task.task_job}/rating"

info_data = self.fetch(info_url)
rating_data = self.fetch(rating_url)
Expand All @@ -56,42 +41,45 @@ def scrap(self, task):
title = info_data.get("title", None)
year = int(info_data.get("year", None))
other_year = int(info_data.get("otherYear", 0))
poster_url = info_data.get("posterPath", "https://vectorified.com/images/no-data-icon-23.png")
poster_url = info_data.get(
"posterPath", "https://vectorified.com/images/no-data-icon-23.png"
)
community_rate = rating_data.get("rate", None)

if title is None or year is None or poster_url is None:
return False

return self.update_data(
self.series_id, title, year, other_year, poster_url, community_rate, task.id_task
self.series_id,
title,
year,
other_year,
poster_url,
community_rate,
task.task_id,
)


def update_data(self, series_id, title, year, other_year, poster_url, community_rate, id_task):
# r = requests.post(
# f"{self.endpoint_url}/series/update",
# headers=self.headers,
# json={
# "id": int(series_id),
# "title": str(title),
# "year": int(year),
# "other_year": int(other_year),
# "poster_uri": str(poster_url),
# "community_rate": float(community_rate),
# },
# )

if r.status_code != 200:
logging.error(f"Error updating series data: HTTP {r.status_code}")
logging.error(r.text)
return False

# r = requests.get(
# f"{self.endpoint_url}/task/update?id_task={id_task}&status=done",
# headers=self.headers,
# )

if r.status_code != 200:
def update_data(
self, series_id, title, year, other_year, poster_url, community_rate, task_id
):
try:
filmweb = FilmWeb(self.headers, self.endpoint_url)
filmweb.update_series(
FilmWeb.FilmWebSeries(
id=series_id,
title=title,
year=year,
other_year=other_year,
poster_url=poster_url,
community_rate=community_rate,
)
)

tasks = Tasks(self.headers, self.endpoint_url)
tasks.update_task_status(task_id, TaskStatus.COMPLETED)

except Exception as e:
logging.error(f"Error updating series data: {e}")
return False

return True
30 changes: 30 additions & 0 deletions FILMAN-CRAWLER/tasks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ class FilmWebMovie(BaseModel):
poster_url: str
community_rate: float

class FilmWebSeries(BaseModel):
id: int
title: str
year: int
other_year: int
poster_url: str
community_rate: float


def update_movie(self, movie: FilmWebMovie):
r = requests.post(
f"{self.endpoint_url}/filmweb/update/movie",
Expand All @@ -82,3 +91,24 @@ def update_movie(self, movie: FilmWebMovie):
return False

return True

def update_series(self, series: FilmWebSeries):
r = requests.post(
f"{self.endpoint_url}/filmweb/update/series",
headers=self.headers,
json={
"id": int(series.id),
"title": str(series.title),
"year": int(series.year),
"other_year": int(series.other_year),
"poster_url": str(series.poster_url),
"community_rate": float(series.community_rate),
},
)

if r.status_code != 200:
logging.error(f"Error updating series data: HTTP {r.status_code}")
logging.error(r.text)
return False

return True
2 changes: 1 addition & 1 deletion FILMAN-DISCORD/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


# bot = lightbulb.BotApp(
# "MTE4MjM3MTY1ODM0NzA2NTM5NA.Gr7OHu.Y3PW4Dl98nLZAzAw1eJfvAzpushE3GXyM0qAX8",
# "MTE4NDUzMTQxMjY2MjEwNDA5NQ.GDmZof.QH06crcIcS3vdiFeH5JhLkkCv-pz2GcccB8360",
# intents=hikari.Intents.ALL,
# banner=None,
# )
Expand Down
Binary file removed FILMAN-DISCORD/requirements.txt
Binary file not shown.
3 changes: 3 additions & 0 deletions FILMAN-SERVER/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
FROM python:3.11-slim-bullseye

RUN apt update
RUN apt install -y pkg-config default-libmysqlclient-dev build-essential

WORKDIR /FILMAN-SERVER

COPY requirements.txt requirements.txt
Expand Down
14 changes: 8 additions & 6 deletions FILMAN-SERVER/database/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,14 @@ def __change_task_status(db: Session, task_id: int, task_status: schemas.TaskSta
return None

db_task.task_status = task_status
db_task.task_started = (
datetime.now() if task_status == schemas.TaskStatus.RUNNING else None
)
db_task.task_finished = (
datetime.now() if task_status == schemas.TaskStatus.COMPLETED else None
)

task_started = datetime.now() if task_status == schemas.TaskStatus.RUNNING else None
if task_started is not None:
db_task.task_started = task_started

task_finished = datetime.now() if task_status == schemas.TaskStatus.COMPLETED else None
if task_finished is not None:
db_task.task_finished = task_finished

db.commit()
db.refresh(db_task)
Expand Down
8 changes: 7 additions & 1 deletion FILMAN-SERVER/database/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
from sqlalchemy.orm import sessionmaker

SQLALCHEMY_DATABASE_URL = os.environ.get(
"SQLALCHEMY_DATABASE_URL", "mysql://root@localhost/filmweb_test3"
"SQLALCHEMY_DATABASE_URL", None
)

# SQLALCHEMY_DATABASE_URL = os.environ.get(
# "SQLALCHEMY_DATABASE_URL", "mysql://root@localhost/filmweb_test3"
# )

print(SQLALCHEMY_DATABASE_URL)

engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Expand Down
2 changes: 1 addition & 1 deletion FILMAN-SERVER/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ async def trigger_error():


if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8001)
uvicorn.run(app, host="0.0.0.0", port=8000)
29 changes: 12 additions & 17 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,20 @@ services:
context: .
dockerfile: FILMAN-SERVER/Dockerfile
environment:
- MYSQL_HOST=host.docker.internal
# - MYSQL_USER=root
# - MYSQL_PASSWORD=
- MYSQL_DATABASE=filmweb_test2
- SQLALCHEMY_DATABASE_URL=mysql://y168:1B00_3267ae@mysql.mikr.us/db_y168
restart: unless-stopped
# extra_hosts:
# - "host.docker.internal"

filman-discord:
depends_on:
- filman-server
container_name: filman-discord
build:
# context: /FILMAN-DISCORD
context: .
dockerfile: FILMAN-DISCORD/Dockerfile
environment:
- DISCORD_TOKEN=MTE4NDUzMTQxMjY2MjEwNDA5NQ.GDmZof.QH06crcIcS3vdiFeH5JhLkkCv-pz2GcccB8360
restart: unless-stopped
# filman-discord:
# depends_on:
# - filman-server
# container_name: filman-discord
# build:
# # context: /FILMAN-DISCORD
# context: .
# dockerfile: FILMAN-DISCORD/Dockerfile
# environment:
# - DISCORD_TOKEN=MTE4NDUzMTQxMjY2MjEwNDA5NQ.GDmZof.QH06crcIcS3vdiFeH5JhLkkCv-pz2GcccB8360
# restart: unless-stopped

filman-crawler:
depends_on:
Expand Down
Binary file modified requirements.txt
Binary file not shown.

0 comments on commit d1e7197

Please sign in to comment.