Added hashtag enpoint
This commit is contained in:
@@ -1,2 +1,4 @@
|
|||||||
# nedim-vjezba-golang
|
# nedim-vjezba-golang
|
||||||
nedim vjezba golang
|
nedim vjezba golang
|
||||||
|
|
||||||
|
Uses dependency `go get github.com/gorilla/mux`
|
||||||
|
|||||||
93
main.go
93
main.go
@@ -5,59 +5,72 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
//"name": "#ChainedToTheRhythm",
|
type Trend [] struct {
|
||||||
//"url": "http://twitter.com/search?q=%23ChainedToTheRhythm",
|
Trends [] struct {
|
||||||
//"promoted_content": null,
|
Name string `json:"name,omitempty"`
|
||||||
//"query": "%23ChainedToTheRhythm",
|
Url string `json:"url,omitempty"`
|
||||||
//"tweet_volume": 48857
|
PromotedContent string `promoted_content:"lastname,omitempty"`
|
||||||
|
Query string `json:"query,omitempty"`
|
||||||
type Tweet struct {
|
TweetVolume int `json:"tweet_volume,omitempty"`
|
||||||
Name string `json:"name,omitempty"`
|
} `json:"trends"`
|
||||||
Url string `json:"url,omitempty"`
|
|
||||||
PromotedContent string `promoted_content:"lastname,omitempty"`
|
|
||||||
Query string `json:"query,omitempty"`
|
|
||||||
TweetVolume string `json:"tweet_volume,omitempty"`
|
|
||||||
}
|
}
|
||||||
type Address struct {
|
|
||||||
City string `json:"city,omitempty"`
|
|
||||||
State string `json:"state,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var tweets []Tweet
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
tweets = append(tweets, Tweet{Name: "#ChainedToTheRhythm", Url: "http://twitter.com/search?q=%23ChainedToTheRhythm", PromotedContent: "null", Query: "%23ChainedToTheRhythm" , TweetVolume: "48857"})
|
|
||||||
//people = append(people, Person{ID: "2", Firstname: "Koko", Lastname: "Doe", Address: &Address{City: "City Z", State: "State Y"}})
|
|
||||||
//people = append(people, Person{ID: "3", Firstname: "Francis", Lastname: "Sunday"})
|
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
router.HandleFunc("/hashtags", GetHastags).Methods("GET")
|
router.HandleFunc("/hashtags", GetHashtags).Methods("GET")
|
||||||
//router.HandleFunc("/people", GetPeople).Methods("GET")
|
router.HandleFunc("/hashtags/{woeid}", GetHashtagFromWOEID).Methods("GET")
|
||||||
////router.HandleFunc("/people/{id}", GetPerson).Methods("GET")
|
|
||||||
//router.HandleFunc("/people/{id}", CreatePerson).Methods("POST")
|
|
||||||
//router.HandleFunc("/people/{id}", DeletePerson).Methods("DELETE")
|
|
||||||
log.Fatal(http.ListenAndServe(":8000", router))
|
log.Fatal(http.ListenAndServe(":8000", router))
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetHastags(w http.ResponseWriter, r *http.Request) {
|
func GetHashtags(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
resp, err := http.Get("https://api.twitter.com/1.1/trends/place.json?id=1")
|
body := sendRequestToTwitter("1")
|
||||||
|
jsonResponse, err := json.Marshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("There was an error")
|
panic(err)
|
||||||
println(err)
|
|
||||||
// handle error
|
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
w.Write(jsonResponse)
|
||||||
println(body)
|
|
||||||
json.NewEncoder(w).Encode(body)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPeople(w http.ResponseWriter, r *http.Request) {
|
func GetHashtagFromWOEID(w http.ResponseWriter, r *http.Request) {
|
||||||
//json.NewEncoder(w).Encode(people)
|
|
||||||
|
params := mux.Vars(r)
|
||||||
|
body := sendRequestToTwitter(params["woeid"])
|
||||||
|
jsonResponse, err := json.Marshal(body)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write(jsonResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
func CreatePerson(w http.ResponseWriter, r *http.Request) {}
|
|
||||||
func DeletePerson(w http.ResponseWriter, r *http.Request) {}
|
|
||||||
Reference in New Issue
Block a user