import time
from datetime import datetime
import os
import json
import sys
import os.path
import requests
import urllib3
import socket
from app.libraries.database import Database
from app.libraries.setup import Setup
import pdb

def is_json(myjson):

    try:
        json_object = json.loads(myjson)
    except ValueError as e:
        return False
    return True

class Nodeinfo():

    name = "Query server nodeinfo data"

    def __init__(self, server=None, db=None, setup=None, soft=None):

        self.server = server
        self.db = Database()
        self.setup = Setup()
        self.soft = ''

    def getdata(self, server, api):

        if server.find(".") == -1:
            return
        if server.find("@") != -1:
            return
        if server.find("/") != -1:
            return
        if server.find(":") != -1:
            return

        url = 'https://' + server

        self.soft = ''

        try:

            response = requests.get(url + api, headers = self.setup.user_agent, timeout=3)

            if response.status_code == 200:

                try:

                    self.soft = response.json()['software']['name']
                    self.soft = self.soft.lower()

                    print(f"Server {server} ({self.soft} is alive!")
                    print('*********************************************************************')

                except:

                    pass

        except:

            pass

        return (server, self.soft)
