Skip to content

Commit 4c63708

Browse files
committed
image save works, need to now put images on host.
1 parent 2769870 commit 4c63708

File tree

5 files changed

+57
-36
lines changed

5 files changed

+57
-36
lines changed

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ RUN pip install -r requirements.txt
1111
# Copy application code
1212
COPY . .
1313

14+
# Make sure .env will work
15+
RUN pip install python-dotenv
16+
17+
1418
# Expose port and run Flask
1519
EXPOSE 5000
1620
CMD ["python3", "-m", "flask", "run", "--host=0.0.0.0"]

app.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
from flask import Flask, render_template, request, jsonify, redirect, url_for
2-
from game_data import game_pages # Import the game_pages dictionary
32
import os
43
import subprocess
4+
from game_data import game_pages
55

66
app = Flask(__name__)
77

8+
# Absolute path to the image_downloader.py script in the Docker container
9+
image_downloader_path = os.path.join(os.path.dirname(__file__), 'static', 'images', 'image_downloader.py')
10+
811
@app.route('/')
912
def home():
1013
return render_template('home.html', game_pages=game_pages)
1114

12-
# Game list is generated at game_data.py
13-
# To add a new game make the html page and needed scripts first then add game at game_data.py last before publishing.
15+
@app.route('/start-download')
16+
def start_download():
17+
try:
18+
# Ensure that the script exists before running
19+
if os.path.exists(image_downloader_path):
20+
subprocess.run(['python', image_downloader_path], check=True)
21+
return jsonify({"message": "Download started!"}), 200
22+
else:
23+
return jsonify({"message": "Image downloader script not found!"}), 404
24+
except subprocess.CalledProcessError as e:
25+
return jsonify({"message": f"Error: {e}"}), 500
26+
1427
@app.route('/games/<game_name>.html')
1528
def game(game_name):
1629
if game_name in game_pages:

requirements.txt

188 Bytes
Binary file not shown.

static/images/image_downloader.py

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,27 @@
22
import json
33
import requests
44

5-
# Path to the JSON file containing the game data
6-
json_file = 'image_urls.json'
5+
# Get the absolute path to the current directory (static/images)
6+
current_dir = os.path.dirname(__file__)
7+
json_file = os.path.join(current_dir, 'image_urls.json') # This ensures the correct path
78

8-
# Folder where images will be saved
9-
save_folder = './static/images'
10-
11-
# Ensure the save folder exists
12-
if not os.path.exists(save_folder):
13-
os.makedirs(save_folder)
14-
15-
# Function to download and save the image
16-
def download_image(image_url, save_path):
9+
# Check if the file exists
10+
if not os.path.exists(json_file):
11+
print(f"File not found: {json_file}")
12+
else:
13+
# Open and read the JSON file
1714
try:
18-
# Send a GET request to the image URL
19-
response = requests.get(image_url)
20-
# Check if the request was successful
21-
if response.status_code == 200:
22-
# Open the file in write-binary mode and save the content
23-
with open(save_path, 'wb') as file:
24-
file.write(response.content)
25-
print(f"Downloaded image and saved as {save_path}")
26-
else:
27-
print(f"Failed to download {image_url}, status code: {response.status_code}")
28-
except Exception as e:
29-
print(f"Error downloading {image_url}: {e}")
30-
31-
# Load the JSON file containing the image URLs
32-
with open(json_file, 'r') as file:
33-
game_data = json.load(file)
34-
35-
# Iterate over each entry in the JSON data
36-
for game in game_data:
37-
game_name, rename_to, image_url = game
38-
save_path = os.path.join(save_folder, rename_to)
39-
download_image(image_url, save_path)
15+
with open(json_file, 'r') as file:
16+
data = json.load(file)
17+
print("Data loaded successfully")
18+
# Add your code to download the images
19+
for game, filename, url in data:
20+
# Download the image
21+
response = requests.get(url)
22+
with open(os.path.join(current_dir, filename), 'wb') as img_file:
23+
img_file.write(response.content)
24+
print(f"Downloaded image and saved as {filename}")
25+
except FileNotFoundError:
26+
print(f"File not found: {json_file}")
27+
except json.JSONDecodeError:
28+
print("Error decoding JSON")

templates/home.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ <h1>WebGSM</h1>
2323
</a>
2424
{% endfor %}
2525
</div>
26+
<!-- Add a button to start the image download process -->
27+
<button id="download-button">Download Images</button>
28+
<p id="download-status"></p>
2629
</div>
30+
31+
<script>
32+
// When the download button is clicked
33+
$('#download-button').click(function() {
34+
// Send an AJAX request to start the download process
35+
$.get('/start-download', function(data) {
36+
$('#download-status').text(data.message);
37+
}).fail(function(error) {
38+
$('#download-status').text("Error starting the download.");
39+
});
40+
});
41+
</script>
2742
</body>
2843
</html>

0 commit comments

Comments
 (0)