commit de75e0752175c4626724daa83c8018768c95647a
parent e31b19fd44e7fc0f51968ddfc0ae87e8afcbb707
Author: danyisill <danyisill@users.noreply.github.com>
Date: Tue, 4 Aug 2020 19:41:12 +0300
Rewrite in Go
Diffstat:
A | his.go | | | 60 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | his.sh | | | 31 | ++++--------------------------- |
2 files changed, 64 insertions(+), 27 deletions(-)
diff --git a/his.go b/his.go
@@ -0,0 +1,59 @@
+package main
+import ("fmt"
+ "net/http"
+ "encoding/json"
+ "sync"
+ "io/ioutil"
+ "strconv"
+ "strings"
+ "net/url"
+ "html"
+)
+var wg sync.WaitGroup
+type Page struct{
+ Page int `json:"page"`
+ Thread []Thread `json:"threads"`
+}
+type Thread struct{
+ No int `json:"no"`
+ Replies int `json:"replies"`
+ Tim int `json:"tim"`
+ Sub string `json:"sub"`
+ Com string `json:"com"`
+ Ext string `json:"ext"`
+}
+func procThread(th Thread, b string){
+ defer wg.Done()
+ params := url.Values{}
+ params.Add("chat_id", fmt.Sprintf("@%s50replies", b))
+ params.Add("photo", fmt.Sprintf("https://is2.4chan.org/%s/%d%s", b, th.Tim, th.Ext))
+ comment := strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(html.UnescapeString(th.Com), "<span class=\"quote\">", ""), "</span>", "\n"), "<br>", "\n"), "class=\"quotelink\"", "")
+ params.Add("caption", fmt.Sprintf("https://boards.4channel.org/%s/thread/%d\n%s\n%s", b, th.No, th.Sub, comment))
+ params.Add("parse_mode", "HTML")
+
+ http.Get(fmt.Sprintf("https://api.telegram.org/bot%s/sendPhoto?%s", "1364237567:AAGjywcrfrePt3Of_fGnaXyei4JL7KVa4ZE", params.Encode()))
+}
+func board(b string){
+ res, _ := http.Get(fmt.Sprintf("https://a.4cdn.org/%s/catalog.json", b))
+ postedBytes, _ := ioutil.ReadFile("posted." + b)
+ posted := string(postedBytes)
+ dec := json.NewDecoder(res.Body)
+ dec.Token()
+ for dec.More(){
+ var p Page
+ dec.Decode(&p)
+ for _, th := range p.Thread{
+ if th.Replies > 50 && !strings.Contains(posted, strconv.Itoa(th.No)){
+ posted += strconv.Itoa(th.No) + "\n"
+ wg.Add(1)
+ go procThread(th, b)
+ }
+ }
+ }
+ ioutil.WriteFile("posted." + b, []byte(posted), 0777)
+}
+func main(){
+ board("his")
+ board("lit")
+ wg.Wait()
+}+
\ No newline at end of file
diff --git a/his.sh b/his.sh
@@ -1,34 +1,11 @@
#!/usr/bin/bash
-[ -z "$HIS_BOT_TOKEN" ] && { echo "No API token in ENV!"; exit 1; }
-
-update () {
- [ ! -f ./"$1.posted" ] && touch "$1.posted"
- curl -s "https://a.4cdn.org/$1/threads.json" \
- | tr n '\n' \
- | tr -cd '1234567890:\n' \
- | sed 1d \
- | while read -r thread; do
- if [[ "$(cut -d : -f 4 <<< "$thread")" -gt 50 ]]; then
- echo "--- Checking if thread already exists... ---"
- if grep -q "$(cut -d : -f 2 <<< "$thread")" "$1.posted"; then
- echo " -> Exists, not posting."
- continue
- else
- echo " -> Posting new message!"
- cut -d : -f 2 <<< "$thread" \
- | tee -a "$1.posted" \
- | xargs -I {} curl -s "https://api.telegram.org/bot$HIS_BOT_TOKEN/sendMessage?chat_id=@$2&text=https://boards.4channel.org/$1/thread/{}" >/dev/null
- fi
- sleep 0.25
- fi
- done
-}
+touch posted.his
+touch posted.lit
+go build his.go
for (( ; ; )); do
- echo "=== Checking. ==="
- update his his50replies
- update lit lit50replies
+ ./his
sleep 300 # 5 min.
done