Example #1
0
func PublishTweet(article *Article, replyToId string, api *anaconda.TwitterApi) (string, error) {
	msg := twitterize(article)
	_, err := api.PostTweet(msg, url.Values{
		"in_reply_to_status_id": []string{replyToId},
	})
	return msg, err
}
Example #2
0
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
}
Example #3
0
func updateStatus(subject string, api anaconda.TwitterApi) bool {
	_, err := api.PostTweet(subject, nil)
	if err != nil {
		fmt.Println("Posting Tweet failed! Error : ", err)
		os.Exit(1)
	}
	return true
}
Example #4
0
func Tweet(status string) (tweet anaconda.Tweet, err error) {
	var client *anaconda.TwitterApi
	if twitterClient != nil {
		client = twitterClient
	} else {
		client = TwitterClient()
	}
	return client.PostTweet(status, url.Values{})
}
Example #5
0
func PostTweets(api *anaconda.TwitterApi, bts []BeetTweet, db *sql.DB) {
	for _, bt := range bts {
		//TODO: Psuedo random reminder messages. For fun, and not angering the twitter api.
		_, err := api.PostTweet(fmt.Sprintf("@%s The number is %d.", bt.Name, bt.Hours), nil)

		if err == nil {
			query := fmt.Sprintf("UPDATE tweet SET NOTIFIED = true WHERE ID = %d;", bt.Id)
			db.Query(query)
		}
	}
}
Example #6
0
// Given config and observation, tweets latest update
func tweetCurrent(config Config, o string) {
	fmt.Println("Preparing to tweet observation...")
	var api *anaconda.TwitterApi
	api = anaconda.NewTwitterApi(config.Token, config.TokenSecret)
	anaconda.SetConsumerKey(config.ConsumerKey)
	anaconda.SetConsumerSecret(config.ConsumerSecret)
	tweet, err := api.PostTweet(o, nil)
	if err != nil {
		fmt.Println("update error:", err)
	} else {
		fmt.Println("Tweet posted:")
		fmt.Println(tweet.Text)
	}
}
Example #7
0
func feedJBBS2Twitter(board *jbbsreader.Board, api *anaconda.TwitterApi) error {
	done := make(chan struct{})
	defer close(done)
	respc, errc := board.FeedNewResponses(done)
	for resp := range respc {
		log.Printf("Fetched %#v\n", resp)
		tweet, err := api.PostTweet(resp.Content, nil)
		if err != nil {
			return err
		}
		log.Printf("Sent tweet %#v\n", tweet)
	}
	return <-errc
}
Example #8
0
// Respond to an image somebody posted
func Respond(tweet anaconda.Tweet, classification string, twitter anaconda.TwitterApi) bool {
	msg := fmt.Sprintf(
		"%s https://twitter.com/%s/status/%s",
		message(classification),
		tweet.User.IdStr,
		tweet.IdStr,
	)

	reply, errRpl := twitter.PostTweet(msg, url.Values{})

	if errRpl != nil {
		log.Printf("Error responding: ", errRpl)
		return false
	}

	// else
	log.Printf("Tweeted; link: https://twitter.com/%s/status/%d\n", reply.User.ScreenName, reply.Id)
	return true
}
Example #9
0
//ツイート関数。無駄に並列化している。140字以上の分割ツイートにも対応。
func tweet(api *anaconda.TwitterApi, waitGroup *sync.WaitGroup, begin string, end string, str string) {
	sLen := 140 - utf8.RuneCountInString(begin) - utf8.RuneCountInString(end)
	for str != "" {
		limit := int(math.Min(float64(sLen), float64(utf8.RuneCountInString(str))))
		waitGroup.Add(1)
		go func(api *anaconda.TwitterApi, waitGroup *sync.WaitGroup, begin, end, str string) {
			defer waitGroup.Done()
			tweetstr := begin + str + end
			if *r {
				tweetstr = reverse(tweetstr)
			}
			if *h {
				hash := sha256.Sum256([]byte(tweetstr))
				tweetstr = hex.EncodeToString(hash[:])
			}

			api.PostTweet(tweetstr, nil)
		}(api, waitGroup, begin, end, string([]rune(str)[:limit]))
		str = string([]rune(str)[limit:])
	}
}
Example #10
0
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
}
Example #11
0
func gameGenerator(api *anaconda.TwitterApi) func(anaconda.Tweet) bool {
	var enemyHp int = 3 //HP is Hit Point
	var myHp int = 3
	var enemyTp int = 0 //Tp is Tame Point
	var myTp int = 0
	var turn int = 0
	var maouRoutine = [3][10]int{
		{0, 1, 0, 2, 0, 0, 0, 2, 0, 1}, //攻撃60%防御20%タメ20%
		{1, 1, 1, 0, 0, 2, 2, 1, 1, 1}, //攻撃20%防御60%タメ20%
		{0, 1, 1, 0, 2, 0, 1, 1, 1, 0}, //攻撃40%防御50%タメ10%
	}
	var commands = [4]string{"こうげき", "ぼうぎょ", "ため", "ひっさつ"}
	var lossRep = " あなたの負けです"
	var winRep = " あなたの勝ちです"

	//げーむるーちん
	return func(tweet anaconda.Tweet) bool {
		//fmt.Println("tw",tweet.Text)
		//fmt.Println("turn",)
		var act = -1

		for i := 0; i < len(commands); i++ {
			if strings.Contains(tweet.Text, commands[i]) {
				act = i
				break
			}
		}
		if act < 0 {
			return false
		}

		turn++
		turn %= len(maouRoutine)
		rand.Seed(time.Now().UnixNano())
		routine := rand.Intn(10)
		mact := maouRoutine[turn][routine]
		if enemyTp == 3 {
			mact = 3
		}

		sendmess := "@" + tweet.User.ScreenName + " "
		sendmess += "勇者は" + commands[act] + "をした\n"
		sendmess += "魔王は" + commands[mact] + "をした\n"

		//以下、初心者あともすふぇあ満載のif祭り
		//こうげき
		if mact == 0 && act != 1 {
			sendmess += "勇者はダメージを受けた\n"
			myHp--
		}
		if mact != 1 && act == 0 {
			sendmess += "魔王はダメージを受けた\n"
			enemyHp--
		}

		//ため
		if act == 2 {
			myTp++
			sendmess += "勇者タメ" + strconv.Itoa(myTp) + "\n"
		}
		if mact == 2 {
			enemyTp++
			sendmess += "魔王タメ" + strconv.Itoa(enemyTp) + "\n"
		}

		//ひっさつ
		if mact == 3 {
			sendmess += "勇者は必殺を受けた\n"
			if act == 1 {
				myHp++
			}
			myHp -= 3
			enemyTp = 0
		}
		if act == 3 {
			if myTp < 3 {
				sendmess += "勇者はためが足りなかった\n"
			} else {
				sendmess += "魔王は必殺を受けた\n"
				if mact == 1 {
					enemyHp++
				}
				enemyHp -= 3
			}
			myTp = 0
		}

		if enemyHp <= 0 {
			sendmess += winRep
		} else if myHp <= 0 {
			sendmess += lossRep
		}

		api.PostTweet(sendmess+"【"+tweet.IdStr+"】"+tag, nil)

		return enemyHp <= 0 || myHp <= 0
	}
}
Example #12
0
func onTweet(api *anaconda.TwitterApi, tw anaconda.Tweet) {
	log.Printf("Tweet: %s", tw.Text)
	var err error
	if urlRegexp == nil {
		urlRegexp, err = regexp.Compile(kUrlRegexp)
		if err != nil {
			log.Fatal(err)
		}
	}
	text := tw.Text
	text = urlRegexp.ReplaceAllString(text, "")
	rs, err := mecab.Parse(text)
	if err != nil {
		log.Fatal(err)
	}
	strs := make([]string, len(rs))
	for i, r := range rs {
		strs[i] = r.Surface
	}
	log.Print(strs)
	alphas := 0
	for _, r := range rs {
		rlen := len([]rune(r.Surface))
		if count(kAlphabet, r.Surface) > (rlen / 2) {
			alphas++
		}
	}
	if alphas > (len(rs) / 2) {
		return
	}
	candidates := make([]string, 0)
	for _, r := range rs {
		if contains(kStopWords, r.Surface) {
			return
		}
		rlen := len([]rune(r.Surface))
		syms := count(kSymbols, r.Surface)
		if rlen <= 1 || (syms) > (rlen/2) {
			continue
		}
		f := r.Feature
		if !(r.Pos == "名詞" && !strings.Contains(f, "接尾") &&
			!strings.Contains(f, "代名詞") &&
			!strings.Contains(f, "接頭詞") &&
			!strings.Contains(f, "形容動詞語幹")) {
			continue
		}
		candidates = append(candidates, r.Surface)
	}
	if len(candidates) <= 0 {
		return
	}
	c := candidates[rand.Intn(len(candidates))]
	log.Printf("%s as a Service\n", c)
	t, err := api.PostTweet(c+" as a Service", nil)
	if err != nil {
		log.Println(err)
	}
	log.Println(t)
	os.Exit(0)

}
Example #13
0
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
}