diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..cfdae31 --- /dev/null +++ b/Readme.md @@ -0,0 +1,48 @@ +#Degrees of Separation + +With cinema going global these days, every one of the [A-Z]ollywoods are now connected. Use the wealth of data available at Moviebuff to see how. + +Write a Go program that behaves the following way: + +$ degrees amitabh-bachchan robert-de-niro + +Degrees of Separation: 3 + +1. Movie: The Great Gatsby +Supporting Actor: Amitabh Bachchan +Actor: Leonardo DiCaprio + +2. Movie: The Wolf of Wall Street +Actor: Leonardo DiCaprio +Director: Martin Scorsese + +3. Movie: Taxi Driver +Director: Martin Scorsese +Actor: Robert De Niro +Your solution should use the Moviebuff data available to figure out the smallest degree of separation between the two people. All the inputs should be Moviebuff URLs for their respective people: For Amitabh Bachchan, his page is on http://www.moviebuff.com/amitabh-bachchan and his Moviebuff URL is amitabh-bachchan. + +Please do not attempt to scrape the Moviebuff website - All the data is available on an S3 bucket in an easy to parse JSON format here: https://data.moviebuff.com/{moviebuff_url} + +To solve the example above, your solution would fetch at least the following: + +http://data.moviebuff.com/amitabh-bachchan + +http://data.moviebuff.com/the-great-gatsby + +http://data.moviebuff.com/leonardo-dicaprio + +http://data.moviebuff.com/the-wolf-of-wall-street + +http://data.moviebuff.com/martin-scorsese + +http://data.moviebuff.com/taxi-driver + +##Notes + +If you receive HTTP errors when trying to fetch the data, that might be the CDN throttling you. Luckily, Go has some very elegant idioms for rate limiting :) +There may be a discrepancy in some cases where a movie appears on an actor's list but not vice versa. This usually happens when we edit data while exporting it, so feel free to either ignore these mismatches or handle them in some way. +Write a program in any language you want (If you're here from Gophercon, use Go :D) that does this. Feel free to make your own input and output format / command line tool / GUI / Webservice / whatever you want. Feel free to hold the dataset in whatever structure you want, but try not to use external databases - as far as possible stick to your langauage without bringing in MySQL/Postgres/MongoDB/Redis/Etc. + +To submit a solution, fork this repo and send a Pull Request on Github. + +For any questions or clarifications, raise an issue on this repo and we'll answer your questions as fast as we can. \ No newline at end of file diff --git a/degree b/degree new file mode 100644 index 0000000..302bdba Binary files /dev/null and b/degree differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ad8c84b --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/RealImage/Challenge + +go 1.21.3 diff --git a/info.log b/info.log new file mode 100644 index 0000000..6f58bb8 --- /dev/null +++ b/info.log @@ -0,0 +1,48 @@ +2023/10/30 00:17:04 main.go:105: fetching tom-cruise +2023/10/30 00:17:04 main.go:105: fetching kevin-bacon +2023/10/30 00:17:04 main.go:105: fetching tom-cruise +2023/10/30 00:17:04 main.go:105: fetching jack-reacher-never-go-back +2023/10/30 00:17:04 main.go:105: fetching mena +2023/10/30 00:17:04 main.go:105: fetching valkyrie +2023/10/30 00:17:04 main.go:105: fetching jack-reacher +2023/10/30 00:17:04 main.go:105: fetching mission-impossible-iii +2023/10/30 00:17:04 main.go:105: fetching mission-impossible-ghost-protocol +2023/10/30 00:17:04 main.go:105: fetching a-few-good-men +2023/10/30 00:17:04 main.go:105: fetching collateral +2023/10/30 00:17:04 main.go:105: fetching far-and-away +2023/10/30 00:17:04 main.go:105: fetching rain-man +2023/10/30 00:17:04 main.go:105: fetching knight-and-day +2023/10/30 00:17:04 main.go:105: fetching mission-impossible-rogue-nation +2023/10/30 00:17:04 main.go:105: fetching top-gun-2 +2023/10/30 00:17:04 main.go:105: fetching edge-of-tomorrow +2023/10/30 00:17:04 main.go:105: fetching top-gun +2023/10/30 00:17:04 main.go:105: fetching risky-business +2023/10/30 00:17:04 main.go:105: fetching go-like-hell +2023/10/30 00:17:04 main.go:105: fetching interview-with-the-vampire-the-vampire-chronicles +2023/10/30 00:17:04 main.go:105: fetching endless-love-1981-english +2023/10/30 00:17:04 main.go:105: fetching tropic-thunder +2023/10/30 00:17:04 main.go:105: fetching lions-for-lambs +2023/10/30 00:17:04 main.go:105: fetching the-color-of-money +2023/10/30 00:17:04 main.go:105: fetching the-last-samurai +2023/10/30 00:17:04 main.go:105: fetching the-firm +2023/10/30 00:17:04 main.go:105: fetching days-of-thunder +2023/10/30 00:17:04 main.go:105: fetching born-on-the-fourth-of-july +2023/10/30 00:17:04 main.go:105: fetching without-limits +2023/10/30 00:17:04 main.go:105: fetching war-of-the-worlds +2023/10/30 00:17:04 main.go:105: fetching cocktail-1988-english +2023/10/30 00:17:04 main.go:105: fetching minority-report +2023/10/30 00:17:04 main.go:105: fetching vanilla-sky +2023/10/30 00:17:04 main.go:105: fetching stanley-kubrick-a-life-in-pictures +2023/10/30 00:17:04 main.go:105: fetching oblivion +2023/10/30 00:17:04 main.go:105: fetching losin-it +2023/10/30 00:17:04 main.go:105: fetching legend-1985-english +2023/10/30 00:17:04 main.go:105: fetching all-the-right-moves +2023/10/30 00:17:04 main.go:105: fetching the-outsiders +2023/10/30 00:17:04 main.go:105: fetching taps +2023/10/30 00:17:04 main.go:105: fetching mission-impossible-ii +2023/10/30 00:17:04 main.go:105: fetching space-station +2023/10/30 00:17:04 main.go:105: fetching eyes-wide-shut +2023/10/30 00:17:04 main.go:105: fetching jerry-maguire +2023/10/30 00:17:04 main.go:105: fetching austin-powers-in-goldmember +2023/10/30 00:17:04 main.go:105: fetching magnolia +2023/10/30 00:17:04 main.go:105: fetching rock-of-ages diff --git a/main.go b/main.go new file mode 100644 index 0000000..21be9cc --- /dev/null +++ b/main.go @@ -0,0 +1,222 @@ + +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "time" + "github.com/RealImage/Challenge/models" + "github.com/RealImage/Challenge/utilities" +) + +var ( + Log *log.Logger +) + +func init() { + file, err := os.Create("info.log") + if err != nil { + panic(err) + } + + Log = log.New(file, "", log.LstdFlags|log.Lshortfile) +} + +func validateInputs() bool { + res := make(chan models.Response) + throttle := time.Tick(time.Second / 800) + + go getdata(os.Args[1], res, throttle) + + r := <-res + if r.Err != nil { + return false + } + + go getdata(os.Args[2], res, throttle) + + r = <-res + if r.Err != nil { + return false + } + + return true +} + +func main() { + if len(os.Args) < 3 { + log.Fatal("Usage: degrees ") + } + + ok := validateInputs() + if !ok { + log.Fatal("Invalid inputs") + } + + out := make(chan models.Out) + q := utilities.NewQueue() + q.Enqueue(utilities.WorkRequest{Actor: os.Args[1]}) + + go getrelationship(q, out) + + for { + o := <-out + if o.Err != nil { + if _, ok := o.Err.(models.Skippable); ok { + continue + } + panic(o.Err) + } + + if o.Relationship == nil { + continue + } + + if o.Relationship.Cast2.URL == os.Args[2] { + var result []models.Relationship + result = o.Relationship.Path + result = append(result, *o.Relationship) + + fmt.Println("Degrees of seperation:", len(result)) + fmt.Println() + + for i, r := range result { + fmt.Printf("%d. Movie: %s\n", i+1, r.Movie) + fmt.Println(r.Cast1.Role+":", r.Cast1.Name) + fmt.Println(r.Cast2.Role+":", r.Cast2.Name) + fmt.Println() + } + return + } + + path := make([]models.Relationship, len(o.Relationship.Path)) + copy(path, o.Relationship.Path) + path = append(path, *o.Relationship) + + q.Enqueue(utilities.WorkRequest{Actor: o.Relationship.Cast2.URL, Path: path}) + } +} + +func getdata(id string, res chan<- models.Response, throttle <-chan time.Time) { + Log.Println("fetching", id) + <-throttle + resp, err := http.Get("http://data.moviebuff.com/" + id) + + if resp != nil { + defer resp.Body.Close() + } + + if err != nil { + res <- models.Response{Data: models.Data{}, Err: err} + return + } + + if resp.StatusCode == http.StatusForbidden { + res <- models.Response{Data: models.Data{}, Err: models.Skippable{Reason: "forbidden"}} + return + } + + jsonBlob, err := ioutil.ReadAll(resp.Body) + if err != nil { + res <- models.Response{Data: models.Data{}, Err: err} + return + } + + var data models.Data + err = json.Unmarshal(jsonBlob, &data) + res <- models.Response{Data: data, Err: err} + return +} + +func getrelationship(q *utilities.Queue, output chan<- models.Out) { + visited := map[string]bool{} + throttle := time.Tick(time.Second / 800) + + for { + if q.Empty() { + continue + } + + i := q.Dequeue() + if visited[i.Actor] { + output <- models.Out{} + } + + res := make(chan models.Response) + go getdata(i.Actor, res, throttle) + + actorProfile := <-res + if actorProfile.Err != nil { + if _, ok := actorProfile.Err.(models.Skippable); ok { + output <- models.Out{} + continue + } + output <- models.Out{Err: actorProfile.Err} + continue + } + + visited[i.Actor] = true + + var count int + urlVsMovie := map[string]models.Movie{} + for _, Movie := range actorProfile.Data.Movies { + if visited[Movie.URL] { + continue + } + + go getdata(Movie.URL, res, throttle) + + urlVsMovie[Movie.URL] = Movie + visited[Movie.URL] = true + count++ + } + + for k := 0; k < count; k++ { + resp := <-res + if resp.Err != nil { + if _, ok := resp.Err.(models.Skippable); ok { + continue + } + output <- models.Out{Err: resp.Err} + return + } + + for _, c := range resp.Data.Casts { + if c.URL == i.Actor || visited[c.URL] { + continue + } + + visited[c.URL] = true + + output <- models.Out{ + Relationship: &models.Relationship{ + Cast1: models.Cast{Name: actorProfile.Data.Name, URL: actorProfile.Data.URL, Role: urlVsMovie[resp.Data.URL].Role}, + Cast2: c, + Movie: urlVsMovie[resp.Data.URL].Name, + Path: i.Path, + }, + } + } + + for _, c := range resp.Data.Crew { + if c.URL == i.Actor || visited[c.URL] { + continue + } + + visited[c.URL] = true + output <- models.Out{ + Relationship: &models.Relationship{ + Cast1: models.Cast{Name: actorProfile.Data.Name, URL: actorProfile.Data.URL, Role: urlVsMovie[resp.Data.URL].Role}, + Cast2: c, + Movie: urlVsMovie[resp.Data.URL].Name, + Path: i.Path, + }, + } + } + } + } +} \ No newline at end of file diff --git a/models/configuration.go b/models/configuration.go new file mode 100644 index 0000000..5a36f10 --- /dev/null +++ b/models/configuration.go @@ -0,0 +1,46 @@ +package models +type Cast struct { + Name string `json:"name"` + Role string `json:"role"` + URL string `json:"url"` +} + +type Movie struct { + Name string `json:"name"` + URL string `json:"url"` + Role string `json:"role"` +} + +type Data struct { + URL string `json:"url"` + Type string `json:"type"` + Name string `json:"name"` + Movies []Movie `json:"movies"` + Casts []Cast `json:"cast"` + Crew []Cast `json:"crew"` +} + +type Out struct { + Relationship *Relationship + Err error +} + +type Skippable struct { + Reason string +} + +func (s Skippable) Error() string { + return s.Reason +} + +type Relationship struct { + Cast1 Cast + Cast2 Cast + Movie string + Path []Relationship +} + +type Response struct { + Data Data + Err error +} \ No newline at end of file diff --git a/utilities/utilities.go b/utilities/utilities.go new file mode 100644 index 0000000..955af10 --- /dev/null +++ b/utilities/utilities.go @@ -0,0 +1,41 @@ +package utilities + +import ( + "sync" + "github.com/RealImage/Challenge/models" +) + +type WorkRequest struct { + Actor string + Path []models.Relationship +} + +type Queue struct { + Works []WorkRequest + sync.Mutex +} + +func (q *Queue) Enqueue(w ...WorkRequest) { + q.Lock() + defer q.Unlock() + q.Works = append(q.Works, w...) +} + +//TODO fix empty relationship +func (q *Queue) Dequeue() WorkRequest { + q.Lock() + defer q.Unlock() + rel := q.Works[0] + q.Works = q.Works[1:] + return rel +} + +func (q *Queue) Empty() bool { + q.Lock() + defer q.Unlock() + return len(q.Works) == 0 +} + +func NewQueue() *Queue { + return &Queue{} +} \ No newline at end of file