Skip to content

Commit 05ae758

Browse files
committed
feat: implement trends
1 parent 44460b2 commit 05ae758

4 files changed

Lines changed: 98 additions & 40 deletions

File tree

bluesky/blueskyapi.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ type RelationshipsRes struct {
259259
Relationships []Relationships `json:"relationships"`
260260
}
261261

262+
type TrendingTopics struct {
263+
Topics []TrendingTopic `json:"topics"`
264+
Suggested []TrendingTopic `json:"suggested"`
265+
}
266+
267+
type TrendingTopic struct {
268+
Topic string `json:"topic"`
269+
Link string `json:"link"`
270+
}
271+
262272
var userCache = bridge.NewCache(5 * time.Minute) // Cache TTL of 5 minutes
263273

264274
func SendRequest(token *string, method string, url string, body io.Reader) (*http.Response, error) {
@@ -996,6 +1006,37 @@ func GetRecord(pds string, uri string) (*RecordResponse, error) {
9961006
return &record, nil
9971007
}
9981008

1009+
// This feature is still in beta, and is likely to break in the future
1010+
func GetTrends(pds string, token string) (*TrendingTopics, error) {
1011+
url := pds + "/xrpc/app.bsky.unspecced.getTrendingTopics"
1012+
1013+
resp, err := SendRequest(&token, http.MethodGet, url, nil)
1014+
if err != nil {
1015+
return nil, err
1016+
}
1017+
defer resp.Body.Close()
1018+
1019+
// // Print the response body
1020+
// bodyBytes, _ := io.ReadAll(resp.Body)
1021+
// bodyString := string(bodyBytes)
1022+
// fmt.Println("Response Body:", bodyString)
1023+
1024+
if resp.StatusCode != http.StatusOK {
1025+
bodyBytes, _ := io.ReadAll(resp.Body)
1026+
bodyString := string(bodyBytes)
1027+
fmt.Println("Response Status:", resp.StatusCode)
1028+
fmt.Println("Response Body:", bodyString)
1029+
return nil, errors.New("failed to fetch trends")
1030+
}
1031+
1032+
trends := TrendingTopics{}
1033+
if err := json.NewDecoder(resp.Body).Decode(&trends); err != nil {
1034+
return nil, err
1035+
}
1036+
1037+
return &trends, nil
1038+
}
1039+
9991040
// Gets the URI components
10001041
//
10011042
// @param uri: The URI to ge split

bridge/bridge.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,21 @@ type SourceTargetFriendship struct {
255255
Target UserFriendship `json:"target" xml:"target"`
256256
}
257257

258+
type Trends struct {
259+
Created time.Time `json:"created_at"` // EVERYWHERE except here it uses a different format for time. why.
260+
Trends []Trend `json:"trends"`
261+
AsOf time.Time `json:"as_of"`
262+
Locations []TrendLocation `json:"locations"` // no idea when i implenented thsi function, but i digress.
263+
}
264+
265+
type Trend struct {
266+
Name string `json:"name"`
267+
URL string `json:"url"`
268+
Promoted bool `json:"promoted"`
269+
Query string `json:"query"`
270+
TweetVolume int `json:"tweet_volume"`
271+
}
272+
258273
// Bluesky's API returns a letter ID for each user,
259274
// While twitter uses a numeric ID, meaning we
260275
// need to convert between the two

twitterv1/discover.go

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ package twitterv1
22

33
import (
44
"fmt"
5+
"net/url"
6+
"strings"
7+
"time"
58

9+
blueskyapi "github.com/Preloading/MastodonTwitterAPI/bluesky"
10+
"github.com/Preloading/MastodonTwitterAPI/bridge"
611
"github.com/gofiber/fiber/v2"
712
)
813

@@ -14,50 +19,47 @@ func Search(c *fiber.Ctx) error {
1419
}
1520

1621
// https://web.archive.org/web/20120313235613/https://dev.twitter.com/docs/api/1/get/trends/%3Awoeid
17-
// For now, we will be pretending WOEID doesn't exist
18-
// TODO: Implement this with data from bsky
22+
// The bluesky feature to make this possible was released 17 hours ago, and is "beta", so this is likely to break
1923
func trends_woeid(c *fiber.Ctx) error {
20-
type Trends struct {
21-
Created string `json:"created_at"`
22-
Trends []struct {
23-
Name string `json:"name"`
24-
URL string `json:"url"`
25-
Promoted bool `json:"promoted"`
26-
Query string `json:"query"`
27-
TweetVolume int `json:"tweet_volume"`
28-
} `json:"trends"`
29-
AsOf string `json:"as_of"`
30-
Locations []struct {
31-
Name string `json:"name"`
32-
WOEID int `json:"woeid"`
33-
} `json:"locations"`
24+
// We don't have location specific trends soooooo
25+
// woeid := c.Params("woeid")
26+
27+
//auth
28+
_, pds, _, oauthToken, err := GetAuthFromReq(c)
29+
if err != nil {
30+
return c.Status(fiber.StatusUnauthorized).SendString("OAuth token not found in Authorization header")
3431
}
3532

36-
return c.JSON(Trends{
37-
Created: "2021-09-01T00:00:00Z",
38-
Trends: []struct {
39-
Name string `json:"name"`
40-
URL string `json:"url"`
41-
Promoted bool `json:"promoted"`
42-
Query string `json:"query"`
43-
TweetVolume int `json:"tweet_volume"`
44-
}{
45-
{
46-
Name: "Trending Topic",
47-
URL: "https://twitter.com/search?q=%22Trending%20Topic%22",
48-
Promoted: false,
49-
Query: "%22Trending%20Topic%22",
50-
TweetVolume: 10000,
51-
},
52-
},
53-
AsOf: "2021-09-01T00:00:00Z",
54-
Locations: []struct {
55-
Name string `json:"name"`
56-
WOEID int `json:"woeid"`
57-
}{
33+
// Get trends
34+
bsky_trends, err := blueskyapi.GetTrends(*pds, *oauthToken)
35+
if err != nil {
36+
fmt.Println("Error:", err)
37+
return c.Status(fiber.StatusInternalServerError).SendString("Failed to fetch trends")
38+
}
39+
40+
trends := []bridge.Trend{}
41+
42+
for _, trend := range bsky_trends.Topics {
43+
topic_query := url.QueryEscape(trend.Topic)
44+
topic_query = strings.ReplaceAll(topic_query, "%20", "+")
45+
trends = append(trends, bridge.Trend{
46+
Name: trend.Topic,
47+
URL: "https://twitter.com/search?q=" + topic_query,
48+
Promoted: false,
49+
Query: topic_query,
50+
TweetVolume: 1337, // We can't get this data without search every, single, topic. So we just make it up.
51+
})
52+
53+
}
54+
55+
return c.JSON(bridge.Trends{
56+
Created: time.Now(),
57+
Trends: trends,
58+
AsOf: time.Now(), // no clue the differ
59+
Locations: []bridge.TrendLocation{
5860
{
5961
Name: "Worldwide",
60-
WOEID: 1,
62+
Woeid: 1, // Where on earth ID. Since bluesky trends are global, this is always 1
6163
},
6264
},
6365
})

twitterv1/twitterv1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func InitServer() {
5454
// Connect
5555
app.Get("/1/users/search.json", UserSearch)
5656

57-
// Trends
57+
// Discover
5858
app.Get("/1/trends/:woeid.json", trends_woeid)
5959

6060
// Setings

0 commit comments

Comments
 (0)