To create the stats.json
file, you can follow these steps:
- Clone the
lemmy-stats-crawler
repository from GitHub by running the following command in your terminal:
git clone https://github.com/LemmyNet/lemmy-stats-crawler
- Change your current directory to the cloned repository:
cd lemmy-stats-crawler
- Build and run the crawler using Cargo:
cargo run -- --json > stats.json
This command will execute the cargo run
command and redirect the output to a file named stats.json
. The --json
flag instructs the crawler to output the JSON data.
To create a string like (site:lemmy.world OR site:lemmy.ml OR site:beehaw.org OR site:feddit.de OR site:sh.itjust.works OR site:lemmy.one OR site:lemmy.ca)
from the list of instances extracted from the JSON output of the stats.json
file, you can follow these steps using Python:
- Import the
json
module to work with JSON data[1]. - Read the JSON file and load the data into a Python object[1].
- Extract the list of instances from the Python object.
- Create a string using the list of instances with the desired format.
Here's a Python script that demonstrates these steps:
import json
def sort_instances_by_monthly_users(data):
instances = [instance for instance in data["instance_details"]]
sorted_instances = sorted(instances, key=lambda x: x["site_info"]["site_view"]["counts"]["users_active_month"], reverse=True)
return sorted_instances[:20] # Limit to 20 instances
# Read the JSON file and load the data into a Python object
with open("stats.json", "r") as file:
data = json.load(file)
# Call the function to sort the instances and print the result
sorted_instances = sort_instances_by_monthly_users(data)
# Create a string using the list of instances with the desired format
formatted_string = " OR ".join([f"site:{instance['domain']}" for instance in sorted_instances])
formatted_string = f"({formatted_string})"
print(formatted_string)
This script will output a string in the desired format:
(site:lemmy.world OR site:lemmy.ml OR site:beehaw.org OR site:feddit.de OR site:sh.itjust.works OR site:lemmy.one OR site:lemmy.ca OR site:lemmy.blahaj.zone OR site:lemmygrad.ml OR site:lemmy.fmhy.ml OR site:sopuli.xyz OR site:lemmynsfw.com OR site:lemm.ee OR site:discuss.tchncs.de OR site:midwest.social OR site:lemmy.sdf.org OR site:lemmy.dbzer0.com OR site:aussie.zone OR site:feddit.uk OR site:feddit.it)
Citations: [1] https://www.geeksforgeeks.org/read-json-file-using-python/