Q1.1 [30 points] Collect data from TMDb and build a graph For this Q1.1, you will be using and submitting a python file. Complete all tasks according to the instructions found in submission.py to...

1 answer below »

See attached




Q1.1 [30 points] Collect data from TMDb and build a graph For this Q1.1, you will be using and submitting a python file. Complete all tasks according to the instructions found in submission.py to complete the Graph class, the TMDbAPIUtils class, and the two global functions. The Graph class will serve as a re-usable way to represent and write out your collected graph data. The TMDbAPIUtils class will be used to work with the TMDB API for data retrieval. NOTE: You must only use a version of Python ≥ 3.7.0 and < 3.8 for this question. this question has been developed, tested for these versions. you must not use any other versions (e.g., python 3.8). while we want to be able to extend to more python versions, the specified versions are what we can definitively support at this time. note: you must only use the modules and libraries provided at the top of submission.py and modules from the python standard library. pandas and numpy cannot be used — while we understand that they are useful libraries to learn, completing this question is not critically dependent on their functionality. in addition, to enable our tas to provide better, more consistent support to our students, we have decided to focus on the subset of libraries. note: we will call each function once in submission.py during grading. the total runtime of submission.py must not exceed 10 minutes. submissions exceeding this limit will receive zero credit. the average runtime of the code during grading is expected to take approximately 4 seconds. when we grade, we will take into account what your code does, and aspects that may be out of your control. for example, sometimes the server may be under heavy load, which may significantly increase the response time (e.g., the closer it is to hw1 deadline, likely the longer the response time!). a. [10 pts] implementation of the graph class according to the instructions in submission.py b. [10 pts] implementation of the tmdbapiutils class according to the instructions in submission.py. you will use version 3 of the tmdb api to download data about actors and their co-actors. to use the tmdb api: create a tmdb account and obtain your clientid/clientsecret which are required to obtain an authentication token. refer to the tmdbapidocumentation as you work on this question.the documentation contains a helpful ‘try-it-out’ feature for interacting with the api calls. c. producing correct nodes.csv and edges.csv. you must upload your nodes.csv and edges.csv files to argo-lite as directed in q1.2. 3.8="" for="" this="" question.="" this="" question="" has="" been="" developed,="" tested="" for="" these="" versions.="" you="" must="" not="" use="" any="" other="" versions="" (e.g.,="" python="" 3.8).="" while="" we="" want="" to="" be="" able="" to="" extend="" to="" more="" python="" versions,="" the="" specified="" versions="" are="" what="" we="" can="" definitively="" support="" at="" this="" time.="" note:="" you="" must="" only="" use="" the="" modules="" and="" libraries="" provided="" at="" the="" top="" of="" submission.py="" and="" modules="" from="" the="" python="" standard="" library.="" pandas="" and="" numpy="" cannot="" be="" used="" —="" while="" we="" understand="" that="" they="" are="" useful="" libraries="" to="" learn,="" completing="" this="" question="" is="" not="" critically="" dependent="" on="" their="" functionality.="" in="" addition,="" to="" enable="" our="" tas="" to="" provide="" better,="" more="" consistent="" support="" to="" our="" students,="" we="" have="" decided="" to="" focus="" on="" the="" subset="" of="" libraries.="" note:="" we="" will="" call="" each="" function="" once="" in="" submission.py="" during="" grading.="" the="" total="" runtime="" of="" submission.py="" must="" not="" exceed="" 10="" minutes.="" submissions="" exceeding="" this="" limit="" will="" receive="" zero="" credit.="" the="" average="" runtime="" of="" the="" code="" during="" grading="" is="" expected="" to="" take="" approximately="" 4="" seconds.="" when="" we="" grade,="" we="" will="" take="" into="" account="" what="" your="" code="" does,="" and="" aspects="" that="" may="" be="" out="" of="" your="" control.="" for="" example,="" sometimes="" the="" server="" may="" be="" under="" heavy="" load,="" which="" may="" significantly="" increase="" the="" response="" time="" (e.g.,="" the="" closer="" it="" is="" to="" hw1="" deadline,="" likely="" the="" longer="" the="" response="" time!).="" a.="" [10="" pts]="" implementation="" of="" the="" graph="" class="" according="" to="" the="" instructions="" in="" submission.py="" b.="" [10="" pts]="" implementation="" of="" the="" tmdbapiutils="" class="" according="" to="" the="" instructions="" in="" submission.py.="" you="" will="" use="" version="" 3="" of="" the="" tmdb="" api="" to="" download="" data="" about="" actors="" and="" their="" co-actors.="" to="" use="" the="" tmdb="" api:="" create="" a="" tmdb="" account="" and="" obtain="" your="" clientid/clientsecret="" which="" are="" required="" to="" obtain="" an="" authentication="" token.="" refer="" to="" the="" tmdbapidocumentation="" as="" you="" work="" on="" this="" question.the="" documentation="" contains="" a="" helpful="" ‘try-it-out’="" feature="" for="" interacting="" with="" the="" api="" calls.="" c.="" producing="" correct="" nodes.csv="" and="" edges.csv.="" you="" must="" upload="" your="" nodes.csv="" and="" edges.csv="" files="" to="" argo-lite="" as="" directed="" in="">
Answered 3 days AfterJan 30, 2021

Answer To: Q1.1 [30 points] Collect data from TMDb and build a graph For this Q1.1, you will be using and...

Vicky answered on Feb 03 2021
144 Votes
import http.client
import json
import csv
#############################################################################################################################
# All instructions, code comments, etc. contained within this notebook are part of the assignment instructions.
# Portions of this file will auto-graded in Gradescope using different sets of parameters / data to ensure that values are not
# hard-coded.
#
# Instructions: Implement all methods in this file that have a return
# value of 'Not
Implemented'. See the documentation within each method for specific details, including
# the expected return value
#
# Helper Functions:
# You are permitted to write additional helper functions/methods or use additional instance variables within
# the `Graph` class or `TMDbAPIUtils` class so long as the originally included methods work as required.
#
# Use:
# The `Graph` class is used to represent and store the data for the TMDb co-actor network graph. This class must
# also provide some basic analytics, i.e., number of nodes, edges, and nodes with the highest degree.
#
# The `TMDbAPIUtils` class is used to retrieve Actor/Movie data using themoviedb.org API. We have provided a few necessary methods
# to test your code w/ the API, e.g.: get_movie_cast(), get_movie_credits_for_person(). You may add additional
# methods and instance variables as desired (see Helper Functions).
#
# The data that you retrieve from the TMDb API is used to build your graph using the Graph class. After you build your graph using the
# TMDb API data, use the Graph class write_edges_file & write_nodes_file methods to produce the separate nodes and edges
# .csv files for use with the Argo-Lite graph visualization tool.
#
# While building the co-actor graph, you will be required to write code to expand the graph by iterating
# through a portion of the graph nodes and finding similar artists using the TMDb API. We will not grade this code directly
# but will grade the resulting graph data in your Argo-Lite graph snapshot.
#
#############################################################################################################################
class Graph:
# Do not modify
def __init__(self, with_nodes_file=None, with_edges_file=None):
"""
option 1: init as an empty graph and add nodes
option 2: init by specifying a path to nodes & edges files
"""
self.nodes = []
self.edges = []
if with_nodes_file and with_edges_file:
nodes_CSV = csv.reader(open(with_nodes_file))
nodes_CSV = list(nodes_CSV)[1:]
self.nodes = [(n[0],n[1]) for n in nodes_CSV]
edges_CSV = csv.reader(open(with_edges_file))
edges_CSV = list(edges_CSV)[1:]
self.edges = [(e[0],e[1]) for e in edges_CSV]
def add_node(self, id: str, name: str)->None:
"""
add a tuple (id, name) representing a node to self.nodes if it does not already exist
The graph should not contain any duplicate nodes
"""
if (id, name) not in self.nodes:
self.nodes.append((id, name))
def add_edge(self, source: str, target: str)->None:
"""
Add an edge between two nodes if it does not already exist.
An edge is represented by a tuple containing two strings: e.g.: ('source', 'target').
Where 'source' is the id of the source node and 'target' is the id of the target node
e.g., for two nodes with ids 'a' and 'b' respectively, add the tuple ('a', 'b') to self.edges
"""
if (source, target) not in self.edges:
self.edges.append((source, target))
def total_nodes(self)->int:
"""
Returns an integer value for the total number of nodes in the graph
"""
return len(self.nodes)
def total_edges(self)->int:
"""
Returns an integer value for the total number of edges in the graph
"""
return len(self.edges)
def max_degree_nodes(self)->dict:
"""
Return the node(s) with the highest degree
Return multiple nodes in the event of a tie
Format is a dict where the key is the node_id and the value is an integer for the node degree
e.g. {'a': 8}
or {'a': 22, 'b': 22}
"""
degree = {}
for (id, name) in self.nodes:
count = 0
for (source, target) in self.edges:
if name in (source, target):
count += 1
degree[name] = count
max_value = max(degree.values())
max_degree = {}
for source, target in degree.items():
if max_value == target:
max_degree[source] = target
return max_degree
def print_nodes(self):
"""
No further implementation required
May be used for de-bugging if necessary
"""
print(self.nodes)
def print_edges(self):
"""
No further implementation required
May be used for de-bugging if necessary
"""
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here