Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Challenge | Software Engineer #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 10 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,17 @@
#Degrees of Separation
# Go Project

With cinema going global these days, every one of the [A-Z]ollywoods are now connected. Use the wealth of data available at [Moviebuff](http://www.moviebuff.com) to see how.
## Overview

Write a Go program that behaves the following way:
This project processes a queue system and requires multiple Go files to run. The command below ensures all necessary files are included in execution.

```
$ degrees amitabh-bachchan robert-de-niro
## Prerequisites

Degrees of Separation: 3
- Install [Go](https://go.dev/dl/) (if not already installed).
- Ensure all required `.go` files are present in the same directory.

1. Movie: The Great Gatsby
Supporting Actor: Amitabh Bachchan
Actor: Leonardo DiCaprio
## Running the Project

2. Movie: The Wolf of Wall Street
Actor: Leonardo DiCaprio
Director: Martin Scorsese
To execute the project, run the following command:

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.
```sh
go run main.go queue.go amitabh-bachchan akshay-kumar
254 changes: 254 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)




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 {
r *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 {
d data
err error
}

func validateInputs() bool {
res := make(chan 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 {
fmt.Print("Usage: degrees <actor-name> <actor-name>")
}

ok := validateInputs()
if !ok {
fmt.Print("Invalid ")
}

out := make(chan out)
q := NewQueue()
q.enqueue(workRequest{actor: os.Args[1]})

go getrelationship(q, out)

for {
o := <-out
if o.err != nil {
if _, ok := o.err.(skippable); ok {
continue
}
panic(o.err)
}

if o.r == nil {
continue
}

if o.r.cast2.URL == os.Args[2] {
var result []relationship
result = o.r.path
result = append(result, *o.r)

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([]relationship, len(o.r.path))
copy(path, o.r.path)
path = append(path, *o.r)

q.enqueue(workRequest{actor: o.r.cast2.URL, path: path})
}
}

func getdata(id string, res chan<- response, throttle <-chan time.Time) {
fmt.Println("fetching", id)
<-throttle
resp, err := http.Get("http://data.moviebuff.com/" + id)

if resp != nil {
defer resp.Body.Close()
}

if err != nil {
res <- response{d: data{}, err: err}
return
}

if resp.StatusCode == http.StatusForbidden {
res <- response{d: data{}, err: skippable{reason: "forbidden"}}
return
}

jsonBlob, err := ioutil.ReadAll(resp.Body)
if err != nil {
res <- response{d: data{}, err: err}
return
}

var data data
err = json.Unmarshal(jsonBlob, &data)
res <- response{d: data, err: err}
return
}

func getrelationship(q *queue, output chan<- out) {
visited := map[string]bool{}
throttle := time.Tick(time.Second / 800)

for {
if q.empty() {
continue
}

i := q.dequeue()
if visited[i.actor] {
output <- out{}
}

res := make(chan response)
go getdata(i.actor, res, throttle)

actorProfile := <-res
if actorProfile.err != nil {
if _, ok := actorProfile.err.(skippable); ok {
output <- out{}
continue
}
output <- out{err: actorProfile.err}
continue
}

visited[i.actor] = true

var count int
urlVsMovie := map[string]movie{}
for _, movie := range actorProfile.d.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.(skippable); ok {
continue
}
output <- out{err: resp.err}
return
}

for _, c := range resp.d.Casts {
if c.URL == i.actor || visited[c.URL] {
continue
}

visited[c.URL] = true

output <- out{
r: &relationship{
cast1: cast{Name: actorProfile.d.Name, URL: actorProfile.d.URL, Role: urlVsMovie[resp.d.URL].Role},
cast2: c,
movie: urlVsMovie[resp.d.URL].Name,
path: i.path,
},
}
}

for _, c := range resp.d.Crew {
if c.URL == i.actor || visited[c.URL] {
continue
}

visited[c.URL] = true
output <- out{
r: &relationship{
cast1: cast{Name: actorProfile.d.Name, URL: actorProfile.d.URL, Role: urlVsMovie[resp.d.URL].Role},
cast2: c,
movie: urlVsMovie[resp.d.URL].Name,
path: i.path,
},
}
}
}
}
}
40 changes: 40 additions & 0 deletions queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"sync"
)

type workRequest struct {
actor string
path []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...)
}


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{}
}