Skip to content

Instantly share code, notes, and snippets.

@navitux
Created November 8, 2023 05:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navitux/c587e11a33e1466fa9ad11a30437138a to your computer and use it in GitHub Desktop.
Save navitux/c587e11a33e1466fa9ad11a30437138a to your computer and use it in GitHub Desktop.
CURL based script to do CRUD operations in your neocities.org site in an easy way
#!/bin/bash
# CURL based wrapper script to do CRUD operations in your neocities.org site one file at time
# Created by: Ivan Robles <navitux@disroot.org>
# License: GPLv3.0
VERSION="1.0"
USER="$NEOCITIES_USER"
PASSWORD="$NEOCITIES_PASS"
howtouse()
{
cat << EOM
neocitiesrequest.sh : CURL based wrapper script to do CRUD operations in your neocities.org site one file at time.
USAGE:[export NEOCITIES_USER=<username> NEOCITIES_PASS=<password> ;] $0 OPERATION [file2 file2]
This script works with 2 enviroment variables: NEOCITIES_USER and NEOCITIES_PASS that need to be defined before running this script, otherwise it'll fail almost all operations.
OPERATIONS:
upload, u : upload or update a file to server if already exists in server, it requires 2 file names, if just one is provided, then the local and server files will have the exact same name (including the path).
Example:
neocitiesrequest.sh upload "file1_server.html" "file1_local.html"
neocitiesrequest.sh upload "same_filename_in_both_sides.html"
neocitiesrequest.sh u "file1_server.html" "file1_local.html"
neocitiesrequest.sh u "same_filename_in_both_sides.html"
delete, d : deletes one file on the server side, this operations requires 1 filename.
Example:
neocitiesrequest.sh delete "file_to_delete.jpg"
neocitiesrequest.sh d "file_to_delete.jpg"
list, l : it returns a JSON output of all your files hosted in your site in neocities if you use it without argument. It can accept just a path name existent in the server, once provided it will return the file hierarchy of that path only.
Example:
neocitiesrequest.sh list
neocitiesrequest.sh l
neocitiesrequest.sh list "directory_to_browse"
neocitiesrequest.sh l "directory_to_browse"
site_info, si : This operation returns the general information of a neocities site, this only can accept the name of a user in the platform. This is the only operation that doesn't need user or password.
Example:
neocitiesrequest.sh site_info another_username
neocitiesrequest.sh si another_username
my_site_info, msi : This one returns information regarding your site in neocities.
Example:
neocitiesrequest.sh my_site_info
neocitiesrequest.sh msi
help, h: you get this help
Example:
neocitiesrequest.sh help
neocitiesrequest.sh h
Version: $VERSION
License: GPLv3.0
Author: Ivan Robles<navitux@disroot.org>
Enjoy.
EOM
exit 1
}
upload()
{
server_name="$1"
local_name="$2"
local_name=${local_name:-$1}
if [[ -z "$server_name" ]]
then
echo ""
echo cannot proceed without at least one argument
echo ""
exit 1
fi
curl --user "$USER":"$PASSWORD" --form "$server_name=@$local_name" "https://neocities.org/api/upload" && exit 0
}
delete()
{
file_to_delete="$1"
if [[ -z $file_to_delete ]]
then
echo cannot proceed without an argument
exit 2
fi
curl --user "$USER":"$PASSWORD" --data "filenames[]=$file_to_delete" "https://neocities.org/api/delete" && exit 0
}
list()
{
directory="$1"
if [[ -z "$directory" ]]
then
curl --silent --user "$USER":"$PASSWORD" "https://neocities.org/api/list" && exit 0
else
curl --silent --user "$USER":"$PASSWORD" "https://neocities.org/api/list?path=$directory" && exit 0
fi
}
my_site_info()
{
curl --silent --user "$USER":"$PASSWORD" "https://neocities.org/api/info" && exit 0
}
site_info()
{
given_username="$1"
if [[ -z $given_username ]]
then
echo cannot proceed without the name of the site to check
exit 2
fi
curl --silent "https://neocities.org/api/info?sitename=$given_username" && exit 0
}
case "$1" in
"upload" | "u")
upload $2 $3;;
"delete" | "d")
delete $2;;
"help" | "h")
howtouse;;
"list" | "l")
list $2;;
"my_site_info" | "msi")
my_site_info;;
"site_info" | "si")
site_info $2;;
*)
howtouse;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment