Skip to content

Instantly share code, notes, and snippets.

@chrislkeller
Created July 7, 2015 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislkeller/1dc352d5c2c1b49a608d to your computer and use it in GitHub Desktop.
Save chrislkeller/1dc352d5c2c1b49a608d to your computer and use it in GitHub Desktop.
Beginning of a class to work through the rap collaboration data...
import logging
import json
logger = logging.getLogger("root")
logging.basicConfig(
format = "\033[1;36m%(levelname)s: %(filename)s (def %(funcName)s %(lineno)s): \033[1;37m %(message)s",
level=logging.DEBUG
)
class SearchJsonFile(object):
target_ids = [
{"rapper": "mc_ren", u'color': u'', u'label': u'MC Ren', u'x': 62.337076030642244, u'y': 46.85040758725747, u'attributes': {u'similar_artists': u'123;46;496;186;7973;291;1423;5687;149;4;404;2931;59;45', u'weight': u'0.000870208413784', u'color': u'd9181e', u'rank': u'305', u'clique': u'0', u'y': u'-236.519226612', u'x': u'-157.636871509'}, u'id': u'1436', u'size': 1},
{"rapper": "easy_e", u'color': u'', u'label': u'Eazy-E', u'x': 60.9474046807051, u'y': -90.79685145142224, u'attributes': {u'similar_artists': u'7973;123;494;495;1436;46;1423;186;217;5687;45;59;4;216', u'weight': u'0.000804139336469', u'color': u'e61113', u'rank': u'342', u'clique': u'0', u'y': u'-261.08484645', u'x': u'-166.504110498'}, u'id': u'496', u'size': 1},
{"rapper": "ice_cube", u'color': u'', u'label': u'Ice Cube', u'x': 66.1530312035062, u'y': -87.43318882743228, u'attributes': {u'similar_artists': u'404;4973;1880;46;42090;7972;6399;5344;1012;123;405;4;3071;2931', u'weight': u'0.00270408792886', u'color': u'9c4150', u'rank': u'25', u'clique': u'0', u'y': u'-205.459842896', u'x': u'-100.439586044'}, u'id': u'186', u'size': 1},
{"rapper": "dre", u'color': u'', u'label': u'Dr. Dre', u'x': -75.48544175983628, u'y': -30.212844559646868, u'attributes': {u'similar_artists': u'46;45;108;149;170;2;4;42;3431;291;496;72;1436;27937', u'weight': u'0.00314635008058', u'color': u'f70505', u'rank': u'16', u'clique': u'0', u'y': u'-57.0063687808', u'x': u'-123.963450098'}, u'id': u'123', u'size': 1},
{"rapper": "yella", u'color': u'', u'label': u'DJ Yella', u'x': 28.547974843246408, u'y': 60.28004423595692, u'attributes': {u'similar_artists': u'496;123;1436;494;495;46;186;45;1423;974;217;1129;4;59', u'weight': u'0.000491866273886', u'color': u'ed0c0e', u'rank': u'683', u'clique': u'0', u'y': u'-249.449412611', u'x': u'-160.503153878'}, u'id': u'7973', u'size': 1},
{"rapper": "the_doc", u'color': u'', u'label': u'The D.O.C.', u'x': 26.5460025848232, u'y': -53.96421965465626, u'attributes': {u'similar_artists': u'123;974;46;186;1436;496;7973;45;3470;1423;4;149;59;170', u'weight': u'0.00027987751911', u'color': u'f00a0c', u'rank': u'1194', u'clique': u'0', u'y': u'-218.705044011', u'x': u'-150.753968662'}, u'id': u'1129', u'size': 1}
]
data_file = "/Volumes/one_tb_hd/_programming/2kpcc/data-tools/extract-nodes/graph.json"
def _init(self, *args, **kwargs):
"""
starts the process a rolling
"""
target_data = self.open_json_file(self.data_file)
#for item in target_data["edges"]:
#if item["sourceID"] == "123":
#if item["targetID"] == "186":
#logger.debug(item)
#for rapper in self.target_ids:
#self.find_artists_similar_to(rapper, target_data["nodes"])
for rapper in self.target_ids:
self.find_artists_connected_to(rapper, target_data["nodes"])
def open_json_file(self, json_file):
with open(json_file) as data_file:
data = json.load(data_file)
return data
def build_rapper_profile(self, nwa_rapper, target_data):
for item in target_data["nodes"]:
if nwa_rapper["id"] == item["id"]:
print item
def find_artists_connected_to(self, nwa_rapper, target_data):
delimited_artist_ids = nwa_rapper["attributes"]["similar_artists"]
list_of_artist_ids = delimited_artist_ids.split(";")
print "* %s" % (nwa_rapper["label"])
for artist_id in list_of_artist_ids:
for item in target_data:
if artist_id == item["id"]:
print "\t* %s" % (item["label"])
else:
pass
print "\n"
def find_artists_similar_to(self, nwa_rapper, target_data):
for item in target_data:
delimited_artist_ids = item["attributes"]["similar_artists"]
list_of_artist_ids = delimited_artist_ids.split(";")
if nwa_rapper["id"] in list_of_artist_ids:
logger.debug("%s - %s" % (nwa_rapper["label"], item["label"]))
else:
pass
if __name__ == "__main__":
task_run = SearchJsonFile()
task_run._init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment