4
submitted 1 year ago* (last edited 1 year ago) by freamon@feddit.nl to c/linuxscripts@lemmy.zip
#!/bin/bash

# Basic login script for Lemmy API

# CHANGE THESE VALUES
my_instance=""			# e.g. https://feddit.nl
my_username=""			# e.g. freamon
my_password=""			# e.g. hunter2

########################################################

# Lemmy API version
API="api/v3"

########################################################

# Turn off history substitution (avoid errors with ! usage)
set +H

########################################################

# Login
login() {
	end_point="user/login"
	json_data="{\"username_or_email\":\"$my_username\",\"password\":\"$my_password\"}"

	url="$my_instance/$API/$end_point"

	curl -H "Content-Type: application/json" -d "$json_data" "$url"
}

login

# Make note of "jwt" from reply

This'll reply with JSON data, that includes a value for "jwt", to be used in other scripts that require you to be logged in.

Personally, I'd use the 'jq' program to de-serialize the JSON data, but I thought I'd keep the script simple for now

top 5 comments
sorted by: hot top controversial new old
[-] kenoh@lemm.ee 1 points 9 months ago* (last edited 9 months ago)

Great. Now how would I make a post? Is there an API endpoint list somewhere?

I wanted to schedule my community posts, and tried to use this: https://github.com/RikudouSage/LemmySchedule but it doesn't actually make posts for me. If I could use stuff like the above I could crontab it all out.

EDIT: Hmmm, looks like I found it https://join-lemmy.org/api/interfaces/CreatePost.html Now to test and figure it all out.

[-] freamon@feddit.nl 2 points 9 months ago* (last edited 9 months ago)

Hi. I've found the best place to test the api is here

Example script for making a post

my_instance="https://lemm.ee"

auth="YOUR-JWT"
community_id=COMMUNITY-ID
name="TITLE OF POST"
body="BODY OF POST"

API="api/v3"

create() {
        end_point="post"
        json_data="{\"auth\":\"$auth\",\"community_id\":$community_id,\"name\":\"$name\",\"body\":\"$body\"}"

        url="$my_instance/$API/$end_point"

        curl -H "Content-Type: application/json" -d "$json_data" "$url"
}

create

You'll need to get your instance's community.id for the community you want to post to. To use this community as an example, it would be:

my_instance="https://lemm.ee"

API="api/v3"

resolve() {
        end_point="resolve_object"
        www_data="q=https://lemmy.zip/c/linuxscripts"

        url="$my_instance/$API/$end_point?$www_data"

        curl "$url"
}

resolve

(lemmy will probably sanitise the ampersand in that query, so you'll have to replace the HTML with a literal ampersand)

[-] kenoh@lemm.ee 1 points 9 months ago

Awesome, thanks a bunch. I'm working through this now as I have minutes to spare, but the community id thing is returning "Query deserialize error: missing field q". I'm wondering if special characters and stuff are being parsed. I also replaced auth= in line 3 with jwt=, just FYI. Thanks, I'll keep plugging.

[-] freamon@feddit.nl 2 points 9 months ago

Ooops. I tested it before I posted, and the reason it worked is because it turns out that authentication isn't needed for that query.

I've edited the comment now, and there only being one part means that there's no need for an ampersand, so there's no probs with special characters.

[-] kenoh@lemm.ee 2 points 9 months ago

That got me there. I just made my first scripted post. I'll take it from here with queuing my content. Thanks again!

this post was submitted on 08 Jul 2023
4 points (100.0% liked)

Linux Troubleshooting and scripts

431 readers
1 users here now

this is a community for sharing your scripts that help when working with Linux as well a general troubleshooting.

Rules

Feel free to ask any kind of support questions, as we grow more succinct answers will be available. Let's make this community grow.

We are important to one another, not only in the linux world but also in the fediverse as a whole. Feel free to reach out to me with questions or concerns, as we grow bigger I will look into adding mods. (This section will be updated as needed.)

founded 1 year ago
MODERATORS