im trying to make a e reader so i have a python3 http.server that i want to download and parse text files from i want to get the entire directory of txt files and for loop through them printing there name and content to the console but i cant figure it out
I have a custom flask container for parsing my music files on my home lab, maybe something like that could help here? This does not include how to make the container or parse your files in a certin manner. my json was set to use first folder as artists, the first subfolders as albums and second subfolders as songs. with them parsed that way i had my keys.
extends Node
const JSON_URL = "http://desktop:5000/music-data" #your local documents dir
const BASE_URL = "http://desktop:5000" # the base url alone
signal song_added(song_path) # I needed a signal as it was a seprate scene/node
func _ready(): # making the request
add_child(http_request)
http_request.request(JSON_URL)
http_request.request_completed.connect(_on_request_completed)
func _on_request_completed(result, response_code, headers, body): # if the request gets OK
if response_code == 200:
var json_data = body.get_string_from_utf8()
var json = JSON.new()
if json.parse(json_data) == OK:
data_store = json.get_data()
if typeof(data_store) == TYPE_DICTIONARY:
_populate_artists(data_store)
func _populate_artists(data): # adding artists keys to the data set into buttons
_clear_container()
for artist in data.keys():
var button = Button.new()
button.text = artist
button.pressed.connect(_on_artist_button_pressed.bind(artist))
container.add_child(button)
# adding and removing the special node
if event.is_action_pressed("t_network_share"):
if music_selection_instance == null:
# Instance and add to the scene tree if it doesn't exist
music_selection_instance = music_selection_scene.instantiate()
music_selection_instance.connect("song_added", add_song_to_playlist) # Connect the signal
add_child(music_selection_instance)
else:
# Remove from the scene tree if it exists
if music_selection_instance.is_inside_tree():
music_selection_instance.queue_free() # Remove it
music_selection_instance = null # Clear the reference
extends Control
var base_url
var files_to_download = []
func _ready() -> void:
var dir_access = DirAccess.open("user://")
if dir_access.dir_exists("Downloads"):
# First, delete all files and subdirectories in the Downloads directory
var x = DirAccess.open("user://Downloads/")
var y = x.get_files()
for file in y:
print(file)
x.remove("user://Downloads/"+file)
dir_access.remove("Downloads")
# Now create the Downloads directory again
dir_access.make_dir("Downloads")
func _on_submit_pressed() -> void:
base_url = $Server_container/Server_IP.text
fetch_directory()
func fetch_directory():
var run = HTTPRequest.new()
$Library.add_child(run)
run.connect("request_completed",httpr)
var err = run.request(base_url)
if err != OK:
print("Failed to request directory listing.")
func httpr(_result, response_code, _headers, body):
if response_code == 200:
$Server_container.hide()
$Library.show()
if files_to_download.size() == 0: # If fetching directory
var html_content = body.get_string_from_utf8()
files_to_download = parse_html_for_txt_files(html_content)
# Print the names of the found files
print("Found .txt files on the server:")
for file_name in files_to_download:
print(file_name)
for file_name in files_to_download:
download_file(file_name)
else: # If downloading files
var last_file_name = files_to_download[files_to_download.size() - 1]
var file_path = "user://Downloads/" + last_file_name
# Check and create the downloads directory if it doesn't exis
var file_access = FileAccess.open(file_path, FileAccess.WRITE)
if file_access:
file_access.store_string(body.get_string_from_utf8())
file_access.close()
print("Downloaded: ", last_file_name)
read_file_content(file_path)
setlib()
else:
print("Failed to save file: ", last_file_name)
else:
print("Error: ", response_code)
func parse_html_for_txt_files(html_content: String) -> Array:
var txt_files = []
# Simple regex to match .txt files in HTML
var regex = RegEx.new()
regex.compile('href="([^"]*\\.txt)"')
var matches = regex.search_all(html_content)
for match in matches:
txt_files.append(match.strings[1]) # Add the matched file name
return txt_files
func download_file(file_name: String):
var url = base_url + file_name
var run = HTTPRequest.new()
$Library.add_child(run)
run.connect("request_completed",httpr)
var err = run.request(url)
if err != OK:
print("Failed to request directory listing.")
func read_file_content(file_path: String):
var file_access = FileAccess.open(file_path, FileAccess.READ)
if file_access:
var content = file_access.get_as_text() # Read the entire file as a string
file_access.close() # Don't forget to close the file
print(content)
else:
print("Failed to open file for reading.")
return ""
func setlib():
var files = DirAccess.get_files_at("user://Downloads/")
for file in files:
var book = load("res://book.tscn")
var book_i = book.instantiate()
$Library/GridContainer.add_child(book_i)
book_i.book = file.get_basename()
func _on_quicklnk_1_pressed() -> void:
base_url = "http://0.0.0.0:8000/"
fetch_directory()
func _on_timer_timeout() -> void:
for child in $Library.get_children():
if child is HTTPRequest:
child.queue_free()