REFACTOR: Get rid of SortableXYZ types, use slices.SortFunc instead

This commit is contained in:
Alessio 2024-12-23 12:51:07 -08:00
parent 81e6dc50be
commit 14024f550d
2 changed files with 10 additions and 19 deletions

View File

@ -8,8 +8,8 @@ import (
"net/url" "net/url"
"path" "path"
"regexp" "regexp"
"sort"
"strconv" "strconv"
"slices"
"strings" "strings"
"time" "time"
) )
@ -38,21 +38,16 @@ func ParseAPIMedia(apiMedia APIMedia) Image {
} }
} }
type SortableVariants []struct { type Variant struct {
Bitrate int `json:"bitrate,omitempty"` Bitrate int `json:"bitrate,omitempty"`
URL string `json:"url"` URL string `json:"url"`
} }
func (v SortableVariants) Len() int { return len(v) }
func (v SortableVariants) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
func (v SortableVariants) Less(i, j int) bool { return v[i].Bitrate > v[j].Bitrate }
type APIExtendedMedia struct { type APIExtendedMedia struct {
ID int64 `json:"id_str,string"` ID int64 `json:"id_str,string"`
MediaURLHttps string `json:"media_url_https"` MediaURLHttps string `json:"media_url_https"`
Type string `json:"type"` Type string `json:"type"`
VideoInfo struct { VideoInfo struct {
Variants SortableVariants `json:"variants"` Variants []Variant `json:"variants"`
Duration int `json:"duration_millis"` Duration int `json:"duration_millis"`
} `json:"video_info"` } `json:"video_info"`
ExtMediaAvailability struct { ExtMediaAvailability struct {
@ -201,7 +196,7 @@ func parse_num_choices(card_name string) int {
func ParseAPIVideo(apiVideo APIExtendedMedia) Video { func ParseAPIVideo(apiVideo APIExtendedMedia) Video {
variants := apiVideo.VideoInfo.Variants variants := apiVideo.VideoInfo.Variants
sort.Sort(variants) slices.SortFunc(variants, func(a, b Variant) int { return b.Bitrate - a.Bitrate })
video_remote_url := variants[0].URL video_remote_url := variants[0].URL
var view_count int var view_count int
@ -769,11 +764,7 @@ type APIv1Entry struct {
} `json:"content"` } `json:"content"`
} }
type SortableEntries []APIv1Entry func entry_sorting_cmp(a, b APIv1Entry) int { return int(b.SortIndex - a.SortIndex) }
func (e SortableEntries) Len() int { return len(e) }
func (e SortableEntries) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
func (e SortableEntries) Less(i, j int) bool { return e[i].SortIndex > e[j].SortIndex }
type APIv1Response struct { type APIv1Response struct {
GlobalObjects struct { GlobalObjects struct {
@ -784,7 +775,7 @@ type APIv1Response struct {
Timeline struct { Timeline struct {
Instructions []struct { Instructions []struct {
AddEntries struct { AddEntries struct {
Entries SortableEntries `json:"entries"` Entries []APIv1Entry `json:"entries"`
} `json:"addEntries"` } `json:"addEntries"`
ReplaceEntry struct { ReplaceEntry struct {
Entry APIv1Entry Entry APIv1Entry
@ -849,7 +840,7 @@ func (t *APIv1Response) HandleTombstones() []UserHandle {
// Handle tombstones in the conversation flow // Handle tombstones in the conversation flow
entries := t.Timeline.Instructions[0].AddEntries.Entries entries := t.Timeline.Instructions[0].AddEntries.Entries
sort.Sort(entries) slices.SortFunc(entries, entry_sorting_cmp)
for i, entry := range entries { for i, entry := range entries {
if entry.Content.Item.Content.Tombstone.TombstoneInfo.RichText.Text != "" { if entry.Content.Item.Content.Tombstone.TombstoneInfo.RichText.Text != "" {
// Try to reconstruct the tombstone tweet // Try to reconstruct the tombstone tweet

View File

@ -5,9 +5,9 @@ import (
"fmt" "fmt"
"net/url" "net/url"
"regexp" "regexp"
"sort"
"strings" "strings"
"time" "time"
"slices"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -137,7 +137,7 @@ func (t *APIv1Response) ToTweetTroveAsNotifications(current_user_id UserID) (Twe
// Find the "addEntries" instruction // Find the "addEntries" instruction
for _, instr := range t.Timeline.Instructions { for _, instr := range t.Timeline.Instructions {
sort.Sort(instr.AddEntries.Entries) slices.SortFunc(instr.AddEntries.Entries, entry_sorting_cmp)
for _, entry := range instr.AddEntries.Entries { for _, entry := range instr.AddEntries.Entries {
id_re := regexp.MustCompile(`notification-([\w-]+)`) id_re := regexp.MustCompile(`notification-([\w-]+)`)
matches := id_re.FindStringSubmatch(entry.EntryID) matches := id_re.FindStringSubmatch(entry.EntryID)
@ -276,7 +276,7 @@ func (t *APIv1Response) ToTweetTroveAsNotificationDetail() (TweetTrove, []TweetI
// Find the "addEntries" instruction // Find the "addEntries" instruction
for _, instr := range t.Timeline.Instructions { for _, instr := range t.Timeline.Instructions {
sort.Sort(instr.AddEntries.Entries) slices.SortFunc(instr.AddEntries.Entries, entry_sorting_cmp)
for _, entry := range instr.AddEntries.Entries { for _, entry := range instr.AddEntries.Entries {
if entry.Content.Item.Content.Tweet.ID != 0 { if entry.Content.Item.Content.Tweet.ID != 0 {
ids = append(ids, TweetID(entry.Content.Item.Content.Tweet.ID)) ids = append(ids, TweetID(entry.Content.Item.Content.Tweet.ID))