From 71ca47846d7a0880622b6307a005727631dcef51 Mon Sep 17 00:00:00 2001 From: Rampoina Date: Wed, 16 Feb 2022 00:15:25 +0100 Subject: [PATCH] Add screenshots --- utils/appstream.py | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/utils/appstream.py b/utils/appstream.py index 9cea1ad..972ca3f 100644 --- a/utils/appstream.py +++ b/utils/appstream.py @@ -1,4 +1,5 @@ import gi +import requests import os import sys import inspect @@ -27,8 +28,8 @@ def acceptedGame(app): 'Game' in app.get_categories() and \ not [x for x in badCategories if x in app.get_categories()] -def getScreenshot(app): - return [images.get_source() for images in app.get_screenshots] +def getScreenshots(app): + return [images.get_source() for images in app.get_screenshots()] currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) parentdir = os.path.dirname(currentdir) @@ -36,6 +37,8 @@ sys.path.insert(0,parentdir) from app.models import * from app.utils import make_flask_login_password +from app.utils.image import get_image_size +from app.utils import randomString parser = argparse.ArgumentParser(description='Generate HTML files from an Appimage and mako templates.') parser.add_argument("-p", '--appstream_path', help='specify the path of the appstream file') @@ -51,14 +54,14 @@ session=db.session licenses = { x.name : x for x in License.query.all() } tags = { x.name : x for x in Tag.query.all() } admin_user = User.query.filter_by(username="appstream").first() -#print("Users: ", users) -#admin_user = User("appstream") -#admin_user.is_active = True -#admin_user.password = make_flask_login_password("appstream") -#admin_user.github_username = "appstream" -#admin_user.forums_username = "appstream" -#admin_user.rank = UserRank.ADMIN -#session.add(admin_user) +if not admin_user: + admin_user = User("appstream") + admin_user.is_active = True + admin_user.password = make_flask_login_password("appstream") + admin_user.github_username = "appstream" + admin_user.forums_username = "appstream" + admin_user.rank = UserRank.ADMIN + session.add(admin_user) for app in apps: @@ -68,7 +71,7 @@ for app in apps: print("APPLICATION: ",app.get_name()) game1 = Package() game1.state = PackageState.APPROVED - game1.name = app.get_id() + game1.name = app.get_id().replace('.','_') game1.title = app.get_name() game1.type = PackageType.GAME game1.license = licenses["MIT"] @@ -89,5 +92,25 @@ for app in apps: game1.desc = app.get_description() session.add(game1) + for i,screenshot in enumerate(screenshots): + ss = PackageScreenshot() + ss.package = game1 + ss.title = "Untitled" + url = screenshot.get_url() + r = requests.get(url) + filename = randomString(10) + "." + "png" + filepath = os.path.join("/var/cdb/uploads", filename) + with open(filepath,"wb") as f: + f.write(r.content) + + ss.url = "uploads/" + filename + ss.approved = True + ss.order = i + ss.width, ss.height = get_image_size(filepath) + session.add(ss) + session.commit() + game1.cover_image = ss + session.commit() + session.commit()