Created
September 20, 2019 18:41
-
-
Save haze/c82192661dcef1459b6f05e2d9ce710f to your computer and use it in GitHub Desktop.
shitpost.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
"strings" | |
"sync" | |
) | |
const ( | |
MaxEmojis = 3 | |
MinRelevance = 0.01 | |
) | |
type Suggestion struct { | |
Score float64 | |
Text string | |
} | |
type dangoResponse struct { | |
Suggestions []Suggestion `json:"results"` | |
} | |
func PredictEmojisForText(client *http.Client, text string) ([]Suggestion, error) { | |
empty := make([]Suggestion, 0) | |
query := url.QueryEscape(text) | |
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.getdango.com/api/emoji?q=%s", query), nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
req.Header.Set("Accept", "application/json, text/javascript, */*; q=0.01") | |
req.Header.Set("Referer", "https://getdango.com/interact/") | |
req.Header.Set("Origin", "https://getdango.com") | |
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36") | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Fatal(err) | |
} | |
var response dangoResponse | |
body, err := ioutil.ReadAll(resp.Body) | |
defer resp.Body.Close() | |
if err != nil { | |
return empty, nil | |
} | |
err = json.Unmarshal(body, &response) | |
if err != nil { | |
return empty, nil | |
} | |
return response.Suggestions, nil | |
} | |
func main() { | |
input, err := ioutil.ReadAll(os.Stdin) | |
if err != nil { | |
panic(err) | |
} | |
wordsPerSection := 3 | |
sInput := strings.ReplaceAll(string(input), "\n", " \n ") | |
words := strings.Split(strings.TrimSpace(sInput), " ") | |
// chunking | |
chunks := make([]string, 0) | |
buf := make([]string, wordsPerSection) | |
n := 0 | |
for _, word := range words { | |
if n == wordsPerSection { | |
joined := strings.Join(buf, " ") | |
if strings.TrimSpace(joined) != "" { | |
chunks = append(chunks, strings.Join(buf, " ")) | |
} | |
buf = make([]string, wordsPerSection) | |
n = 0 | |
} | |
buf[n] = word | |
n += 1 | |
} | |
if len(buf) != 0 { | |
chunks = append(chunks, strings.Join(buf, " ")) | |
} | |
var wg sync.WaitGroup | |
wg.Add(len(chunks)) | |
m := sync.Map{} | |
client := http.Client{} | |
sem := make(chan struct{}, 32) | |
for ci, chunk := range chunks { | |
select { | |
case sem <- struct{}{}: | |
go compute(ci, chunk, sem, &client, &m, &wg) | |
default: | |
compute(ci, chunk, nil, &client, &m, &wg) | |
} | |
} | |
wg.Wait() | |
for n := 0; n < len(chunks); n += 1 { | |
text, _ := m.Load(n) | |
fmt.Print(text) | |
} | |
fmt.Println() | |
} | |
func compute(n int, text string, sem chan struct{}, client *http.Client, m *sync.Map, wg *sync.WaitGroup) { | |
suggestions, err := PredictEmojisForText(client, text) | |
if err == nil { | |
var builder strings.Builder | |
builder.WriteString(text) | |
builder.WriteRune(' ') | |
for sIdx, suggestion := range suggestions { | |
if sIdx >= MaxEmojis { | |
break | |
} | |
if suggestion.Score >= MinRelevance { | |
builder.WriteString(suggestion.Text) | |
builder.WriteRune(' ') | |
} | |
} | |
m.Store(n, builder.String()) | |
} else { | |
m.Store(n, text) | |
} | |
if sem != nil { | |
<-sem | |
} | |
wg.Done() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment