Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kinabcd committed Apr 15, 2023
0 parents commit b1742d7
Show file tree
Hide file tree
Showing 16 changed files with 1,918 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Kin Lo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
goplurk
======
A golang wrapper of [Plurk API 2.0](https://www.plurk.com/API)

Getting started
----
With Go module support, simply add the following import
```
import "github.com/kinabcd/goplurk"
```

Basic usage
----
```golang
package main

import (
"github.com/kinabcd/goplurk"
)

var consumerToken = "..."
var consumerSecret = "..."
var accessToken = "..."
var accessSecret = "..."

func main() {
client, _ := goplurk.NewClient(consumerToken, consumerSecret, accessToken, accessSecret)
client.Timeline.PlurkAdd("says", "somecontent")
}

```

Advenced usage
----
see **example/** for more usages

Author
------

Kin Lo :: kinabcd@gmail.com :: @kinabcd
35 changes: 35 additions & 0 deletions api_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package goplurk

import (
"encoding/json"
"strconv"
)

type APIProfile struct {
client *Client
}

func (u *APIProfile) GetOwnProfile() (*Profile, error) {
resBytes, err := u.client.Engine.CallAPI("/APP/Profile/getOwnProfile", map[string]string{})
if err != nil {
return nil, err
}
var userDate = &Profile{}
if err := json.Unmarshal(resBytes, userDate); err != nil {
return nil, err
}
return userDate, nil
}
func (u *APIProfile) GetPublicProfile(userId int64) (*Profile, error) {
resBytes, err := u.client.Engine.CallAPI("/APP/Profile/getPublicProfile", map[string]string{
"user_id": strconv.FormatInt(userId, 10),
})
if err != nil {
return nil, err
}
var userDate = &Profile{}
if err := json.Unmarshal(resBytes, userDate); err != nil {
return nil, err
}
return userDate, nil
}
61 changes: 61 additions & 0 deletions api_responses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package goplurk

import (
"encoding/json"
"fmt"
"strconv"
)

type APIResponses struct {
client *Client
}

func (u *APIResponses) Get(plurkId int64, fromResoponse int64, count int64) (*Responses, error) {
var body = map[string]string{}
body["plurk_id"] = strconv.FormatInt(plurkId, 10)
body["from_response"] = strconv.FormatInt(fromResoponse, 10)
body["count"] = strconv.FormatInt(count, 10)
res, err := u.client.Engine.CallAPI("/APP/Responses/get", body)
if err != nil {
return nil, err
}
responses := Responses{}
if err := json.Unmarshal(res, &responses); err != nil {
return nil, fmt.Errorf("failed to unmarshal responses: %v, %s", err, string(res))
}

return &responses, nil
}
func (u *APIResponses) ResponseAdd(plurkId int64, qualifier string, content string) (*Response, error) {
if qualifier == "" {
qualifier = ":"
}
if content == "" {
return nil, fmt.Errorf("content can not be empty")
}
var body = map[string]string{}
body["plurk_id"] = strconv.FormatInt(plurkId, 10)
body["qualifier"] = qualifier
body["content"] = content
res, err := u.client.Engine.CallAPI("/APP/Responses/responseAdd", body)
if err != nil {
return nil, err
}
response := Response{}
if err := json.Unmarshal(res, &response); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %v, %s", err, string(res))
}
return &response, nil
}

func (u *APIResponses) ResponseDelete(responseId int64, plurkId int64) error {
_, err := u.client.Engine.CallAPI("/APP/Responses/responseDelete", map[string]string{
"response_id": strconv.FormatInt(responseId, 10),
"plurk_id": strconv.FormatInt(plurkId, 10),
})
if err != nil {
return fmt.Errorf("failed to delete response: %v", err)
}
return nil

}
108 changes: 108 additions & 0 deletions api_timeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package goplurk

import (
"encoding/json"
"fmt"
"strconv"
"strings"

"golang.org/x/exp/maps"
)

type APITimeline struct {
client *Client
}

func (u *APITimeline) GetPlurk(plurkId int64) (*Plurk, error) {
resBytes, err := u.client.Engine.CallAPI("/APP/Timeline/getPlurk", map[string]string{
"plurk_id": strconv.FormatInt(plurkId, 10),
})
if err != nil {
return nil, fmt.Errorf("failed to get plurk: %v", err)
}
plurk := struct {
Plurk Plurk `json:"plurk"`
}{}
if err := json.Unmarshal(resBytes, &plurk); err != nil {
return nil, fmt.Errorf("failed to unmarshal plurk: %v, %s", err, string(resBytes))
}
return &plurk.Plurk, nil
}

func (u *APITimeline) PlurkAdd(qualifier string, content string, optionSets ...Options) (*Plurk, error) {
if qualifier == "" {
qualifier = ":"
}
if content == "" {
return nil, fmt.Errorf("content can not be empty")
}
var body = map[string]string{}
body["qualifier"] = qualifier
body["content"] = content
for _, optionSet := range optionSets {
maps.Copy(body, optionSet.Get())
}
res, err := u.client.Engine.CallAPI("/APP/Timeline/plurkAdd", body)
if err != nil {
return nil, err
}
plurk := Plurk{}
if err := json.Unmarshal(res, &plurk); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %v, %s", err, string(res))
}
return &plurk, nil
}

func (u *APITimeline) PlurkDelete(plurkId int64) error {
_, err := u.client.Engine.CallAPI("/APP/Timeline/plurkDelete", map[string]string{
"plurk_id": strconv.FormatInt(plurkId, 10),
})
if err != nil {
return fmt.Errorf("failed to delete plurk: %v", err)
}
return nil
}

func (u *APITimeline) MutePlurks(plurkIds []int64) error {
return u.opPlurk("/APP/Timeline/mutePlurks", plurkIds)
}

func (u *APITimeline) UnmutePlurks(plurkIds []int64) error {
return u.opPlurk("/APP/Timeline/unmutePlurks", plurkIds)
}

func (u *APITimeline) FavoritePlurks(plurkIds []int64) error {
return u.opPlurk("/APP/Timeline/favoritePlurks", plurkIds)
}

func (u *APITimeline) UnfavoritePlurks(plurkIds []int64) error {
return u.opPlurk("/APP/Timeline/unfavoritePlurks", plurkIds)
}

func (u *APITimeline) Replurk(plurkIds []int64) error {
return u.opPlurk("/APP/Timeline/replurk", plurkIds)
}

func (u *APITimeline) Unreplurk(plurkIds []int64) error {
return u.opPlurk("/APP/Timeline/unreplurk", plurkIds)
}

func (u *APITimeline) MarkAsRead(plurkIds []int64) error {
return u.opPlurk("/APP/Timeline/markAsRead", plurkIds)
}

func (u *APITimeline) opPlurk(_url string, plurkIds []int64) error {
if len(plurkIds) != 0 {
plurkIdStrs := []string{}
for _, limited := range plurkIds {
plurkIdStrs = append(plurkIdStrs, strconv.FormatInt(limited, 10))
}
_, err := u.client.Engine.CallAPI(_url, map[string]string{
"ids": "[" + strings.Join(plurkIdStrs, ",") + "]",
})
return err
} else {
return fmt.Errorf("plurkIds can not be empty")
}

}
21 changes: 21 additions & 0 deletions api_users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package goplurk

import (
"encoding/json"
)

type APIUsers struct {
client *Client
}

func (u *APIUsers) Me() (*User, error) {
resBytes, err := u.client.Engine.CallAPI("/APP/Users/me", map[string]string{})
if err != nil {
return nil, err
}
var userDate = &User{}
if err := json.Unmarshal(resBytes, userDate); err != nil {
return nil, err
}
return userDate, nil
}
Loading

0 comments on commit b1742d7

Please sign in to comment.