Peertube: Auto follow instances of instances.joinpeertube.org

Created on 25 Mar 2018  路  10Comments  路  Source: Chocobozzz/PeerTube

On our instance, we follow every other instance listed on instances.peertu.be as soon as they arrive on the website.
Problem, since the follow process has to be done manually, it is pretty boring.

So i was wondering if at the "Manage follows" section, we could add a checkbox (not checked by default) to auto-follow the instances listed on instances.peertu.be o/

Low Type

Most helpful comment

I updated the script to be more admin-friendly o/

#!/bin/bash

pt_user=""
pt_password=""

# Do NOT put http(s):// in this string
pt_instance="peertube.exemple"

echo $(jq ".client_id" <(curl "https://${pt_instance}/api/v1/oauth-clients/local") | tr -d '"') > /tmp/pt_client-id.txt
echo $(jq ".client_secret" <(curl "https://${pt_instance}/api/v1/oauth-clients/local") | tr -d '"') > /tmp/pt_client-secret.txt
clientid="$(cat /tmp/pt_client-id.txt)"
clientsecret="$(cat /tmp/pt_client-secret.txt)"
echo $(jq ".access_token" <(curl -X POST -d "client_id=${clientid}&client_secret=${clientsecret}&grant_type=password&response_type=code&username=${pt_user}&password=${pt_password}" https://${pt_instance}/api/v1/users/token) | tr -d '"') > /tmp/pt_token.txt
peertubetoken="$(cat /tmp/pt_token.txt)"
jq ".data | .[] | .host" <(curl "https://instances.joinpeertube.org/api/v1/instances?start=0&count=500") | tr -d '"' > /tmp/pt_list.txt
jq -R -s -c 'split("\n")' < /tmp/pt_list.txt | head --bytes -5 > /tmp/pt_process.txt
process='{"hosts":'
process+="$(cat /tmp/pt_process.txt)"
process+="]}"
echo $process > /tmp/pt_FINAL.txt
curl --data-binary "@/tmp/pt_FINAL.txt" -X POST "https://${pt_instance}/api/v1/server/following" -H "Host: ${pt_instance}" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0" -H "Accept: application/json, text/plain, /" -H "Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3" -H "Authorization: Bearer ${peertubetoken}" -H "Content-Type: application/json" -H "DNT: 1" -H "Connection: keep-alive"
cd /tmp
rm pt_client-id.txt pt_client-secret.txt pt_token.txt pt_list.txt pt_process.txt pt_FINAL.txt

All 10 comments

And an option in the admin panel and/or an action on the followers list to auto follow back or follow back from the list of followers will be fine too.

It's somehow what i was about to suggest. Basically, it's the following of a tracker of instances.

I would also add the ability for the tracker to automatically add and remove from his list the instances that start/stop following him...

Auto follow back and follow all instances listed on https://instances.peertu.be/ are two different issues. I created another issue: https://github.com/Chocobozzz/PeerTube/issues/413

from main post, this is awesome thanks for doing this.. <3

I got a toot on masto with this some weeks ago, and it is pretty interesting.
https://framabin.org/?4e7b46df544281f8#IRx+11i/dxYDSsE+Fw5R0HqTlTCtoVU1XnYalC+ZAUU=

(need to change instances.peertu.be to instances.joinpeertube.org)

I need to check if there is a possibility to automate the following process with a crontab.

Until it is implemented in peertube, i have written this little bash script.
Added on the crontab of peertube.nsa.ovh, seems to work for now :)

To get <token> ==> https://github.com/Chocobozzz/PeerTube/blob/develop/support/doc/api/quickstart.md

#!/bin/bash
jq ".data | .[] | .host" <(curl "https://instances.joinpeertube.org/api/v1/instances?start=0&count=500") | tr -d '"' > /tmp/peertube-list.txt
jq -R -s -c 'split("\n")' < /tmp/peertube-list.txt | head --bytes -5 > /tmp/process.txt
process='{"hosts":'
process+="$(cat /tmp/process.txt)"
process+="]}"
echo $process > /tmp/FINAL.txt
curl --data-binary "@/tmp/FINAL.txt" -X POST "https://peertube.exemple/api/v1/server/following" -H "Host: peertube.exemple" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0" -H "Accept: application/json, text/plain, /" -H "Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3" -H "Authorization: Bearer <token>" -H "Content-Type: application/json" -H "DNT: 1" -H "Connection: keep-alive"
rm /tmp/peertube-list.txt
rm /tmp/process.txt
rm /tmp/FINAL.txt

Thx @Chocobozzz for the help :)

I updated the script to be more admin-friendly o/

#!/bin/bash

pt_user=""
pt_password=""

# Do NOT put http(s):// in this string
pt_instance="peertube.exemple"

echo $(jq ".client_id" <(curl "https://${pt_instance}/api/v1/oauth-clients/local") | tr -d '"') > /tmp/pt_client-id.txt
echo $(jq ".client_secret" <(curl "https://${pt_instance}/api/v1/oauth-clients/local") | tr -d '"') > /tmp/pt_client-secret.txt
clientid="$(cat /tmp/pt_client-id.txt)"
clientsecret="$(cat /tmp/pt_client-secret.txt)"
echo $(jq ".access_token" <(curl -X POST -d "client_id=${clientid}&client_secret=${clientsecret}&grant_type=password&response_type=code&username=${pt_user}&password=${pt_password}" https://${pt_instance}/api/v1/users/token) | tr -d '"') > /tmp/pt_token.txt
peertubetoken="$(cat /tmp/pt_token.txt)"
jq ".data | .[] | .host" <(curl "https://instances.joinpeertube.org/api/v1/instances?start=0&count=500") | tr -d '"' > /tmp/pt_list.txt
jq -R -s -c 'split("\n")' < /tmp/pt_list.txt | head --bytes -5 > /tmp/pt_process.txt
process='{"hosts":'
process+="$(cat /tmp/pt_process.txt)"
process+="]}"
echo $process > /tmp/pt_FINAL.txt
curl --data-binary "@/tmp/pt_FINAL.txt" -X POST "https://${pt_instance}/api/v1/server/following" -H "Host: ${pt_instance}" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0" -H "Accept: application/json, text/plain, /" -H "Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3" -H "Authorization: Bearer ${peertubetoken}" -H "Content-Type: application/json" -H "DNT: 1" -H "Connection: keep-alive"
cd /tmp
rm pt_client-id.txt pt_client-secret.txt pt_token.txt pt_list.txt pt_process.txt pt_FINAL.txt

@skid9000 here's a python3 version, this version doesn't write token on unsafe place, do less request and don't bug if the is more 500 nodes and execute more faster :

#!/usr/bin/env python3
import json
import sys
import os
from urllib.parse import urlencode
from urllib.request import Request, urlopen

key = []
allNode = []

if not os.path.exists("secret.json"):
    print("you don't have any key configuration, let's do one right know")#use print for crash with broken pipe if there isn't any conf
    st = '{\n    "node":"'+input("node name (without https://) ? ")+'",\n    "account":"'+input("account name (with admin right) ? ")+'",\n    "password":"'+input("accout password ? ")+'"\n}\n'#use a temp string for one step make memory foot print bigger but if user cancel this no empty file will be created
    f = open("secret.json","w")
    f.write(st)
    f.close()
f = open("secret.json","r")
key = json.loads(f.read())
f.close()

for i in json.loads(urlopen(Request("https://instances.joinpeertube.org/api/v1/instances?start=0&count="+str(json.loads(urlopen(Request("https://instances.joinpeertube.org/api/v1/instances?start=0&count=0"), timeout=15).read().decode())["total"])), timeout=15).read().decode())["data"]:
    allNode.append(i["host"])

allNode.remove(key["node"])#don't send them self to the node we are seeding
if len(allNode) == 0:
    sys.stderr.write("node other node than original were found, you must follow or been followed by an already integrated node\n")
    sys.exit(3)

r = json.loads(urlopen(Request("https://"+key["node"]+"/api/v1/oauth-clients/local"),timeout=15).read().decode())
token = json.loads(urlopen(Request("https://"+key["node"]+"/api/v1/users/token",urlencode({"client_id":r["client_id"],"client_secret":r["client_secret"],"grant_type":"password","response_type":"code","username":key["account"],"password":key["password"]}).encode()),timeout=15).read().decode())
urlopen(Request("https://"+key["node"]+"/api/v1/server/following",(json.dumps({"hosts":allNode})+"\n").encode("ascii"),headers={"Authorization":"Bearer "+token["access_token"],"Content-Type":"application/json"}),timeout=15).read().decode()

sys.exit(1)

Thank youuuuu for these scripts !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Angedestenebres picture Angedestenebres  路  3Comments

NoraCodes picture NoraCodes  路  3Comments

conferences-gesticulees picture conferences-gesticulees  路  3Comments

filmaidykai picture filmaidykai  路  3Comments

Jorropo picture Jorropo  路  3Comments