95 lines
3.5 KiB
Python
95 lines
3.5 KiB
Python
import yaml
|
|
from path import Path
|
|
import subprocess
|
|
from wxr2md.lib import Blog
|
|
|
|
WP_PATH = '/home/miembros-comerciojusto-org/htdocs/miembros.comerciojusto.org'
|
|
|
|
def run_wp_cli_export(c_slug, directory_path):
|
|
|
|
command = f"wp export --path=/home/miembros-comerciojusto-org/htdocs/miembros.comerciojusto.org --with_attachments --dir=\"{directory_path}/\" --post__in=\"$(wp post list --path=/home/miembros-comerciojusto-org/htdocs/miembros.comerciojusto.org --post_type=documento --tipo={c_slug} --format=ids --fields=ID)\" --filename_format={c_slug}.xml"
|
|
|
|
# command = f"wp export --post_type=documento --posts_per_page=-1 --cat-name={c_slug} --file=actas_{c_slug}.zip --path={directory_path}"
|
|
# print(command)
|
|
wxr_file = directory_path / (c_slug + ".xml")
|
|
if wxr_file.exists() is False:
|
|
subprocess.run(command, shell=True)
|
|
blog = Blog.from_file(wxr_file,types=['documento','attachment'])
|
|
attachments = []
|
|
docs = []
|
|
for item in blog.posts:
|
|
if item.type == 'attachment':
|
|
attachments.append(item)
|
|
elif item.type == 'documento':
|
|
docs.append(item)
|
|
for post in docs:
|
|
# print(post)
|
|
# print(post.type)
|
|
# print(post.to_md())
|
|
# if description:
|
|
description_file = directory_path / "Readme.md"
|
|
with open(description_file, 'w') as f:
|
|
f.write(post.to_md())
|
|
attachments = search_attachment(attachments,post.id)
|
|
for at in attachments:
|
|
# print(at.url)
|
|
path_rep = 'https://miembros.comerciojusto.org'
|
|
if at.url.startswith('http:'):
|
|
path_rep = 'http://miembros.comerciojusto.org'
|
|
# at_path = at.url.replace('http://miembros.comerciojusto.org', WP_PATH)
|
|
at_path = Path(at.url.replace(path_rep, WP_PATH))
|
|
new_path = directory_path / at_path.name
|
|
# print(at_path.name)
|
|
# print(at_path.exists())
|
|
if at_path.exists() and new_path.exists() is False:
|
|
at_path.copy(new_path)
|
|
# print(attachments)
|
|
return wxr_file
|
|
|
|
def search_attachment(posts, id):
|
|
results = []
|
|
for post in posts:
|
|
if post.parent == id:
|
|
results.append(post)
|
|
return results
|
|
|
|
|
|
# Replace 'your_directory_path' with the actual path where you want to create directories
|
|
directory_path = Path('/home/miembros-comerciojusto-org/htdocs/migrate-to-nx/export')
|
|
|
|
def get_yaml_content():
|
|
with open('tree.yaml','r') as tree_file:
|
|
return yaml.safe_load(tree_file)
|
|
|
|
def create_dir(dir,description=False):
|
|
dir_path = dir
|
|
if not dir_path.exists():
|
|
dir_path.mkdir(parents=True)
|
|
if description:
|
|
description_file = dir_path / "Readme.md"
|
|
with open(description_file, 'w') as f:
|
|
f.write(description)
|
|
|
|
def process_tree():
|
|
# Load the YAML content and create directories
|
|
tree = get_yaml_content()
|
|
for dir in tree:
|
|
create_dir(directory_path / dir['title'],dir.get('description'))
|
|
# create_directory_tree(tree, directory_path)
|
|
if 'subitems' in dir:
|
|
for item in dir['subitems']:
|
|
subpath = directory_path / dir['title'] / item['title']
|
|
create_dir(subpath, item.get('description'))
|
|
run_wp_cli_export(item['c_slug'], subpath)
|
|
|
|
# if 'c_slug' in subitem and subitem['c_slug']:
|
|
# c_slug = subitem['c_slug']
|
|
|
|
def main():
|
|
print("Hello from migrate-to-nx!")
|
|
process_tree()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|