2018-04-24 16:56:59 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"encoding/json"
|
2018-04-25 17:11:12 +02:00
|
|
|
"fmt"
|
2018-04-24 16:56:59 +02:00
|
|
|
)
|
|
|
|
|
|
2018-04-25 17:11:12 +02:00
|
|
|
type Trend [] struct {
|
|
|
|
|
Trends [] struct {
|
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
|
Url string `json:"url,omitempty"`
|
|
|
|
|
PromotedContent string `promoted_content:"lastname,omitempty"`
|
|
|
|
|
Query string `json:"query,omitempty"`
|
|
|
|
|
TweetVolume int `json:"tweet_volume,omitempty"`
|
|
|
|
|
} `json:"trends"`
|
2018-04-24 16:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
|
|
router := mux.NewRouter()
|
2018-04-25 17:11:12 +02:00
|
|
|
router.HandleFunc("/hashtags", GetHashtags).Methods("GET")
|
|
|
|
|
router.HandleFunc("/hashtags/{woeid}", GetHashtagFromWOEID).Methods("GET")
|
2018-04-24 16:56:59 +02:00
|
|
|
log.Fatal(http.ListenAndServe(":8000", router))
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 17:11:12 +02:00
|
|
|
func GetHashtags(w http.ResponseWriter, r *http.Request) {
|
2018-04-24 16:56:59 +02:00
|
|
|
|
2018-04-25 17:11:12 +02:00
|
|
|
body := sendRequestToTwitter("1")
|
|
|
|
|
jsonResponse, err := json.Marshal(body)
|
2018-04-24 16:56:59 +02:00
|
|
|
if err != nil {
|
2018-04-25 17:11:12 +02:00
|
|
|
panic(err)
|
2018-04-24 16:56:59 +02:00
|
|
|
}
|
2018-04-25 17:11:12 +02:00
|
|
|
|
|
|
|
|
w.Write(jsonResponse)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetHashtagFromWOEID(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
params := mux.Vars(r)
|
|
|
|
|
body := sendRequestToTwitter(params["woeid"])
|
|
|
|
|
jsonResponse, err := json.Marshal(body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(jsonResponse)
|
2018-04-24 16:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
2018-04-25 17:11:12 +02:00
|
|
|
func sendRequestToTwitter(WOEID string) []string {
|
|
|
|
|
|
|
|
|
|
url := "https://api.twitter.com/1.1/trends/place.json?id=" + WOEID
|
|
|
|
|
req, _ := http.NewRequest("GET", url, nil)
|
|
|
|
|
req.Header.Add("Authorization", "Bearer AAAAAAAAAAAAAAAAAAAAAKCtPAAAAAAAq1L5CTf40mf5K%2B7Q6QcWxsyjNvo%3DRkSK8AQBdk6latG2h47XWJVSQdn98heLv8HDLDhviicP3xvodm")
|
|
|
|
|
req.Header.Add("Cache-Control", "no-cache")
|
|
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
|
res, _ := http.DefaultClient.Do(req)
|
|
|
|
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
trends := Trend{}
|
|
|
|
|
var hashtags []string
|
|
|
|
|
|
|
|
|
|
err := json.NewDecoder(res.Body).Decode(&trends)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error while parsing data: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, t := range trends[0].Trends {
|
|
|
|
|
hashtags = append(hashtags, t.Name)
|
|
|
|
|
println(t.Name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hashtags
|
2018-04-24 16:56:59 +02:00
|
|
|
}
|