threads

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2023 License: MIT Imports: 11 Imported by: 0

README

Dwarves Golang Threads API

Dwarves Foundation Dwarves Foundation Discord

Unofficial, Reverse-Engineered Golang client for Meta's Threads. Supports Read and Write.

Getting started

How to install Install the library with the following command using go module:

$ go get github.com/dwarvesf/go-threads

Examples Find examples of how to use the library in the examples folder:

ls examples
├── create_post
│   └── main.go
...

API

Disclaimer

The Threads API is a public API in the library, requiring no authorization. In contrast, the Instagram API, referred to as the private API, requires the Instagram username and password for interaction.

The public API offers read-only endpoints, while the private API provides both read and write endpoints. The private API is generally more stable as Instagram is a reliable product.

Using the public API reduces the risk of rate limits or account suspension. However, there is a trade-off between stability, bugs, rate limits, and suspension. The library allows for combining approaches, such as using the public API for read-only tasks and the private API for write operations. A retry mechanism can also be implemented, attempting the public API first and then falling back to the private API if necessary.

Initialization

To start using the GoThreads package, import the relevant class for communication with the Threads API and create an instance of the object.

For utilizing only the public API, use the following code snippet:

import (
    "github.com/dwarvesf/go-threads"
)

func main() {
    th := threads.NewThreads()
    th.GetThreadLikers(<thread_id>)

    // Using global instance
    threads.GetThreadLikers(<thread_id>)
}

If you intend to use the private API exclusively or both the private and public APIs, utilize the following code snippet:

package main

import (
	"fmt"

	"github.com/dwarvesf/go-threads"
)

func main() {
	cfg, err := threads.InitConfig(
		threads.WithDoLogin("instagram_username", "instagram_password"),
	)
	if err != nil {
		fmt.Println("unable init config", err)
		return
	}

	client, err := threads.NewPrivateAPIClient(cfg)
	if err != nil {
		fmt.Println("unable init API client", err)
		return
	}
}

Or the shorter syntax

package main

import (
	"fmt"

	"github.com/dwarvesf/go-threads"
	"github.com/dwarvesf/go-threads/model"
)

func main() {
	client, err := threads.InitAPIClient(
		threads.WithDoLogin("instagram_username", "instagram_password"),
	)
	if err != nil {
		fmt.Println("unable init API client", err)
		return
	}

	p, err := client.CreatePost(model.CreatePostRequest{Caption: "new post"})
	if err != nil {
		fmt.Println("unable create a post", err)
		return
	}
}

To mitigate the risk of blocking our users, an alternative initialization method can be implemented for the client. This method entails storing the API token and device token, which are subsequently utilized for initializing the API client.

package main

import (
	"fmt"

	"github.com/dwarvesf/go-threads"
	"github.com/dwarvesf/go-threads/model"
)

func main() {
	client, err := threads.InitAPIClient(
		threads.WithCridential("instagram_username", "instagram_password"),
		threads.WithAPIToken("device_id", "api_token"),
	)
	if err != nil {
		fmt.Println("unable init API client", err)
		return
	}

	p, err := client.CreatePost(model.CreatePostRequest{Caption: "new post"})
	if err != nil {
		fmt.Println("unable create a post", err)
		return
	}
}

Public API

Coming soon

Private API

Coming soon

Road map

  • Improve the perfomance
  • Listing API
  • Mutation API

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetThreadLikers added in v0.0.2

func GetThreadLikers(ID int) (*model.LikersResponse, error)

func GetUserByUsername

func GetUserByUsername(username string) (int, error)

GetUserByUsername get user by username via instagram api

Types

type Config added in v0.0.2

type Config struct {
	Username           string
	Password           string
	UserID             int
	APIToken           string
	TimezoneOffset     int
	DeviceID           string
	DeviceModel        string
	DeviceManufacturer string
	DeviceOsVersion    int
	DeviceOsRelease    string
}

Config config to init thread client

func InitConfig added in v0.0.2

func InitConfig(configs ...ConfigFn) (*Config, error)

InitConfig init configs

func (Config) ReadyCheck added in v0.0.2

func (c Config) ReadyCheck() error

type ConfigFn added in v0.0.2

type ConfigFn func(Config) (Config, error)

ConfigFn define the function to update config data

func WithAPIToken added in v0.0.2

func WithAPIToken(deviceID string, apitoken string) ConfigFn

WithAPIToken

func WithCridential added in v0.0.2

func WithCridential(username string, password string) ConfigFn

WithCridential update the user cridential

func WithDefaultValue added in v0.0.2

func WithDefaultValue() ConfigFn

WithDefaultValue initial default value

func WithDeviceID added in v0.0.2

func WithDeviceID(deviceID string) ConfigFn

WithDeviceID update the device ID

func WithDoLogin added in v0.0.2

func WithDoLogin(username string, password string) ConfigFn

WithDoLogin fetch user id from instagram

func WithUserID added in v0.0.2

func WithUserID(userID int) ConfigFn

WithUserID update the user ID

func WithUserIDFetching added in v0.0.2

func WithUserIDFetching(username string) ConfigFn

WithUserIDFetching fetch user id from instagram

type PrivateAPI added in v0.0.2

type PrivateAPI interface {
	CreatePost(content model.CreatePostRequest) (*model.CreatePostResponse, error)
	GetUserFollowers(id int) (*model.UserFollowersResponse, error)
	GetFollowers() (*model.UserFollowersResponse, error)
	GetFollowing() (*model.UserFollowingResponse, error)
	GetThreadByID(ID string) (*model.ThreadDetailResponse, error)
	SearchUser(query string) (*model.UserResponse, error)
	LikeThread(id string) (map[string]interface{}, error)
	UnLikeThread(id string) (map[string]interface{}, error)
	FollowUser(id int) (*model.FollowUserResponse, error)
	UnFollowUser(id int) (*model.FollowUserResponse, error)
}

PrivateAPI interface for private API

func InitAPIClient added in v0.0.2

func InitAPIClient(cfgFn ...ConfigFn) (PrivateAPI, error)

InitAPIClient new api client for private API

func NewPrivateAPIClient added in v0.0.2

func NewPrivateAPIClient(cfg *Config) (PrivateAPI, error)

NewPrivateAPIClient new api client for private API

type PublicAPI added in v0.0.2

type PublicAPI interface {
	GetThreadLikers(ID int) (*model.LikersResponse, error)
}

PublicAPI interface for Public API

func NewThreads added in v0.0.2

func NewThreads() PublicAPI

NewThreads return the public api

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL