gotumblr

package module
v0.0.0-...-93ca5e5 Latest Latest
Warning

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

Go to latest
Published: May 5, 2017 License: Apache-2.0 Imports: 8 Imported by: 8

README

gotumblr

Description

A Go Tumblr API v2 Client.

GoDoc

Install gotumblr

In terminal write go get github.com/MariaTerzieva/gotumblr

Running the tests

Run the tests with go test to check if everything is ok.

Using the package

To use this package in your projects do this (after install):

import "github.com/MariaTerzieva/gotumblr"

You are going to need a consumer key, consumer secret, callback URL, token and token secret. You can get the consumer key, consumer secret and callback URL by registering a Tumblr application. You can do so by clicking here. The token and token secret you can get by using OAUTH. If you want to use this package just for your own tumblr account, after registering the application, click on the Explore API option and allow it access to your Tumblr account. You are going to see a tab "Show keys". Click on it and you will get your token and token secret but if you want, you can also obtain them using OAUTH.

Examples

First import the package in your project as shown above.

Then create NewTumblrRestClient with your credentials(consumer key, consumer secret, token, token secret and callback url):

	client := gotumblr.NewTumblrRestClient("consumer_key", "consumer_secret", "token", "token_secret", "callback_url", "http://api.tumblr.com")

Then use the client you just created to get the information you need. Here are some examples with what I got for my account:

	info := client.Info()
	fmt.Println(info.User.Name)
	//Output:
	//mgterzieva

	likes := client.Likes(map[string]string{})
	fmt.Println(likes.Liked_count)
	//Output:
	//63

	following := client.Following(map[string]string{})
	fmt.Println(following.Total_blogs)
	//Output:
	//1

	dashboard := client.Dashboard(map[string]string{"limit": "1"})
	if len(dashboard.Posts) != 0 {
		var base_dashboard_post gotumblr.BasePost
		for i, _ := range dashboard.Posts {
			json.Unmarshal(dashboard.Posts[i], &base_dashboard_post)
			fmt.Println(base_dashboard_post.State)
			//Output:
			//published
		}
	}

	tagged := client.Tagged("golang", map[string]string{"limit": "1"})
	if len(tagged) != 0 {
		var base_tagged_post gotumblr.BasePost
		for i, _ := range tagged {
			json.Unmarshal(tagged[i], &base_tagged_post)
			fmt.Println(base_tagged_post.State)
			//Output:
			//published
		}
	}

	blogname := "mgterzieva.tumblr.com" //this is my blogname. Change this according to your usecase and credentials.
	blogInfo := client.BlogInfo(blogname)
	fmt.Println(blogInfo.Blog.Title)
	//Output:
	//Maria's blog

	followers := client.Followers(blogname, map[string]string{})
	fmt.Println(followers.Total_users)
	//Output:
	//0

	blog_likes := client.BlogLikes(blogname, map[string]string{})
	fmt.Println(blog_likes.Liked_count)
	//Output:
	//63

	queue := client.Queue(blogname, map[string]string{})
	fmt.Println(len(queue.Posts))
	//Output:
	//0

	drafts := client.Drafts(blogname, map[string]string{})
	fmt.Println(len(drafts.Posts))
	//Output:
	//6

	submission := client.Submission(blogname, map[string]string{})
	fmt.Println(len(submission.Posts))
	//Output:
	//0

	avatar := client.Avatar(blogname, 64)
	fmt.Println(avatar.Avatar_url)
	//Output:
	//http://25.media.tumblr.com/avatar_49f49d0b9209_64.png

	other_blogname := "http://thehungergamesmovie.tumblr.com"
	follow := client.Follow(other_blogname)
	fmt.Println(follow)
	//Output:
	//<nil>

	unfollow := client.Unfollow(other_blogname)
	fmt.Println(unfollow)
	//Output:
	//<nil>

	id := "72078164824" //this is the id of a post of mine. Change this according to your usecase.
	//There is an Id field in all of the post object types in this library.
	reblogKey := "6l3e2pGL" //this is the reblogKey of a post of mine. Change this according to your usecase.
	//There is a Reblog_key field in all of the post object types in this library.
	like := client.Like(id, reblogKey)
	fmt.Println(like)
	//Output:
	//<nil>

	unlike := client.Unlike(id, reblogKey)
	fmt.Println(unlike)
	//Output:
	//<nil>

	reblog := client.Reblog(blogname, map[string]string{"id": id, "reblog_key": reblogKey})
	fmt.Println(reblog)
	//Output:
	//<nil>

	state := "draft"
	textPost := client.CreateText(blogname, map[string]string{"body": "Hello happy world!", "state": state})
	fmt.Println(textPost)
	//Output:
	//<nil>

	quote := "A happy heart makes the face cheerful."
	source := "Proverbs 15:13"
	quotePost := client.CreateQuote(blogname, map[string]string{"quote": quote, "source": source, "state": state})
	fmt.Println(quotePost)
	//Output:
	//<nil>

	title := "Follow me on tumblr, guys! :)"
	url := "http://mgterzieva.tumblr.com"
	linkPost := client.CreateLink(blogname, map[string]string{"url": url, "title": title, "state": state})
	fmt.Println(linkPost)
	//Output:
	//<nil>

	conversation := "John Doe: Hi there!\nJane Doe: Hi, John!\nJane Doe: ♥♥♥"
	//separate the tags with commas and don't leave whitespaces around the commas
	tags := "Saint Valentine's day,14th of February,lots of love,xoxo"
	chatPost := client.CreateChatPost(blogname, map[string]string{"conversation": conversation, "tags": tags, "state": state})
	fmt.Println(chatPost)
	//Output:
	//<nil>

	text := "Hello happy world!" //if you are editing a text post
	editPost := client.EditPost(blogname, map[string]string{"id": id, "body": text})
	fmt.Println(editPost)
	//Output:
	//<nil>

	deletePost := client.DeletePost(blogname, id)
	fmt.Println(deletePost)
	//Output:
	//<nil>

	code := `<iframe width="560" height="315" src="//www.youtube.com/embed/uJNvZRAmeqY" frameborder="0" allowfullscreen></iframe>`
	caption := "<b>Mother knows best</b>"
	embedVideo := client.CreateVideo(blogname, map[string]string{"embed": code, "state": state, "caption": caption})
	fmt.Println(embedVideo)
	//Output:
	//<nil>

	song := "https://soundcloud.com/tiffany-alvord-song/the-one-that-got-away-cover-by"
	songPostByURL := client.CreateAudio(blogname, map[string]string{"external_url": song, "state": state})
	fmt.Println(songPostByURL)
	//Output:
	//<nil>

	picture := "http://thumbs.dreamstime.com/z/cute-panda-17976617.jpg"
	photoPostByURL := client.CreatePhoto(blogname, map[string]string{"source": picture, "state": state})
	fmt.Println(photoPostByURL)
	//Output:
	//<nil>

Further information

If you still don't get how to work with this package don't worry! :) Read the project documentation or the Tumblr API or write an e-mail at mgterzieva@abv.bg if these don't help. :)

Contribution

In case you find any issues with this code, use the project's Issues page to report them or send pull requests.

License

Copyright 2014 Maria Terzieva

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AltSize

type AltSize struct {
	Width, Height int64
	Url           string
}

type AnswerPost

type AnswerPost struct {
	BasePost
	Asking_name string
	Asking_url  string
	Question    string
	Answer      string
}

type AudioPost

type AudioPost struct {
	BasePost
	Caption      string
	Player       string
	Plays        int64
	Album_art    string
	Artist       string
	Album        string
	Track_name   string
	Track_number int64
	Year         int64
}

type AvatarResponse

type AvatarResponse struct {
	Avatar_url string
}

type BasePost

type BasePost struct {
	Blog_name    string
	Id           int64
	Post_url     string
	PostType     string `json:"type"`
	Timestamp    int64
	Date         string
	Format       string
	Reblog_key   string
	Tags         []string
	Bookmarklet  bool
	Mobile       bool
	Source_url   string
	Source_title string
	Liked        bool
	State        string
	Total_Posts  int64
	Note_count   int64
	Notes        []Note
}

type BlogInfo

type BlogInfo struct {
	Title       string
	Posts       int64
	Name        string
	Url         string
	Updated     int64
	Description string
	Ask         bool
	Ask_anon    bool
	Likes       int64
}

type BlogInfoResponse

type BlogInfoResponse struct {
	Blog BlogInfo
}

type ChatPost

type ChatPost struct {
	BasePost
	Title, Body string
	Dialogue    []DialogueInfo
}

type CompleteResponse

type CompleteResponse struct {
	Meta     MetaInfo
	Response json.RawMessage
}

type DialogueInfo

type DialogueInfo struct {
	Name, Label, Phrase string
}

type DraftsResponse

type DraftsResponse struct {
	Posts []json.RawMessage
}

type FollowedBlog

type FollowedBlog struct {
	Name        string
	Url         string
	Updated     int64
	Title       string
	Description string
}

type FollowersResponse

type FollowersResponse struct {
	Total_users int64
	Users       []User
}

type FollowingResponse

type FollowingResponse struct {
	Total_blogs int64
	Blogs       []FollowedBlog
}

type LikesResponse

type LikesResponse struct {
	Liked_posts []json.RawMessage
	Liked_count int64
}

type LinkPost

type LinkPost struct {
	BasePost
	Title, Url, Description string
}

type MetaInfo

type MetaInfo struct {
	Status int64
	Msg    string
}

type Note

type Note struct {
	Type                    string
	Timestamp               int64
	Blog_name               string
	Blog_uuid               string
	Blog_url                string
	Followed                bool
	Avatar_shape            string
	Post_id                 string
	Reblog_parent_blog_name string
}

type OwnedBlog

type OwnedBlog struct {
	Name      string
	Url       string
	Title     string
	Primary   bool
	Followers int64
	Tweet     string
	Facebook  string
	Type      string
}

type PhotoObject

type PhotoObject struct {
	Caption   string
	Alt_sizes []AltSize
}

type PhotoPost

type PhotoPost struct {
	BasePost
	Photos        []PhotoObject
	Caption       string
	Width, Height int64
}

type PlayerInfo

type PlayerInfo struct {
	Width      int64
	Embed_code string
}

type PostsResponse

type PostsResponse struct {
	Blog        BlogInfo
	Posts       []json.RawMessage
	Total_posts int64
}

type QuotePost

type QuotePost struct {
	BasePost
	Text, Source string
}

type TextPost

type TextPost struct {
	BasePost
	Title, Body string
}

type TumblrRequest

type TumblrRequest struct {
	// contains filtered or unexported fields
}

Make queries to the Tumblr API through TumblrRequest.

func NewTumblrRequest

func NewTumblrRequest(consumerKey, consumerSecret, oauthToken, oauthSecret, callbackUrl, host string) *TumblrRequest

Initializes the TumblrRequest. consumerKey is the consumer key of your Tumblr Application. consumerSecret is the consumer secret of your Tumblr Application. callbackUrl is the callback URL of your Tumblr Application. oauthToken is the user specific token, received from the /access_token endpoint. oauthSecret is the user specific secret, received from the /access_token endpoint. host is the host that you are tryng to send information to (e.g. http://api.tumblr.com).

func (*TumblrRequest) Get

func (tr *TumblrRequest) Get(requestUrl string, params map[string]string) CompleteResponse

Make a GET request to the API with properly formatted parameters. requestUrl: the url you are making the request to. params: the parameters needed for the request.

func (*TumblrRequest) JSONParse

func (tr *TumblrRequest) JSONParse(content []byte) CompleteResponse

Parse JSON response. content: the content returned from the web request to be parsed as JSON.

func (*TumblrRequest) Post

func (tr *TumblrRequest) Post(requestUrl string, params map[string]string) CompleteResponse

Makes a POST request to the API, allows for multipart data uploads. requestUrl: the url you are making the request to. params: all the parameters needed for the request.

type TumblrRestClient

type TumblrRestClient struct {
	// contains filtered or unexported fields
}

defines a Go Client for the Tumblr API.

func NewTumblrRestClient

func NewTumblrRestClient(consumerKey, consumerSecret, oauthToken, oauthSecret, callbackUrl, host string) *TumblrRestClient

Initializes the TumblrRestClient, creating TumblrRequest that deals with all request formatting. consumerKey is the consumer key of your Tumblr Application. consumerSecret is the consumer secret of your Tumblr Application. oauthToken is the user specific token, received from the /access_token endpoint. oauthSecret is the user specific secret, received from the /access_token endpoint. host is the host that you are tryng to send information to (e.g. http://api.tumblr.com).

func (*TumblrRestClient) Avatar

func (trc *TumblrRestClient) Avatar(blogname string, size int) AvatarResponse

Retrieves the url of the blog's avatar. size can be: 16, 24, 30, 40, 48, 64, 96, 128 or 512.

func (*TumblrRestClient) BlogInfo

func (trc *TumblrRestClient) BlogInfo(blogname string) BlogInfoResponse

Gets general information about the blog. blogname: name of the blog you want to get information about(e.g. mgterzieva.tumblr.com).

func (*TumblrRestClient) BlogLikes

func (trc *TumblrRestClient) BlogLikes(blogname string, options map[string]string) LikesResponse

Gets the likes of blog given. blogname: name of the blog whose likes you want to get. options can be: limit: how many likes do you want to get; offset: the number of the like you want to start from.

func (*TumblrRestClient) CreateAudio

func (trc *TumblrRestClient) CreateAudio(blogname string, options map[string]string) error

Create an audio post on a blog. blogname: the url of the blog you want to post to. options can be: (with * are marked required options) state: the state of the post(e.g. published, draft, queue, private); tags: a list of tags you want applied to the post; tweet: manages the autotweet for this post: set to off for no tweet or enter text to override the default tweet; date: the GMT date and time of the post as a string; format: sets the format type of the post(html or markdown); slug: add a short text summary to the end of the post url; caption: the caption of the post; *external_url: the url of the site that hosts the audio file.

func (*TumblrRestClient) CreateChatPost

func (trc *TumblrRestClient) CreateChatPost(blogname string, options map[string]string) error

Create a chat post on a blog. blogname: the url of the blog you want to post to. options can be: (with * are marked required options) state: the state of the post(e.g. published, draft, queue, private); tags: a list of tags you want applied to the post; tweet: manages the autotweet for this post: set to off for no tweet or enter text to override the default tweet; date: the GMT date and time of the post as a string; format: sets the format type of the post(html or markdown); slug: add a short text summary to the end of the post url; title: the title of the chat; *conversation: the text of the conversation/chat, with dialogue labels.

func (trc *TumblrRestClient) CreateLink(blogname string, options map[string]string) error

Create a link post on a blog. blogname: the url of the blog you want to post to. options can be: (with * are marked required options) state: the state of the post(e.g. published, draft, queue, private); tags: a list of tags you want applied to the post; tweet: manages the autotweet for this post: set to off for no tweet or enter text to override the default tweet; date: the GMT date and time of the post as a string; format: sets the format type of the post(html or markdown); slug: add a short text summary to the end of the post url; title: the title of the page the link points to; *url: the link you are posting; description: the description of the link you are posting.

func (*TumblrRestClient) CreatePhoto

func (trc *TumblrRestClient) CreatePhoto(blogname string, options map[string]string) error

Create a photo post or photoset on a blog. blogname: the url of the blog you want to post to. options can be: (with * are marked required options) state: the state of the post(e.g. published, draft, queue, private); tags: a list of tags you want applied to the post; tweet: manages the autotweet for this post: set to off for no tweet or enter text to override the default tweet; date: the GMT date and time of the post as a string; format: sets the format type of the post(html or markdown); slug: add a short text summary to the end of the post url; caption: the caption that you want applied to the photo; link: the 'click-through' url for the photo; *source: the photo source url.

func (*TumblrRestClient) CreateQuote

func (trc *TumblrRestClient) CreateQuote(blogname string, options map[string]string) error

Create a quote post on a blog. blogname: the url of the blog you want to post to. options can be: (with * are marked required options) state: the state of the post(e.g. published, draft, queue, private); tags: a list of tags you want applied to the post; tweet: manages the autotweet for this post: set to off for no tweet or enter text to override the default tweet; date: the GMT date and time of the post as a string; format: sets the format type of the post(html or markdown); slug: add a short text summary to the end of the post url; *quote: the full text of the quote; source: the cited source of the quote.

func (*TumblrRestClient) CreateText

func (trc *TumblrRestClient) CreateText(blogname string, options map[string]string) error

Create a text post on a blog. blogname: the url of the blog you want to post to. options can be: (with * are marked required options) state: the state of the post(e.g. published, draft, queue, private); tags: a list of tags you want applied to the post; tweet: manages the autotweet for this post: set to off for no tweet or enter text to override the default tweet; date: the GMT date and time of the post as a string; format: sets the format type of the post(html or markdown); slug: add a short text summary to the end of the post url; title: the optional title of the post; *body: the full text body.

func (*TumblrRestClient) CreateVideo

func (trc *TumblrRestClient) CreateVideo(blogname string, options map[string]string) error

Create a video post on a blog. blogname: the url of the blog you want to post to. options can be: (with * are marked required options) state: the state of the post(e.g. published, draft, queue, private); tags: a list of tags you want applied to the post; tweet: manages the autotweet for this post: set to off for no tweet or enter text to override the default tweet; date: the GMT date and time of the post as a string; format: sets the format type of the post(html or markdown); slug: add a short text summary to the end of the post url; caption: the caption for the post; *embed: the html embed code for the video.

func (*TumblrRestClient) Dashboard

func (trc *TumblrRestClient) Dashboard(options map[string]string) DraftsResponse

Gets the dashboard of the user. options can be: limit: number of results to return; offset: post number to start at; type: the type of posts to return(text, photo, quote, link, chat, audio, video, answer); since_id: return posts that have apeared after this id; reblog_info: whether to return reblog information about the posts; notes_info: whether to return notes information about the posts.

func (*TumblrRestClient) DeletePost

func (trc *TumblrRestClient) DeletePost(blogname, id string) error

Deletes a post with a given id. blogname: the url of the blog you want to delete from. id: the id of the post you want to delete.

func (*TumblrRestClient) Drafts

func (trc *TumblrRestClient) Drafts(blogname string, options map[string]string) DraftsResponse

Gets posts that are currently in the blog's drafts. options can be: filter: specify posts' format(e.g. format="html", format="text", format="raw").

func (*TumblrRestClient) EditPost

func (trc *TumblrRestClient) EditPost(blogname string, options map[string]string) error

Edits a post with a given id. blogname: the url of the blog you want to post to. options can be: (with * are marked required options) tags: a list of tags you want applied to the post; tweet: manages the autotweet for this post: set to off for no tweet or enter text to override the default tweet; date: the GMT date and time of the post as a string; format: sets the format type of the post(html or markdown); slug: add a short text summary to the end of the post url; *id: the id of the post. The other options are specific to the type of post you want to edit.

func (*TumblrRestClient) Follow

func (trc *TumblrRestClient) Follow(blogname string) error

Follow the url of a given blog. blogname: the url of the blog to follow.

func (*TumblrRestClient) Followers

func (trc *TumblrRestClient) Followers(blogname string, options map[string]string) FollowersResponse

Gets the followers of the blog given. blogname: name of the blog whose followers you want to get. optons can be: limit: the number of results to return, inclusive; offset: result to start at.

func (*TumblrRestClient) Following

func (trc *TumblrRestClient) Following(options map[string]string) FollowingResponse

Gets the blogs that the user is following. options can be: limit: the number of results to return; offset: result number to start at.

func (*TumblrRestClient) Info

func (trc *TumblrRestClient) Info() UserInfoResponse

Gets the user information.

func (*TumblrRestClient) Like

func (trc *TumblrRestClient) Like(id, reblogKey string) error

Like post of a given blog. id: the id of the post you want to like. reblog_key: the reblog key for the post id.

func (*TumblrRestClient) Likes

func (trc *TumblrRestClient) Likes(options map[string]string) LikesResponse

Gets the likes of the given user. options can be: limit: the number of results to return, inclusive; offset: liked post number to start at.

func (*TumblrRestClient) Posts

func (trc *TumblrRestClient) Posts(blogname, postsType string, options map[string]string) PostsResponse

Gets a list of posts from a blog. blogname: the name of the blog you want to get posts from (e.g. mgterzieva.tumblr.com). postsType: the type of the posts you want to get (e.g. text, quote, link, answer, video, audio, photo, chat, all). options can be: id: the id of the post you are looking for; tag: return only posts with this tag; limit: the number of posts to return; offset: the number of the post you want to start from; filter: return only posts with a specific format(e.g. html, text, raw).

func (*TumblrRestClient) Queue

func (trc *TumblrRestClient) Queue(blogname string, options map[string]string) DraftsResponse

Gets posts that are currently in the blog's queue. options can be: limit: the number of results to return; offset: post number to start at; filter: specify posts' format(e.g. format="html", format="text", format="raw").

func (*TumblrRestClient) Reblog

func (trc *TumblrRestClient) Reblog(blogname string, options map[string]string) error

Creates a reblog on the given blog. blogname: the url of the blog you want to reblog to. options should be: (with * are marked required options) *id: the id of the reblogged post; *reblog_key: the reblog key of the rebloged post.

func (*TumblrRestClient) Submission

func (trc *TumblrRestClient) Submission(blogname string, options map[string]string) DraftsResponse

Retrieve submission posts. options can be: offset: post number to start at; filter: specify posts' format(e.g. format="html", format="text", format="raw").

func (*TumblrRestClient) Tagged

func (trc *TumblrRestClient) Tagged(tag string, options map[string]string) []json.RawMessage

Gets a list of posts with the given tag. tag: the tag you want to look for. options can be: before: the timestamp of when you'd like to see posts before; limit: the number of results to return; filter: the post format you want to get(e.g html, text, raw).

func (*TumblrRestClient) Unfollow

func (trc *TumblrRestClient) Unfollow(blogname string) error

Unfollow the url of a given blog. blogname: the url of the blog to unfollow.

func (*TumblrRestClient) Unlike

func (trc *TumblrRestClient) Unlike(id, reblogKey string) error

Unlike a post of a given blog. id: the id of the post you want to unlike. reblog_key: the reblog key for the post id.

type User

type User struct {
	Name      string
	Following bool
	Url       string
	Updated   int64
}

type UserInfo

type UserInfo struct {
	Following           int64
	Default_post_format string
	Name                string
	Likes               int64
	Blogs               []OwnedBlog
}

type UserInfoResponse

type UserInfoResponse struct {
	User UserInfo
}

type VideoPost

type VideoPost struct {
	BasePost
	Caption string
	Player  []PlayerInfo
}

Jump to

Keyboard shortcuts

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