cosmicflow/scripts/generate_and_update_address_files.py
2024-06-21 23:08:58 +05:30

84 lines
2.8 KiB
Python

# Write to txt files
# [[file:../main.org::*Write to txt files][Write to txt files:1]]
def read_until_newline_after_name(filename,name):
with open(filename, 'r') as file:
lines = file.readlines()
content = ""
in_name = False
for line in lines:
if "#+name" in line and name in line:
in_name = True
continue
if in_name:
if line == '\n':
in_name = False
else:
content += line
return(content)
def write_to_file(file_path, content):
with open(file_path, 'w') as file:
file.write(content)
write_to_file('content/static/i2p-b32-address.txt',read_until_newline_after_name('main.org','i2p-b32-address'))
print('content/static/i2p-b32-address.txt written')
write_to_file('content/static/tor-onionv3-address.txt',read_until_newline_after_name('main.org','tor-onionv3-address'))
print('content/static/tor-onionv3-address.txt written')
# Write to txt files:1 ends here
# Read and write addresses to all files
# [[file:../main.org::*Read and write addresses to all files][Read and write addresses to all files:1]]
import os
def write_output_to_org_file(file_path, name, output):
erase_until_newline_after_name(file_path,name)
with open(file_path, 'r') as file:
data = file.readlines()
with open(file_path, 'w') as file:
for line in data:
if '#+name: ' in line and name in line:
file.write(line)
file.write(output)
else:
file.write(line)
def erase_until_newline_after_name(filename,name):
with open(filename, 'r') as file:
lines = file.readlines()
with open(filename, 'w') as file:
erase = False
for line in lines:
if "#+name" in line and name in line:
erase = True
file.write(line)
if erase:
if line == '\n':
erase = False
file.write(line)
else:
file.write(line)
def read_file(file_path):
try:
with open(file_path, 'r') as file:
file_contents = file.read()
return file_contents
except FileNotFoundError:
return "Not found"
directory = './content/'
for root,dir,filename in os.walk(directory):
for file in filename:
if file.endswith('.org') and file != 'main.org':
file_path = os.path.join(root, file)
# Process the org file here
write_output_to_org_file( file_path,"i2p-b32-address.txt", read_file('i2p-b32-address.txt'))
write_output_to_org_file( file_path,"tor-onionv3-address.txt", read_file('tor-onionv3-address.txt'))
print(f' Addresses written in {file_path}')
# Read and write addresses to all files:1 ends here