2024-09-02 11:08:07 +02:00
|
|
|
from flask import Flask, request, jsonify, g
|
2024-09-02 09:02:32 +02:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
from database import init_db
|
|
|
|
from models import db, Game
|
2024-09-02 11:08:07 +02:00
|
|
|
from user_management import user_bp, authenticate
|
2024-09-02 09:35:10 +02:00
|
|
|
import os
|
2024-09-02 09:49:10 +02:00
|
|
|
from datetime import datetime
|
2024-09-02 17:02:38 +02:00
|
|
|
import requests
|
|
|
|
from bs4 import BeautifulSoup
|
2024-09-02 09:02:32 +02:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
2024-09-02 09:35:10 +02:00
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(app.instance_path, 'games.db')
|
2024-09-02 09:02:32 +02:00
|
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
|
db.init_app(app)
|
|
|
|
|
2024-09-02 09:35:10 +02:00
|
|
|
# Stelle sicher, dass der instance-Ordner existiert
|
|
|
|
os.makedirs(app.instance_path, exist_ok=True)
|
|
|
|
|
2024-09-02 09:02:32 +02:00
|
|
|
with app.app_context():
|
2024-09-02 09:32:28 +02:00
|
|
|
init_db() # Ensure the database is initialized before any operations
|
2024-09-02 09:02:32 +02:00
|
|
|
|
|
|
|
@app.route('/games', methods=['POST'])
|
|
|
|
def add_game():
|
2024-09-02 11:08:07 +02:00
|
|
|
if not authenticate():
|
|
|
|
return jsonify({'message': 'Unauthorized access!'}), 401
|
2024-09-02 09:02:32 +02:00
|
|
|
data = request.json
|
2024-09-02 11:47:29 +02:00
|
|
|
buyer_username = g.user.username # Get the username from the authenticated user
|
2024-09-02 09:02:32 +02:00
|
|
|
new_game = Game(
|
|
|
|
image=data['image'],
|
|
|
|
title=data['title'],
|
2024-09-02 09:49:10 +02:00
|
|
|
date=datetime.now().strftime('%Y-%m-%d'), # Set current date
|
2024-09-02 11:47:29 +02:00
|
|
|
buyer=buyer_username, # Set buyer as the authenticated user's username
|
2024-09-02 09:02:32 +02:00
|
|
|
owned=data['owned']
|
|
|
|
)
|
|
|
|
db.session.add(new_game)
|
|
|
|
db.session.commit()
|
|
|
|
return jsonify({'message': 'Game added!'}), 201
|
|
|
|
|
|
|
|
@app.route('/games', methods=['GET'])
|
|
|
|
def get_games():
|
2024-09-02 11:08:07 +02:00
|
|
|
if not authenticate():
|
|
|
|
return jsonify({'message': 'Unauthorized access!'}), 401
|
2024-09-02 09:02:32 +02:00
|
|
|
games = Game.query.all()
|
|
|
|
return jsonify([{
|
|
|
|
'id': game.id,
|
|
|
|
'image': game.image,
|
|
|
|
'title': game.title,
|
|
|
|
'date': game.date,
|
|
|
|
'buyer': game.buyer,
|
|
|
|
'owned': game.owned
|
|
|
|
} for game in games])
|
|
|
|
|
2024-09-02 17:02:38 +02:00
|
|
|
@app.route('/games/search', methods=['GET'])
|
|
|
|
def search_game_api():
|
|
|
|
search_term = request.args.get('search_keywords')
|
|
|
|
if not search_term:
|
|
|
|
return jsonify({'message': 'No search term provided!'}), 400
|
|
|
|
|
|
|
|
url = "https://www.switchscores.com/games/search"
|
|
|
|
params = {
|
|
|
|
'_token': 'UioRaADSoKqKdebnI66WRqBwtgqqte79sEMm0tVK',
|
|
|
|
'search_keywords': search_term
|
|
|
|
}
|
|
|
|
response = requests.get(url, params=params)
|
|
|
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
soup = BeautifulSoup(response.text, 'html.parser')
|
|
|
|
results = soup.select('.row > .col-sm-12') # Using CSS selector based on the provided HTML structure
|
|
|
|
|
|
|
|
games_list = [] # List to hold found games
|
|
|
|
for game in results:
|
|
|
|
title_tag = game.find('span', class_='h5').find('a') if game.find('span', class_='h5') else None
|
|
|
|
if title_tag: # Only output if a title is found
|
|
|
|
title = title_tag.text.strip()
|
|
|
|
link = title_tag['href']
|
|
|
|
image_tag = game.find('img') # Find the image tag
|
|
|
|
image_link = image_tag['src'] if image_tag and 'src' in image_tag.attrs else "Kein Bild gefunden" # Get the image link if it exists
|
|
|
|
if image_link != "Kein Bild gefunden":
|
|
|
|
image_link = f"https://www.switchscores.com{image_link}" # Prefix the image link
|
|
|
|
release_date = game.find('span', class_='h6').text.strip() if game.find('span', class_='h6') else "Kein Veröffentlichungsdatum gefunden"
|
|
|
|
price = game.find('span', class_='h4').text.strip() if game.find('span', class_='h4') else "Kein Preis gefunden"
|
|
|
|
rating = game.find('span', class_='switch-rating-badge').text.strip() if game.find('span', class_='switch-rating-badge') else "Keine Bewertung gefunden"
|
|
|
|
|
|
|
|
games_list.append({
|
|
|
|
'title': title,
|
|
|
|
'link': link,
|
|
|
|
'image_link': image_link,
|
|
|
|
'release_date': release_date,
|
|
|
|
'price': price,
|
|
|
|
'rating': rating
|
|
|
|
})
|
|
|
|
|
|
|
|
return jsonify(games_list), 200 # Return the list of games found
|
|
|
|
else:
|
|
|
|
return jsonify({'message': 'Error fetching the page.'}), 500
|
|
|
|
|
2024-09-02 10:32:26 +02:00
|
|
|
app.register_blueprint(user_bp) # Register the user management blueprint
|
|
|
|
|
2024-09-02 09:02:32 +02:00
|
|
|
if __name__ == '__main__':
|
2024-09-02 09:32:28 +02:00
|
|
|
with app.app_context():
|
|
|
|
init_db() # Ensure the database is initialized when the app starts
|
2024-09-02 09:02:32 +02:00
|
|
|
app.run(debug=True)
|