Last active
February 22, 2024 22:41
-
-
Save fmaida/349722add0a88f0f749e38715f7ffdd8 to your computer and use it in GitHub Desktop.
This python scripts allows to post a text file to the https://smol.pub server. Requires Python 3.7+ and the library "requests".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Version 1.0 | |
from pathlib import Path | |
from os.path import basename, isfile | |
import subprocess | |
import sys | |
import re | |
# The script needs the python library "requests". If the library | |
# is not found, the script will be halted | |
try: | |
import requests | |
except ModuleNotFoundError: | |
print("'Requests' Library not found. Please install requests.") | |
sys.exit(1) | |
HOST = "https://smol.pub" | |
def load_token(): | |
""" | |
Load the token string from the file | |
located under ~/.config/.smolpub | |
""" | |
config_path = Path.home() / ".config" / ".smolpub" | |
if not isfile(config_path): | |
raise FileNotFoundError(f"{config_path} does not exist") | |
with open(config_path, "r") as f: | |
token = f.read().strip() | |
return token | |
def open_validate_article(filename): | |
""" | |
Load an article from a file | |
and extracts its title, slug and content | |
""" | |
# Load the article from the file | |
with open(filename, "r") as f: | |
lines = f.readlines() | |
# Extract the title from the first line | |
title = lines[0][1:].strip() | |
# Extract the slug from the filename | |
slug = basename(filename).lower().split(".")[0] | |
full_pattern = re.compile('[^a-z0-9\\-]|_') | |
slug = re.sub(full_pattern, '', slug) | |
# Checks if the article is valid | |
is_valid = False | |
if len(lines) >= 2: | |
if lines[0].strip()[0] == "#" and lines[1].strip() == "": | |
is_valid = True | |
# Extract the content from the remaining lines | |
content = "".join(lines[2:]) | |
# Returns the title, slug, content | |
# and if the article is valid | |
return (title, slug, content, is_valid) | |
def upload_article(title, slug, content): | |
""" | |
Uploads an article to smol.pub server | |
""" | |
token = load_token() | |
headers = {"Content-Type": "application/x-www-form-urlencoded"} | |
cookies = {"smolpub": token} | |
payload = {"title": title, | |
"slug": slug, | |
"content": content} | |
request = requests.Request("POST", f"{HOST}/posts/save", | |
headers=headers, | |
data=payload, | |
cookies=cookies).prepare() | |
# Tries to send the article as a new post | |
session = requests.Session() | |
result = session.send(request) | |
if result.status_code == 500: | |
# If the upload fails because the article already | |
# exists, tries to send it again as an update | |
request = requests.Request("POST", | |
f"{HOST}/posts/{slug}/update", | |
headers=headers, data=payload, | |
cookies=cookies).prepare() | |
session = requests.Session() | |
result = session.send(request) | |
elif result.status_code == 200: | |
# Otherwise, the upload was successful | |
pass | |
else: | |
# In any other case of failure, | |
# prints the status code and exits | |
print(result.status_code) | |
sys.exit(1) | |
# Check that the user has provided the | |
# correct number of arguments | |
if len(sys.argv) < 2: | |
print(f"Usage: {basename(__file__)} <file>") | |
sys.exit(1) | |
# Load the article from the file and verifies its vailidity | |
title, slug, content, is_valid = open_validate_article(sys.argv[1]) | |
if is_valid: | |
upload_article(title=title, slug=slug, content=content) | |
else: | |
print(f"Invalid article: {sys.argv[1]}") | |
sys.exit(1) | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment