Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release/1.19.2 #71

Merged
merged 6 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions src/dataimport/pietsmietdevideoimporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import json
import asyncio
from uuid import uuid4
from PIL import Image
from io import BytesIO

from rich.console import Console
from databases import Database
Expand Down Expand Up @@ -128,15 +130,32 @@ async def stuff() -> asyncio.coroutine:
f"Try redownloading thumbnail for {content['remoteId']}...",
style="bright_magenta",
)

filename_id = uuid4()
try:
thumbnail = requests.get(content["imageUri"]).content
filename = f"{uuid4()}.jpg"
with open(f"/app/cdn/psde/{filename}", "wb") as f:
with open(f"/app/cdn/psde/{filename_id}.jpg", "wb") as f:
f.write(thumbnail)
newImageUri = f"/cdn/psde/{filename}"
newImageUri = f"/cdn/psde/{filename_id}.jpg"
except Exception as e:
console.log(f"Error downloading thumbnail: {e}", style="bold red")

try:
console.log(
f"Resizing thumbnail for {content['remoteId']}...",
style="bright_magenta",
)
image = Image.open(BytesIO(thumbnail))
width, height = 300, int((300 / image.width) * image.height)
resized_300 = image.resize((width, height))
resized_300.save(f"/app/cdn/psde/{filename_id}-w300.jpg")

width, height = 768, int((768 / image.width) * image.height)
resized_768 = image.resize((width, height))
resized_768.save(f"/app/cdn/psde/{filename_id}-w768.jpg")
except Exception as e:
console.log(f"Error resizing thumbnail: {e}", style="bold red")

await db.execute(
UPDATE_STATEMENT,
values={
Expand All @@ -156,16 +175,36 @@ async def stuff() -> asyncio.coroutine:
)

if content["imageUri"] != None:
filename_id = uuid4()
try:
console.log(
f"Downloading thumbnail for {content['remoteId']}...",
style="bright_magenta",
)
thumbnail = requests.get(content["imageUri"]).content
filename = f"{uuid4()}.jpg"
with open(f"/app/cdn/psde/{filename}", "wb") as f:
with open(f"/app/cdn/psde/{filename_id}.jpg", "wb") as f:
f.write(thumbnail)
content["imageUri"] = f"/cdn/psde/{filename}"
content["imageUri"] = f"/cdn/psde/{filename_id}.jpg"
except Exception as e:
console.log(f"Error downloading thumbnail: {e}", style="bold red")
content["imageUri"] = content["imageUri"]

try:
console.log(
f"Resizing thumbnail for {content['remoteId']}...",
style="bright_magenta",
)
image = Image.open(BytesIO(thumbnail))
width, height = 300, int((300 / image.width) * image.height)
resized_300 = image.resize((width, height))
resized_300.save(f"/app/cdn/psde/{filename_id}-w300.jpg")

width, height = 768, int((768 / image.width) * image.height)
resized_768 = image.resize((width, height))
resized_768.save(f"/app/cdn/psde/{filename_id}-w768.jpg")
except Exception as e:
console.log(f"Error resizing thumbnail: {e}", style="bold red")

await db.execute(
INSERT_STATEMENT,
values={
Expand Down
48 changes: 33 additions & 15 deletions src/dataimport/youtube_video_linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
collection_url = "https://www.googleapis.com/youtube/v3/search?part=snippet&order=date&maxResults=50&channelId={}&type=video&key={}"
youtube_url_template = "https://youtu.be/{}"

UPDATE_STATEMENT = "UPDATE ContentPiece SET description = :description, secondaryHref = :secondaryHref WHERE id = :id"
UPDATE_STATEMENT_CONTENTPIECE = "UPDATE ContentPiece SET description = :description, secondaryHref = :secondaryHref WHERE id = :id AND type = 'PSVideo'"
UPDATE_STATEMENT_PLAN = "UPDATE ScheduledContentPiece SET description = :description, secondaryHref = :secondaryHref WHERE remoteId = :remoteId AND type = 'PSVideo'"


def replace_emojis_with_question_mark(text):
Expand Down Expand Up @@ -74,27 +75,44 @@ async def youtube():
"upperBoundary": one_week_after.strftime("%Y-%m-%d %H:%M:%S"),
}

description = video["snippet"]["description"]
secondaryHref = youtube_url_template.format(video["id"]["videoId"])

result = await db.fetch_one(query=query, values=values)
if not result:
console.log(f"Could not find a matching video for '{title}'")
continue
if result.description is not None or result.secondaryHref is not None:
console.log(
f"ContentPiece {result.id} already has a description or secondaryHref"
)
continue

description = video["snippet"]["description"]
secondaryHref = youtube_url_template.format(video["id"]["videoId"])
if result.description is None and result.secondaryHref is None:
values = {
"id": result.id,
"description": description,
"secondaryHref": secondaryHref,
}

values = {
"id": result.id,
"description": description,
"secondaryHref": secondaryHref,
}
console.log(
f"Updating ContentPiece {result.id} with secondaryHref {secondaryHref}"
)
await db.execute(query=UPDATE_STATEMENT_CONTENTPIECE, values=values)
else:
console.log(
f"ContentPiece {result.id} already has a description and secondaryHref. Skipping update"
)

console.log(f"Updating {result.id} with secondaryHref {secondaryHref}")
await db.execute(query=UPDATE_STATEMENT, values=values)
if result.remoteId:
values = {
"remoteId": result.remoteId,
"description": description,
"secondaryHref": secondaryHref,
}
console.log(
f"Updating ScheduledContentPiece {result.remoteId} with secondaryHref {secondaryHref}"
)
await db.execute(query=UPDATE_STATEMENT_PLAN, values=values)
else:
console.log(
f"ContentPiece {result.id} does not have a remoteId. Skipping ScheduledContentPiece update"
)

await db.disconnect()
console.log("Done")
Expand Down
Loading
Loading