You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.4 KiB

package WorstCaptcha
import (
"fmt"
"github.com/kurrik/oauth1a"
"github.com/kurrik/twittergo"
"net/http"
"net/url"
"os"
6 years ago
"strings"
)
func NewTwitter(config Config) Twitter {
twitterConfig := &oauth1a.ClientConfig{
ConsumerKey: config.Twitter.ConsumerKey,
ConsumerSecret: config.Twitter.ConsumerSecret,
}
user := oauth1a.NewAuthorizedConfig(
config.Twitter.AccessToken,
config.Twitter.AccessTokenSecret,
)
return Twitter{twittergo.NewClient(twitterConfig, user)}
}
type Twitter struct {
client *twittergo.Client
}
6 years ago
//
// Searching
//
func (t *Twitter) doSearch() []twittergo.Tweet {
var (
err error
req *http.Request
resp *twittergo.APIResponse
results *twittergo.SearchResults
)
// Build search
query := url.Values{}
query.Set("q", "from:reverseocr")
query.Set("count", "20")
// Build URI
url := fmt.Sprintf("/1.1/search/tweets.json?%v", query.Encode())
// Prepare request
if req, err = http.NewRequest("GET", url, nil); err != nil {
fmt.Printf("Could not parse request: %v\n", err)
os.Exit(1)
}
// Sign and send request
if resp, err = t.client.SendRequest(req); err != nil {
fmt.Printf("Could not send request: %v\n", err)
os.Exit(1)
}
// Parse response
results = &twittergo.SearchResults{}
if err = resp.Parse(results); err != nil {
fmt.Printf("Problem parsing response: %v\n", err)
os.Exit(1)
}
// Print ratelimit data
if resp.HasRateLimit() {
fmt.Printf("Rate limit: %v\n", resp.RateLimit())
fmt.Printf("Rate limit remaining: %v\n", resp.RateLimitRemaining())
fmt.Printf("Rate limit reset: %v\n", resp.RateLimitReset())
}
6 years ago
return results.Statuses()
}
6 years ago
//
// Get Images from twitter
//
func (t *Twitter) GetImages() WordLinks {
var wls = WordLinks{}
for _, tweet := range t.doSearch() {
wls[t.filterTweet(tweet)] = true
}
return wls
}
//
// Convert a single tweet to a set of word and link
//
func (t *Twitter) filterTweet(tweet twittergo.Tweet) WordLink {
// Type assert entities to be a map
entities := tweet["entities"].(map[string]interface{})
// Type assert media to be a list
mediaList := entities["media"].([]interface{})
// Type assert the media to be a map
media := mediaList[0].(map[string]interface{})
// Get the link
imageUrl := media["media_url_https"].(string)
// Parts of word
parts := strings.Split(tweet.Text(), " ")
// Build word without link
word := strings.Join(parts[0:len(parts)-1], " ")
return WordLink{word, imageUrl}
}