From 82a380de0ecac9bf35e047edc4066d918966ad9d Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 19 Apr 2023 21:12:45 +0200 Subject: [PATCH] Remove unused HN code --- .env.template | 2 -- hn.go | 86 --------------------------------------------------- main.go | 23 -------------- sn.go | 68 ---------------------------------------- 4 files changed, 179 deletions(-) diff --git a/.env.template b/.env.template index 4ed0ca1..fe54f4d 100644 --- a/.env.template +++ b/.env.template @@ -1,3 +1 @@ SN_AUTH_COOKIE= -SN_USERNAME=hn -HN_AUTH_COOKIE= diff --git a/hn.go b/hn.go index fda1104..f200536 100644 --- a/hn.go +++ b/hn.go @@ -3,16 +3,8 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" "log" "net/http" - "net/url" - "regexp" - "strconv" - "strings" - - "github.com/joho/godotenv" - "github.com/namsral/flag" ) type ItemID = int @@ -31,21 +23,11 @@ type Story struct { var ( HackerNewsUrl string HackerNewsFirebaseUrl string - HnAuthCookie string ) func init() { HackerNewsUrl = "https://news.ycombinator.com" HackerNewsFirebaseUrl = "https://hacker-news.firebaseio.com/v0" - err := godotenv.Load() - if err != nil { - log.Fatal("Error loading .env file") - } - flag.StringVar(&HnAuthCookie, "HN_AUTH_COOKIE", "", "Cookie required for authorizing requests to news.ycombinator.com") - flag.Parse() - if HnAuthCookie == "" { - log.Fatal("HN_AUTH_COOKIE not set") - } } func FetchHackerNewsTopStories() []Story { @@ -97,61 +79,6 @@ func FetchStoryById(id ItemID) Story { return story } -func FetchHackerNewsItemHMAC(id ItemID) string { - hnUrl := fmt.Sprintf("%s/item?id=%d", HackerNewsUrl, id) - req, err := http.NewRequest("GET", hnUrl, nil) - if err != nil { - panic(err) - } - // Cookie header must be set to fetch the correct HMAC for posting comments - req.Header.Set("Cookie", HnAuthCookie) - client := http.DefaultClient - resp, err := client.Do(req) - if err != nil { - panic(err) - } - log.Printf("GET %s %d\n", hnUrl, resp.StatusCode) - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatal("Failed to read response body:", err) - } - - // Find HMAC in body - re := regexp.MustCompile(`name="hmac" value="([a-z0-9]+)"`) - match := re.FindStringSubmatch(string(body)) - if len(match) == 0 { - log.Fatal("No HMAC found") - } - hmac := match[1] - - return hmac -} - -func CommentHackerNewsStory(text string, id ItemID) { - hmac := FetchHackerNewsItemHMAC(id) - - hnUrl := fmt.Sprintf("%s/comment", HackerNewsUrl) - data := url.Values{} - data.Set("parent", strconv.Itoa(id)) - data.Set("goto", fmt.Sprintf("item?id=%d", id)) - data.Set("text", text) - data.Set("hmac", hmac) - req, err := http.NewRequest("POST", hnUrl, strings.NewReader(data.Encode())) - if err != nil { - panic(err) - } - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - req.Header.Set("Cookie", HnAuthCookie) - client := http.DefaultClient - resp, err := client.Do(req) - if err != nil { - panic(err) - } - defer resp.Body.Close() - log.Printf("POST %s %d\n", hnUrl, resp.StatusCode) -} - func HackerNewsUserLink(user string) string { return fmt.Sprintf("%s/user?id=%s", HackerNewsUrl, user) } @@ -159,16 +86,3 @@ func HackerNewsUserLink(user string) string { func HackerNewsItemLink(id int) string { return fmt.Sprintf("%s/item?id=%d", HackerNewsUrl, id) } - -func FindHackerNewsItemId(text string) int { - re := regexp.MustCompile(fmt.Sprintf(`\[HN\]\(%s/item\?id=([0-9]+)\)`, HackerNewsUrl)) - match := re.FindStringSubmatch(text) - if len(match) == 0 { - log.Fatal("No Hacker News item URL found") - } - id, err := strconv.Atoi(match[1]) - if err != nil { - panic(err) - } - return id -} diff --git a/main.go b/main.go index 3ea1d09..27fa5c6 100644 --- a/main.go +++ b/main.go @@ -1,28 +1,5 @@ package main -import ( - "log" - - "github.com/joho/godotenv" - "github.com/namsral/flag" -) - -var ( - SnUserName string -) - -func init() { - err := godotenv.Load() - if err != nil { - log.Fatal("Error loading .env file") - } - flag.StringVar(&SnUserName, "SN_USERNAME", "", "Username of bot on SN") - flag.Parse() - if SnUserName == "" { - log.Fatal("SN_USERNAME not set") - } -} - func main() { stories := FetchHackerNewsTopStories() filtered := CurateContentForStackerNews(&stories) diff --git a/sn.go b/sn.go index fe03886..a77f1f6 100644 --- a/sn.go +++ b/sn.go @@ -203,71 +203,3 @@ func CommentStackerNewsPost(text string, parentId int) { log.Println("Commented post on SN") log.Printf("text='%s' parentId=%d\n", text, parentId) } - -func FetchStackerNewsUserItems(user string) *[]Item { - query := ` - query items($name: String!, $cursor: String) { - items(name: $name, sort: "user", cursor: $cursor) { - items { - id - title - url - sats - createdAt - comments { - id - text - user { - name - } - comments { - id - text - user { - name - } - } - } - ncomments - } - cursor - } - } - ` - var items []Item - var cursor string - for { - body := GraphQLPayload{ - Query: query, - Variables: map[string]interface{}{ - "name": user, - "cursor": cursor, - }, - } - resp := MakeStackerNewsRequest(body) - defer resp.Body.Close() - - var itemsResp ItemsResponse - err := json.NewDecoder(resp.Body).Decode(&itemsResp) - if err != nil { - log.Fatal("Error decoding items JSON:", err) - } - fetchedItems := itemsResp.Data.Items.Items - - for _, item := range fetchedItems { - items = append(items, item) - } - if len(fetchedItems) < 21 { - break - } - cursor = itemsResp.Data.Items.Cursor - } - - log.Printf("Fetched %d items\n", len(items)) - - return &items -} - -func StackerNewsItemLink(id int) string { - return fmt.Sprintf("%s/items/%d", StackerNewsUrl, id) -}