Skip to content

Instantly share code, notes, and snippets.

@redrover9
Created April 4, 2022 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redrover9/ef653b6c279df0addba5971e56c8fc32 to your computer and use it in GitHub Desktop.
Save redrover9/ef653b6c279df0addba5971e56c8fc32 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"
"time"
)
func getUrl() struct {
URL string "json:\"url\""
} {
calResp, err := http.Get("https://sefaria.org/api/calendars")
if err != nil {
log.Fatalln(err)
}
calBody, err := ioutil.ReadAll(calResp.Body)
if err != nil {
log.Fatalln(err)
}
type All struct {
CalendarItems []struct {
URL string `json:"url"`
} `json:"calendar_items"`
}
var allCal All
err = json.Unmarshal(calBody, &allCal)
if err != nil {
log.Fatalln(err)
}
incompleteURL := allCal.CalendarItems[2]
return incompleteURL
}
func regexGarbage() string {
urlStruct := getUrl()
urlStr := fmt.Sprintf("%#v", urlStruct)
re, _ := regexp.Compile("\"[A-Z][a-z]*.[0-9]*\"")
urlOnly := re.FindString(urlStr)
urlOnlyMinusQuotes := urlOnly[1 : len(urlOnly)-1]
return urlOnlyMinusQuotes
}
func getDafYomi() []interface{} {
fullUrl := "https://sefaria.org/api/texts/" + regexGarbage()
dafYomi, err := http.Get(fullUrl)
if err != nil {
log.Fatalln(err)
}
dafYomiBody, err := ioutil.ReadAll(dafYomi.Body)
if err != nil {
log.Fatalln(err)
}
type DY struct {
DYText []interface{} `json:"text"`
}
var dy DY
err = json.Unmarshal(dafYomiBody, &dy)
if err != nil {
log.Fatalln(err)
}
dyText := dy.DYText
return dyText
}
func main() {
var dyTextStr string = fmt.Sprintf("%#v", getDafYomi())
var subscriptionKey string = "123"
var endpoint string = "https://123.cognitiveservices.azure.com"
const uriPath = "/text/analytics/v3.2-preview.1/analyze"
var uri = endpoint + uriPath
data := []map[string]string{
{"id": "1", "language": "en", "text": dyTextStr},
}
params := []map[string]string{
{"model-version": "latest", "sentenceCount": "3", "sortBy": "Offset"},
}
documents, err := json.Marshal(&data)
if err != nil {
fmt.Printf("Error marshaling data: %v\n", err)
return
}
tasks, err := json.Marshal(&params)
if err != nil {
fmt.Printf("Error marshaling data: %v\n", err)
return
}
r := strings.NewReader("[{\"analysisInput\":{\"documents\":{\"id\":\"1\",\"language\":\"en\",\"text\":" + string(documents) + "}}},{\"tasks\":{\"extractiveSummarizationTasks\":{\"parameters\":" + string(tasks) + "}}}]")
fmt.Println(r)
fmt.Println("-----------------------")
client := &http.Client{
Timeout: time.Second * 2,
}
req, err := http.NewRequest("POST", uri, r)
if err != nil {
fmt.Printf("Error creating request: %v\n", err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Ocp-Apim-Subscription-Key", subscriptionKey)
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error on request: %v\n", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response body: %v\n", err)
return
}
var f interface{}
json.Unmarshal(body, &f)
jsonFormatted, err := json.MarshalIndent(f, "", " ")
if err != nil {
fmt.Printf("Error producing JSON: %v\n", err)
return
}
fmt.Println(string(jsonFormatted))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment