diff --git a/game_collection/app.py b/game_collection/app.py index e612d40..dee199f 100644 --- a/game_collection/app.py +++ b/game_collection/app.py @@ -5,6 +5,8 @@ from models import db, Game from user_management import user_bp, authenticate import os from datetime import datetime +import requests +from bs4 import BeautifulSoup app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(app.instance_path, 'games.db') @@ -48,6 +50,50 @@ def get_games(): 'owned': game.owned } for game in games]) +@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 + app.register_blueprint(user_bp) # Register the user management blueprint if __name__ == '__main__':