converter/app/backend/binary.go
2025-06-26 11:52:12 -04:00

126 lines
3.3 KiB
Go

package backend
import (
"binaryserver/app/db"
"binaryserver/app/utils"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/Davincible/goinsta"
)
func asciiToBinary(text string) string {
utils.Logger.Infof("Converting %v to binary", text)
var result []string
for _, char := range text {
binary := fmt.Sprintf("%08b", char) // 8-bit binary format
result = append(result, binary)
}
return strings.Join(result, " ")
}
func binaryToASCII(binary string) (string, error) {
utils.Logger.Infof("Converting %v to ASCII", binary)
// Ensure the length of the binary string is a multiple of 8
if len(binary)%8 != 0 {
return "", fmt.Errorf("binary string length must be a multiple of 8")
}
var sb strings.Builder
for i := 0; i < len(binary); i += 8 {
byteStr := binary[i : i+8]
b, err := strconv.ParseUint(byteStr, 2, 8)
if err != nil {
return "", fmt.Errorf("invalid binary sequence at position %d: %v", i, err)
}
sb.WriteByte(byte(b))
}
return sb.String(), nil
}
func instagramHandleExists(username string, bypass ...bool) string {
utils.Logger.Infof("Checking Insta account: %v", username)
records, _ := db.GetRecords(db.QueryOptions{Where: fmt.Sprintf(`handle='%v'`, username)})
if len(records) > 0 && len(bypass) == 0 {
utils.Logger.Infof("Using stored value")
return records[0].URL
}
url := fmt.Sprintf("https://www.instagram.com/%s/", username)
utils.Logger.Infof("Attempting to import goinsta.json file")
insta, err := goinsta.Import("goinsta.json")
if err != nil {
utils.Logger.Errorf("Could not log in: %v", err)
user, pass, err := utils.GetCreds()
if err != nil {
utils.Logger.Fatalf("Something went wrong: %v", err)
}
insta = goinsta.New(user, pass)
if err := insta.Login(); err != nil {
utils.Logger.Errorf("Could not login: %v", err)
return ""
}
}
defer insta.Export("goinsta.json")
record := db.NewRecord(username, url)
res, err := insta.Searchbar.SearchUser(username, true)
if err != nil {
utils.Logger.Infof("Storing empty record to DB: Account doesn't exist")
record.URL = ""
db.SaveRecord(record)
return ""
}
if len(res.Users) == 0 || res.Users[0].Username != username {
utils.Logger.Infof("Storing empty record to DB: Account not listed")
record.URL = ""
db.SaveRecord(record)
return ""
}
utils.Logger.Infof("Storing record %v to DB with url: %v", username, url)
db.SaveRecord(record)
return url
}
func BinaryHandler(w http.ResponseWriter, req *http.Request) {
binaryInput := strings.ReplaceAll(req.URL.Query().Get("binary"), " ", "")
var text string
var handle string
var err error
if binaryInput == "" {
asciiInput := req.URL.Query().Get("text")
if asciiInput == "" {
fmt.Fprintf(w, "Could not encode or decode!\n")
return
}
text = asciiToBinary(asciiInput)
fmt.Fprintf(w, "%v\n", GetFancyResponse(`<center><p>`+text+`</p></center>`, ""))
return
} else {
text, err = binaryToASCII(binaryInput)
text = strings.ReplaceAll(text, "@", "")
handle = text
}
if err != nil {
fmt.Fprintf(w, "Could not decode!\n")
return
}
var url string
if url = instagramHandleExists(text); url != "" {
text = `<center><a href="` + url + `">` + text + `</a></center>`
} else {
text = `<center><p>` + text + `</p></center>`
}
if url == "" {
handle = ""
}
fmt.Fprintf(w, "%v\n", GetFancyResponse(text, handle))
}