This commit is contained in:
callebtc
2022-08-24 20:40:16 +02:00
parent f2207f797d
commit 4867d649c9
3 changed files with 334 additions and 0 deletions

76
api/1ml.go Normal file
View File

@@ -0,0 +1,76 @@
package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
type OneML_NodeInfoResponse struct {
LastUpdate int `json:"last_update"`
PubKey string `json:"pub_key"`
Alias string `json:"alias"`
Addresses []struct {
Network string `json:"network"`
Addr string `json:"addr"`
} `json:"addresses"`
Color string `json:"color"`
Capacity int `json:"capacity"`
Channelcount int `json:"channelcount"`
Noderank struct {
Capacity int `json:"capacity"`
Channelcount int `json:"channelcount"`
Age int `json:"age"`
Growth int `json:"growth"`
Availability int `json:"availability"`
} `json:"noderank"`
}
type OneMlClient struct {
}
func GetOneMlClient() OneMlClient {
return OneMlClient{}
}
func (c *OneMlClient) GetNodeInfo(pubkey string) (OneML_NodeInfoResponse, error) {
url := fmt.Sprintf("https://1ml.com/node/%s/json", pubkey)
log.Infof("Getting info from 1ml.com for %s", pubkey)
client := http.Client{
Timeout: time.Second * 2, // Timeout after 2 seconds
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatal(err)
}
res, getErr := client.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
if res.Body != nil {
defer res.Body.Close()
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
r := OneML_NodeInfoResponse{}
jsonErr := json.Unmarshal(body, &r)
if jsonErr != nil {
log.Errorf("[1ml] api error: %v", jsonErr)
}
return r, nil
}

224
api/amboss.go Normal file
View File

@@ -0,0 +1,224 @@
package api
import (
"context"
"time"
"github.com/machinebox/graphql"
log "github.com/sirupsen/logrus"
)
type AutoGenerated struct {
Data struct {
GetNode struct {
GraphInfo struct {
Node struct {
Alias string `json:"alias"`
} `json:"node"`
} `json:"graph_info"`
} `json:"getNode"`
} `json:"data"`
}
var simple_query = `query Node($pubkey: String!) {
getNode(pubkey: $pubkey) {
graph_info {
node {
alias
}
}
}
}`
type Amboss_NodeInfoResponse struct {
Socials struct {
Info struct {
Email string `json:"email"`
Telegram string `json:"telegram"`
Twitter string `json:"twitter"`
LightningAddress string `json:"lightning_address"`
Website string `json:"website"`
Pubkey string `json:"pubkey"`
MinChannelSize interface{} `json:"minChannelSize"`
Message string `json:"message"`
TwitterVerified bool `json:"twitter_verified"`
Updated time.Time `json:"updated"`
} `json:"info"`
} `json:"socials"`
GraphInfo struct {
LastUpdate time.Time `json:"last_update"`
Metrics struct {
Capacity string `json:"capacity"`
CapacityRank int `json:"capacity_rank"`
Channels int `json:"channels"`
ChannelsRank int `json:"channels_rank"`
} `json:"metrics"`
Node struct {
Addresses []struct {
Addr string `json:"addr"`
IPInfo struct {
City string `json:"city"`
Country string `json:"country"`
CountryCode string `json:"country_code"`
} `json:"ip_info"`
Network string `json:"network"`
} `json:"addresses"`
LastUpdate int `json:"last_update"`
Color string `json:"color"`
Features []struct {
FeatureID string `json:"feature_id"`
IsKnown bool `json:"is_known"`
IsRequired bool `json:"is_required"`
Name string `json:"name"`
} `json:"features"`
} `json:"node"`
} `json:"graph_info"`
Amboss struct {
IsFavorite bool `json:"is_favorite"`
IsPrime bool `json:"is_prime"`
NumberFavorites int `json:"number_favorites"`
NewChannelGossipDelta struct {
Mean string `json:"mean"`
Sd string `json:"sd"`
} `json:"new_channel_gossip_delta"`
Notifications struct {
NumberSubscribers int `json:"number_subscribers"`
} `json:"notifications"`
} `json:"amboss"`
}
type Amboss_NodeInfoResponse_Nested struct {
Data struct {
GetNode struct {
Amboss_NodeInfoResponse
} `json:"getNode"`
} `json:"data"`
}
var amboss_graphql_query = `query Info($pubkey: String!) {
getNode(pubkey: $pubkey) {
socials {
info {
email
telegram
twitter
lightning_address
website
pubkey
minChannelSize
message
twitter_verified
updated
}
}
graph_info {
last_update
metrics {
capacity
capacity_rank
channels
channels_rank
}
node {
addresses {
addr
ip_info {
city
country
country_code
}
network
}
last_update
color
features {
feature_id
is_known
is_required
name
}
}
}
amboss {
is_favorite
is_prime
number_favorites
new_channel_gossip_delta {
mean
sd
}
notifications {
number_subscribers
}
}
}
}`
var amboss_graphql_variabnes = `{
"pubkey": "%s"
}
`
type AmbossClient struct {
}
func GetAmbossClient() AmbossClient {
return AmbossClient{}
}
func (c *AmbossClient) GetNodeInfo(pubkey string) (Amboss_NodeInfoResponse, error) {
url := "https://api.amboss.space/graphql"
log.Infof("Getting info from amboss.space for %s", pubkey)
graphqlClient := graphql.NewClient(url)
graphqlRequest := graphql.NewRequest(simple_query)
graphqlRequest.Var("pubkey", pubkey)
// set header fields
graphqlRequest.Header.Set("Cache-Control", "no-cache")
graphqlRequest.Header.Set("Content-Type", "application/json")
var r_nested *AutoGenerated
if err := graphqlClient.Run(context.Background(), graphqlRequest, &r_nested); err != nil {
log.Errorf("[amboss] api error: %v", err)
}
// r := r_nested.Data.GetNode.Amboss_NodeInfoResponse
return Amboss_NodeInfoResponse{}, nil
// jsonData := map[string]string{
// "query": query,
// "variables": variables,
// }
// client := http.Client{
// Timeout: time.Second * 2, // Timeout after 2 seconds
// }
// req, err := http.NewRequest(http.MethodGet, url, nil)
// if err != nil {
// log.Fatal(err)
// }
// res, getErr := client.Do(req)
// if getErr != nil {
// log.Fatal(getErr)
// }
// if res.Body != nil {
// defer res.Body.Close()
// }
// body, readErr := ioutil.ReadAll(res.Body)
// if readErr != nil {
// log.Fatal(readErr)
// }
// r := Amboss_NodeInfoResponse{}
// jsonErr := json.Unmarshal(body, &r)
// if jsonErr != nil {
// log.Errorf("API error: %v", jsonErr)
// }
// return r, nil
}

34
api/api.go Normal file
View File

@@ -0,0 +1,34 @@
package api
import log "github.com/sirupsen/logrus"
type ApiClient interface {
GetNodeInfo(pubkey string) OneML_NodeInfoResponse
}
type ApiNodeInfo struct {
OneMl OneML_NodeInfoResponse
Amboss Amboss_NodeInfoResponse
}
func GetApiNodeinfo(pubkey string) (ApiNodeInfo, error) {
// get info from 1ml
OnemlClient := GetOneMlClient()
onemlNodeInfo, err := OnemlClient.GetNodeInfo(pubkey)
if err != nil {
log.Errorf(err.Error())
onemlNodeInfo = OneML_NodeInfoResponse{}
}
// get info from amboss
ambossClient := GetAmbossClient()
ambossNodeInfo, err := ambossClient.GetNodeInfo(pubkey)
if err != nil {
log.Errorf(err.Error())
ambossNodeInfo = Amboss_NodeInfoResponse{}
}
return ApiNodeInfo{
OneMl: onemlNodeInfo,
Amboss: ambossNodeInfo,
}, err
}