Skip to content

Commit

Permalink
twittermeow: copy x-go library into repo
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jul 19, 2024
1 parent e796813 commit 368933e
Show file tree
Hide file tree
Showing 39 changed files with 4,285 additions and 3 deletions.
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ module go.mau.fi/mautrix-twitter
go 1.22

require (
github.com/google/go-querystring v1.1.0
github.com/google/uuid v1.6.0
github.com/mattn/go-colorable v0.1.13
github.com/rs/zerolog v1.33.0
go.mau.fi/util v0.6.0
golang.org/x/net v0.27.0
maunium.net/go/mautrix v0.19.1-0.20240719130542-cc5f225bc61c
)

require (
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/rs/zerolog v1.33.0 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
Expand All @@ -22,7 +25,6 @@ require (
go.mau.fi/zeroconfig v0.1.3 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240707233637-46b078467d37 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
Expand Down Expand Up @@ -48,6 +55,7 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
Expand Down
32 changes: 32 additions & 0 deletions pkg/twittermeow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# x-go
A Go library for interacting with X's API

### Test steps

1. Create cookies.txt in this directory, grab your cookie string from x.com and paste it there
2. Run `go test client_test.go -v`

### Testing functionality

```go
_, _, err = cli.LoadMessagesPage()
if err != nil {
log.Fatal(err)
}
```
The `LoadMessagesPage` method makes a request to `https://x.com/messages` then makes 2 calls:
```go
data, err := c.GetAccountSettings(...)
initialInboxState, err := c.GetInitialInboxState(...)
```
it sets up the current "page" session for the client, fetches the current authenticated user info as well as the initial inbox state (the very starting inbox information you see when u load `/messages`) then returns the parsed data.

To easily test with the available functions I have made, lets say you wanna test uploading an image and sending it to the top conversation in your inbox you could simply do something like:
```go
initialInboxData, _, err := cli.LoadMessagesPage()
if err != nil {
log.Fatal(err)
}
uploadAndSendImageTest(initialInboxData)
```
Or feel free to try it out yourself! All the methods are available on the client instance.
57 changes: 57 additions & 0 deletions pkg/twittermeow/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package twittermeow

import (
"encoding/json"
"fmt"

"go.mau.fi/mautrix-twitter/pkg/twittermeow/data/endpoints"
"go.mau.fi/mautrix-twitter/pkg/twittermeow/data/payload"
"go.mau.fi/mautrix-twitter/pkg/twittermeow/data/response"
)

func (c *Client) Login() error {
err := c.session.LoadPage(endpoints.BASE_LOGIN_URL)
if err != nil {
return err
}
return nil
}

func (c *Client) GetAccountSettings(params payload.AccountSettingsQuery) (*response.AccountSettingsResponse, error) {
encodedQuery, err := params.Encode()
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s?%s", endpoints.ACCOUNT_SETTINGS_URL, string(encodedQuery))
apiRequestOpts := apiRequestOpts{
Url: url,
Method: "GET",
}
_, respBody, err := c.makeAPIRequest(apiRequestOpts)
if err != nil {
return nil, err
}

data := response.AccountSettingsResponse{}
return &data, json.Unmarshal(respBody, &data)
}

func (c *Client) GetDMPermissions(params payload.GetDMPermissionsQuery) (*response.GetDMPermissionsResponse, error) {
encodedQuery, err := params.Encode()
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s?%s", endpoints.DM_PERMISSIONS_URL, string(encodedQuery))
apiRequestOpts := apiRequestOpts{
Url: url,
Method: "GET",
WithClientUUID: true,
}
_, respBody, err := c.makeAPIRequest(apiRequestOpts)
if err != nil {
return nil, err
}

data := response.GetDMPermissionsResponse{}
return &data, json.Unmarshal(respBody, &data)
}
Loading

0 comments on commit 368933e

Please sign in to comment.