from app.libraries.setup import Setup
from app.libraries.database import Database
from app.libraries.peers import Peers
from app.libraries.nodeinfo import Nodeinfo
import ray

ray.init(num_cpus = 25) # Specify this system CPUs.

@ray.remote
def peerapi(peer):

    api = peers.getapi(peer) # get peer nodeinfo's URL

    if api != '':

        try:

            server, software = nd.getdata(peer, api)

            db.write_server(peer, api, software)

            print(f'server: {peer}, api: {api}, software: {software}')

        except:

            pass

if __name__ == '__main__':

    db = Database()

    setup = Setup()

    peers = Peers()

    nd = Nodeinfo()

    peers_list = peers.getpeers(setup.mastodon_hostname) #get peers from Mastodon host

    if peers_list != None:

        ray.get([peerapi.remote(peer) for peer in peers_list])
