python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
By default, the date are displayed using the French locale. Make sure to have it installed.
# display list of installed locale
locale -a
# generate french locale
sudo locale-gen fr_FR.UTF-8
# update locale database
sudo update-locale
# french locale should be displayed
locale -a
Blog root file is stored in .env file
blog_url="https://myblog.blogger.com/"
There are 3 scripts to use in order
- crawl_blog.py : get a list of blog post urls and store them in a file
- scrapblog.py : using the file list of urls in input, download all posts in mardown format (images are also downloaded)
- the blog can be generated again using generate_website.py script
This script allows you to crawl a blog and retrieve URLs of blog posts.
- Python 3.x
- Required Python packages (install using
pip install -r requirements.txt):- requests
- beautifulsoup4
- python-dotenv
- Create a
.envfile in the same directory as the script. - Add the following line to the
.envfile, replacing<your-blog-url>with your actual blog URL: blog_url=""
Run the script using the following command:
python crawl_blog.py -h -f FIRST_POST_URL -m MAX_POSTS -o OUTPUT
-h,--help: Show the help message and exit.-f FIRST_POST_URL,--first_post_url FIRST_POST_URL: URL of the first post to start crawling from.-m MAX_POSTS,--max_posts MAX_POSTS: Maximum number of posts to crawl (integer).-o OUTPUT,--output OUTPUT: Output file to save the crawled URLs.
- Crawl all posts starting from the blog URL specified in the
.envfile:python crawl_blog.py -f https://example.com/first-post -m 50 - Crawl and save URLs to a custom output file:
python crawl_blog.py -o my_blog_urls.txt - Combine multiple options:
python crawl_blog.py -f https://example.com/start-here -m 100 -o recent_posts.txt
- If no
first_post_urlis provided via command line, the script will attempt to use theblog_urlfrom the.envfile. - If no
max_postsis specified, the script will attempt to crawl all available posts. - The default output file is
all_urls.txtif not specified.
The script will print progress information to the console and save the crawled URLs to the specified output file (or all_urls.txt by default).
To scrape your blog posts and save them locally, run the following command:
python3 scrapblog.py -u <blog_url> -t <target_directory> -m <max_pages>
-u,--blog_url: The URL of the blog you want to scrape. This is a required argument.-t,--target_directory: The directory where the scraped blog posts will be saved as Markdown files. This is a required argument.-m,--max_pages: The maximum number of pages (blog posts) to scrape. If not provided, the script will scrape all available posts.
For example, to scrape the latest 50 posts from https://myblog.example.com and save them as Markdown files in the ~/blog-backup directory, you would run:
python scrapblog.py -u https://myblog.example.com -t ~/blog-backup -m 50
The markdown files and images are stored in
MD_DIR/YYYY/MM/DD/XX-folder_name/post.md
MD_DIR/YYYY/MM/DD/XX-folder_name/images/*.jpg
The structure of the markdown file is composed of
- a yaml block
- the main text
- an optionnal comment section that starts with "Commentaires"
---
title: Direction equateur - 8 juillet
date: lundi 10 juillet 2023
tags: equateur
---
My blog post
blabla
Commentaires:
Anonymous (10 juillet 2023 à 14:52):
Good job !
This project contains a Python script (generate_website.py) that generates a static website from Markdown files. It creates HTML articles, collection pages, and index pages for a blog-like structure.
-
Ensure you have Python installed on your system.
-
Install the required dependencies (if any - you may need to create a
requirements.txtfile listing the dependencies). -
Prepare your Markdown files:
- Place all your Markdown (.md) files in a directory named
mdin the project root. - Each Markdown file should represent a blog post and include metadata (title, date, tags) at the top.
- Place all your Markdown (.md) files in a directory named
-
Run the script
-
The generated website will be created in the
htmldirectory.
After running the script, the following directory structure will be created:
html/
├── assets/
│ ├── css/
│ │ ├── style.css
│ │ ├── v0-article.css
│ │ ├── v0-article-black.css
│ │ └── v0-index.css
│ └── images/
├── articles/
│ └── generated HTML articles
├── collections/
│ └── generated collection pages
└── index.html
The script uses a configuration dictionary to manage various directories:
md_dir: Directory containing Markdown files (default: 'md')html_dir: Output directory for the generated website (default: 'html')assets_dir: Directory for static assets (default: 'html/assets')css_dir: Directory for CSS files (default: 'html/assets/css')img_dir: Directory for images (default: 'html/assets/images')articles_dir: Directory for generated HTML articles (default: 'html/articles')collections_dir: Directory for generated collection pages (default: 'html/collections')
- Generates individual HTML articles from Markdown files
- Creates collection pages based on tags
- Generates index pages
- Supports navigation between articles (previous/next links)
- Copies CSS files to the output directory
Tags are currently used to manage collections. Each article is associated to a collection, and a collection groups all articles containing this tag.
- Add parameters for "cache" directory, tags.json file -> review .env file to read each parameters
- I18N : get rid of
"Commentaires"constant inscrapblog.py - I18N : translate python comments from french to english
- Dynamic handling of comments : currently as the website is static, old comments were retreived and are displayed, but for new comments we need to modify the code.
- Add a post form with a jquery, to create a comment
- add a jquery to fetch recent comments
- have a backend server with REST API to get/add comments.
- once a day recreate the static web site by adding the most recent comments