func postImageTweet(api *anaconda.TwitterApi, gifFile string, t *anaconda.Tweet) error { // Post data, err := ioutil.ReadFile(gifFile) if err != nil { return err } mediaResponse, err := api.UploadMedia(base64.StdEncoding.EncodeToString(data)) if err != nil { return err } v := url.Values{} v.Set("media_ids", strconv.FormatInt(mediaResponse.MediaID, 10)) v.Set("in_reply_to_status_id", t.IdStr) tweetString := fmt.Sprintf("@%s here are your fireworks", t.User.ScreenName) _, err = api.PostTweet(tweetString, v) if err != nil { return err } else { // fmt.Println(result) } return nil }
func PostTweet(api *anaconda.TwitterApi, reader io.ReadCloser) error { buff := bytes.Buffer{} encoder := base64.NewEncoder(base64.StdEncoding, &buff) _, err := io.Copy(encoder, reader) if err != nil { return err } reader.Close() encoder.Close() media, err := api.UploadMedia(buff.String()) if err != nil { return err } values := url.Values{} values.Set("media_ids", fmt.Sprintf("%d", media.MediaID)) _, err = api.PostTweet("", values) if err != nil { fmt.Println(err) } return nil }
func (g *gweet) tweet(api *anaconda.TwitterApi, text string, files []string, lewd bool) (tweetUrl string, err error) { log.Printf(`Tweeting {"%s", %v, lewd=%v}.`+"\n", text, files, lewd) var ids string var data []byte for _, filePath := range files { log.Println("Uploading", filePath) // TODO: refuse files larger than 15mb for vids and 5mb for images data, err = ioutil.ReadFile(filePath) if err != nil { return } /*mime*/ meme := http.DetectContentType(data) log.Println(meme) if !strings.HasPrefix( /*mime*/ meme, "image") { var media anaconda.ChunkedMedia var videoMedia anaconda.VideoMedia // TODO: do not read entire file to memory media, err = api.UploadVideoInit(len(data), "video/mp4") if err != nil { return } chunkIndex := 0 for i := 0; i < len(data); i += 5242879 { log.Println("Chunk", chunkIndex) err = api.UploadVideoAppend(media.MediaIDString, chunkIndex, base64.StdEncoding.EncodeToString( data[i:int(math.Min(5242879.0, float64(len(data))))], ), ) if err != nil { return } chunkIndex++ } videoMedia, err = api.UploadVideoFinalize(media.MediaIDString) if err != nil { return } ids += videoMedia.MediaIDString ids += "," } else { var media anaconda.Media media, err = api.UploadMedia( base64.StdEncoding.EncodeToString(data)) ids += media.MediaIDString ids += "," } if err != nil { return } } ids = ids[:len(ids)-1] log.Println("media_ids:", ids) v := url.Values{} v.Set("media_ids", ids) v.Set("possibly_sensitive", strconv.FormatBool(lewd)) tweet, err := api.PostTweet(text, v) if err != nil { return } tweetUrl = fmt.Sprintf("https://twitter.com/%s/status/%s", tweet.User.ScreenName, tweet.IdStr) return }