Skip to content

Instantly share code, notes, and snippets.

@BoQsc
Last active May 15, 2023 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BoQsc/7b19cb03e60b8f4865aa1981eebbd479 to your computer and use it in GitHub Desktop.
Save BoQsc/7b19cb03e60b8f4865aa1981eebbd479 to your computer and use it in GitHub Desktop.
Download revolt.chat server.
import json
import os
import requests
from dotenv import load_dotenv
def main():
load_dotenv()
TOKEN = 'rxPrvEqnIZZfZ4JO9Lu4dHf-bBhd_29CGKzWnaJeWKl71f01W_EMrV6lA_2aXsO'
SERVER_ID = '0sGJ0CVBeZV2HwYAq3S6ZKrN4N'
BASE_URL = 'https://api.revolt.chat'
client = requests.Session()
client.headers['x-session-token'] = TOKEN
url = f'{BASE_URL}/servers/{SERVER_ID}'
server = client.get(url).json()
with open(f'server-{SERVER_ID}.json', 'w') as file:
json.dump(server, file, indent='\t')
print(f'Server: {server}')
for channel_id in server['channels']:
if os.path.exists(f'messages-{channel_id}.json'):
continue
url = f'{BASE_URL}/channels/{channel_id}'
channel = client.get(url).json()
with open(f'channel-{channel_id}.json', 'w') as file:
json.dump(channel, file, indent='\t')
print(f'Channel: {channel}')
if channel.get('channel_type') != 'TextChannel':
continue
channel_messages = []
after = '0' * 26
url = f'{BASE_URL}/channels/{channel_id}/messages'
while True:
messages = client.get(url, params={'after': after, 'limit': 100, 'sort': 'Oldest'}).json()
channel_messages.extend(messages)
print(f'Messages: {channel_id}, {len(channel_messages)}')
if not messages:
break
if isinstance(messages, list) and len(messages) > 0:
after = messages[-1]['_id']
with open(f'messages-{channel_id}.json', 'w') as file:
json.dump(channel_messages, file, indent='\t')
print(f'ChannelMessages: {channel_id}, {len(channel_messages)}')
if __name__ == '__main__':
main()
@BoQsc
Copy link
Author

BoQsc commented May 15, 2023

concise version:

import os, json, requests

client = requests.Session()
client.headers['x-session-token'] = ' YOUR_TOKEN'

server = client.get('https://api.revolt.chat/servers/YOUR_SERVER').json()

json.dump(server, open(f'server-01GJ0CVB1ZV2H1YA13S6ZK4N4N.json', 'w'), indent='\t')

for channel_id in server['channels']:
    if not os.path.exists(f'messages-{channel_id}.json'):
        channel = client.get(f'https://api.revolt.chat/channels/{channel_id}').json()
        json.dump(channel, open(f'channel-{channel_id}.json', 'w'), indent='\t')
        if channel.get('channel_type') == 'TextChannel':
            channel_messages, after = [], '0' * 26
            while True:
                messages = client.get(f'https://api.revolt.chat/channels/{channel_id}/messages', params={'after': after, 'limit': 100, 'sort': 'Oldest'}).json()
                if not messages: break
                channel_messages.extend(messages)
                if isinstance(messages, list) and len(messages) > 0: after = messages[-1]['_id']
            json.dump(channel_messages, open(f'messages-{channel_id}.json', 'w'), indent='\t')

@BoQsc
Copy link
Author

BoQsc commented May 15, 2023

Even more concise:

import os, json, requests

client = requests.Session()
client.headers['x-session-token'] = ' YOUR_TOKEN'

server = client.get('https://api.revolt.chat/servers/YOUR_SERVER_ID').json()

json.dump(server, open(f'server-YOUR_SERVER_ID.json', 'w'), indent='\t')

for channel_id in server['channels']:
    if not os.path.exists(f'messages-{channel_id}.json'):
        json.dump(client.get(f'https://api.revolt.chat/channels/{channel_id}').json(), open(f'channel-{channel_id}.json', 'w'), indent='\t')
        if client.get(f'https://api.revolt.chat/channels/{channel_id}').json().get('channel_type') == 'TextChannel':
            messages = []
            while True:
                messages = client.get(f'https://api.revolt.chat/channels/{channel_id}/messages', params={'after': '0' * 26 if not isinstance(messages, list) or len(messages) == 0 else messages[-1]['_id'], 'limit': 100, 'sort': 'Oldest'}).json()
                if not messages or isinstance(messages, list) and len(messages) == 0:
                    break
                json.dump(messages, open(f'messages-{channel_id}.json', 'a'), indent='\t')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment