def CreateChemicalPathwayRelationship(file="data/CTD_chem_pathways_enriched.csv"): head=["ChemicalName","ChemicalID","CasRN","PathwayName","PathwayID","PValue","CorrectedPValue","TargetMatchQty","TargetTotalQty","BackgroundMatchQty","BackgroundTotalQty"] relcount = 0 graph = Graph("http://localhost:7474") with open(file, mode='r',encoding='utf-8') as fr: reader = csv.reader(fr) for line in reader: if not line: break if not line[0].startswith("#"): line_dict = dict(zip(head, line)) line_dict["ChemicalID"] = "MESH:" + line_dict["ChemicalID"] # find chemical ChemiExitNode = graph.nodes.match("Chemical", id=line_dict["ChemicalID"]).first() if not ChemiExitNode: continue # find pathway PathwayExitNode = graph.nodes.match("Pathway", id=line_dict["PathwayID"]).first() if not PathwayExitNode: continue # create relationship ChemiRelation = Relationship(ChemiExitNode, 'CInteractionP', PathwayExitNode, **line_dict) graph.create(ChemiRelation) relcount += 1 if relcount % 100 == 0: print(relcount)
def createGraph(self): """ form: (self) --> print description: function that creates the neo4j graph exemple: >>> graphWN..createGraph() creating graph... graph created """ print "creating graph..." graph = Graph() graph.delete_all() for synset in self.synset2synonym: word_node = Node("word", literal=self.synset2word[synset]) #print synset, self.synset2synonym[synset] #if graph.find(self.synset2word[synset])!=None: #print "Exist" #word_node=graph.find_one("word", 'literal', self.synset2word[synset]) #print word_node synset_node = Node("synset", name=synset) word_has_synset = Relationship(word_node, "has_synset", synset_node) if self.synset2synonym[synset][0]!='_EMPTY_': for synonym in self.synset2synonym[synset]: word_syn = Node("word", literal=synonym) synset_has_synonym = Relationship(synset_node, "has_synonym", word_syn) graph.create(synset_has_synonym) graph.create(word_has_synset) print "graph created"
def CreateChemicalDiseaseRelationship(file="data/CTD_chemicals_diseases.csv"): head=["ChemicalName","ChemicalID","CasRN","DiseaseName","DiseaseID","DirectEvidence","InferenceGeneSymbol","InferenceScore","OmimIDs","PubMedIDs"] headarray=["DirectEvidence","OmimIDs","PubMedIDs"] relcount = 0 graph = Graph("http://localhost:7474") with open(file, mode='r',encoding='utf-8') as fr: reader = csv.reader(fr) for line in reader: if not line: break if not line[0].startswith("#") and line[7]!="" and float(line[7])>=8: line_dict = dict(zip(head, line)) line_dict["ChemicalID"] = "MESH:" + line_dict["ChemicalID"] for elearray in headarray: line_dict[elearray] = line_dict[elearray].strip().split('|') # find chemical ChemiExitNode = graph.nodes.match("Chemical", id=line_dict["ChemicalID"]).first() if not ChemiExitNode: continue # find disease DiseaseExitNode = graph.nodes.match("Disease", id=line_dict["DiseaseID"]).first() if not DiseaseExitNode: continue # create relationship ChemiRelation = Relationship(ChemiExitNode, 'CInteractionD', DiseaseExitNode, **line_dict) graph.create(ChemiRelation) relcount += 1 if relcount % 100 == 0: print(relcount)
class PersonLoader(object): def __init__(self): authenticate("localhost:7474", "neo4j", "1111") self.graph_db = Graph(graph_DB_url) self.api_url = "http://127.0.0.1:8000/api/v1/person/?format=json" self.statement = "MERGE (n:Person {Name:{N}, Identification:{I}, id: {ID}, NonResidentForeigner:{NR}," \ "MoreInformation:{MI}}) RETURN n" def whipUp(self): objects = json.load(urlopen(self.api_url))["objects"] for object in objects: args = {} args["ID"]=object["id"] args["I"]=object["Identification"] args["NR"]=object["NonResidentForeigner"] args["N"]=object["Name"] args["MI"]=object["MoreInformation"] #args["AD"]=object["Address"]["id"] perCh = getPerson(Name=args["N"]) if perCh!=None: continue db = self.graph_db.cypher.begin() db.append(self.statement, args) db.commit() address = getAddress(id=int(object["Address"]["id"])) person = getPerson(id=int(args["ID"])) self.graph_db.create(rel(person.one, "LIVED", address.one))
def entity(request): graph = Graph("bolt://10.60.42.201:7687", username="******", password="******") if request.method == "POST": file_obj = request.FILES.get("up_file") print(file_obj.name) # f1 = open(file_obj.name, "wb")' string = "" for i in file_obj.chunks(): string += str(i, encoding="utf-8") datas = string.split(".\n") for d in datas: d = str(d).split(' ') tmp = [] for item in d: item = item.split('/') tmp.append(item[-1][:-1]) node1 = Node(name=tmp[0]) node2 = Node(name=tmp[2]) prop = Relationship(node1, tmp[1], node2) graph.create(prop) return render(request, 'ETL.html')
def create_business_using_csv(self, filename): graph = Graph(self.url + '/db/data/') with open(filename, mode='r') as csv_file: csv_reader = csv.reader(csv_file) line_count = 0 for i, row in enumerate(csv_reader): if i == 0: continue business_id = row[0] name = row[1] address = row[3] city = row[4] state = row[5] postal_code = row[6] latitude = row[7] longitude = row[8] Category = row[12] # Yelp_Business object business = Yelp_Business(business_id, name, Category, longitude, latitude, address, city, state, postal_code) business_Node = Node("Yelp_Business", Business_id=business.business_id, Business_name=business.business_name, Address=business.address, City=business.city, State=business.state, Postal_code=business.postal_code, Latitude=business.latitude, Longitude=business.longitude, Category=business.category) graph.create(business_Node)
class MyGraph(object): def __init__(self): self.graph = Graph('bolt://localhost:7687', user='******', password='******') self.mather = NodeMatcher(self.graph) self.node_name = 'JIKE_user' self.FOLLOWER_REL = 'FOLLOWER' self.FOLLOWING_REL = 'FOLLOWING' def add_a_rel(self, node_a, node_b, rel_type='FOLLOWER'): ab = Relationship(node_a, rel_type, node_b) self.graph.create(ab) def add_a_person(self, user): if self.search_a_person(username=user.username): return node = Node(self.node_name, **user.to_dict()) self.graph.create(node) def search_a_person(self, **properties): return self.mather.match(self.node_name, **properties).first() def flush(self): self.graph.delete_all()
class Graph(object): def __init__(self, neo4j_uri): self.graph = NeoGraph(neo4j_uri) def find_node(self, label, node_id): args = dict(property_key="node_id", property_value=node_id) return self.graph.find_one(label, **args) def create_user(self, args): node = self.find_node("User", args["username"]) if not node: properties = dict( node_id=args["username"], name=args["name"], city=args["city"] ) node = Node("User", **properties) self.graph.create(node) return node, True return node, False def delete_user(self, user): node = self.find_node("User", user) if node: self.graph.delete(node) return True return False
def CreateOMIM_DO_RELATIONSHIP(): RelCount = 0 ODgraph = Graph("http://localhost:7474") resultDOs = ODgraph.run('MATCH (n:DO) RETURN n').data() for do in resultDOs: doNode = do["n"] if "xref" in doNode: xref = doNode["xref"] for ele in xref: if "OMIM" in ele: #get OMIM node exitOMIMNodelist = list(ODgraph.nodes.match("OMIM", id=ele)) if len(exitOMIMNodelist) == 0: #no omim name, couldn't create node continue else: currentOMIMNode = exitOMIMNodelist[0] # get DO node exitDONodelist = list( ODgraph.nodes.match("DO", id=doNode["id"])) if len(exitDONodelist) == 0: continue else: currentDONode = exitDONodelist[0] #create relationship if currentOMIMNode and currentDONode: DO_OMIM_relation = Relationship( currentDONode, 'Association', currentOMIMNode) ODgraph.create(DO_OMIM_relation) RelCount += 1 if RelCount % 100 == 0: print(RelCount)
class SocialFormationLoader(object): def __init__(self): authenticate("localhost:7474", "neo4j", "1111") self.graph_db = Graph(graph_DB_url) self.api_url = "http://127.0.0.1:9000/api/v1/socialformation/?format=json" self.statement = "MERGE (n:SocialFormation {id: {ID}, Name:{N}, DateReg:{DR}, RegNumb:{RN}}) RETURN n" def whipUp(self): objects = json.load(urlopen(self.api_url))["objects"] for object in objects: args = {} args["ID"]=object["id"] args["N"]=object["Name"] args["DR"]=object["DateReg"] args["RN"]=object["RegNumb"] #print args db = self.graph_db.cypher.begin() db.append(self.statement, args) db.commit() sf = getSocialFormation(args["ID"]) adr = getAddress(id = int(object["Address"]["id"]), Street = object["Address"]["Street"]) #per = getPerson(id = int(object["Person"]["id"]), Name = (object["Person"]["Surname"] + " " + object["Person"]["Name"])) per = getPerson(Name = (object["Person"]["Surname"] + " " + object["Person"]["Name"])) if adr != None: self.graph_db.create(rel(sf.one, "HAVE_ADDRESS", adr.one)) if per != None: self.graph_db.create(rel(sf.one, "SF_HAVE_PERSON", per.one))
class KnowledgeGraph(object): def __init__(self): conn_neo4j = ConnectionNeo4j() self._graph = Graph(host=conn_neo4j.ip, auth=(conn_neo4j.username, conn_neo4j.password)) def __repr__(self): self._object = "[INFO] The neo4j version is {}.".format( py2neo.__version__) def load_file(self, cypher): self._graph.run(cypher) def add_node(self, labels, **kwargs): node = Node(labels, **kwargs) self._graph.create(node) def delete_node(self): self._graph.delete_all() # def find(self, label): # return self._graph.find_one(label=label) def find(self): data = self._graph.data('MATCH (p:F**K) return p') df = pd.DataFrame(data) print(df) def match(self): pass
def CreateChemicalEntity(file='data/CTD_chemicals.csv'): nodecount = 0 head = [ "name", "id", "CasRN", "Definition", "ParentIDs", "TreeNumbers", "ParentTreeNumbers", "Synonyms", "DrugBankIDs" ] type = "Chemical" Cgraph = Graph("http://localhost:7474") with open(file, mode='r', encoding='utf-8') as fr: while True: line = fr.readline() if not line: break if not line.startswith("#"): line_list = line.strip('\n').split(',') line_dict = dict(zip(head, line_list)) Parentslist = line_dict["ParentIDs"].strip().split('|') line_dict["ParentIDs"] = Parentslist #create node NewNode = Node(type, **line_dict) Cgraph.create(NewNode) nodecount += 1 if nodecount % 100 == 0: print(nodecount)
def store_in_neo4j(triple): from py2neo import Graph, Node, Relationship ,NodeSelector graph = Graph('http://52.83.213.55:7474',user ='******',password = '******') # graph = Graph('http://localhost:7474',user = '******',password='******') select = NodeSelector(graph) # 加载entity with open('entity.pkl','rb') as f: entities = pickle.load(f) entities = list(flatten(entities)) # 添加所有实体为结点 for en in entities: node = Node('Entity',name= en) graph.create(node) # 遍历三元组,添加节点的属性,结点间关系等 for en, kw in triple.items(): node_1 = select.select('Entity').where(name = en).first() for item in kw: if item[1] in triple.keys(): node_2 = select.select('Entity').where(name = item[1]).first() relate = Relationship(node_1,item[0],node_2) graph.create(relate) else: node_1[item[0]] = item[1] graph.push(node_1) print('数据存储完毕')
class Grapher: def __init__(self): self.graph = Graph() """Accepts a dict of scanning results and adds the server, its ports and vulerabilities in Neo4jDB""" def plot_scan_results(self, res): for host in res.keys(): hostname = res[host][KEY_HOSTNAME] server = Node(SERVER, id=host, address=host, hostname=hostname) for attr in res[host].keys(): if attr not in top_keys: for portno in res[host][attr]: if res[host][attr][portno].get(KEY_STATE, "closed") == OPEN: product = res[host][attr][portno][KEY_PRODUCT] version = res[host][attr][portno][KEY_VERSION] cpe = res[host][attr][portno][KEY_CPE] vulnerabilities = res[host][attr][portno][KEY_VULNERABILITIES] port = Node(PORT, id=portno, number=portno, protocol=attr, product=product, version=version, cpe=cpe, state=OPEN) server_has_port = Relationship(server, HAS, port) self.graph.create(server_has_port) for vulnerability in vulnerabilities: published = vulnerability[KEY_PUBLISHED] cve = vulnerability[KEY_CVE] summary = vulnerability[KEY_SUMMARY] vuln = Node(VULNERABILITY, id=cve, cve=cve, summary=summary, published=published) port_has_vuln = Relationship(port, HAS, vuln) self.graph.create(port_has_vuln)
def moveContentSummaryTable(): graph = Graph() lids = session.execute( "SELECT DISTINCT learner_id from learnercontentsummary") for lid in lids: uid = lid['learner_id'] print("** learner:", uid) # content_id text, interactions_per_min double, #num_of_sessions_played int, #time_spent double, node = Node("Learner", id=uid) graph.merge(node, "Learner", "id") contentDict = session.execute( "SELECT * from learnercontentsummary WHERE learner_id='" + uid + "'")[0] cid = contentDict['content_id'] tsp = contentDict['time_spent'] ipm = contentDict['interactions_per_min'] node2 = Node("Content", id=cid) graph.merge(node2, "Content", "id") # add a relationship with property score graph.create( Relationship(node, "INTERACTED_WITH", node2, timeSpent=tsp, ipm=ipm)) print('content: ', cid, 'tsp: ', tsp, 'ipm', ipm)
class Neo4jConnector: _graph_connection = None def __init__(self, connectionString=None): # set up authentication parameters authenticate("localhost:7474", "neo4j", "123") self.connectionString = "http://localhost:7474/db/data/" self._graph_connection = Graph(connectionString) self.error_file = open("Dump/log/dberror.txt", "a") return def createNodes(self, wikiPage): print ("creating node %s" %wikiPage.getTitle()) try: if self._graph_connection is not None: alice = Node("article2",name = wikiPage.title,content= wikiPage.getContent()) self._graph_connection.create(alice) else: self.error_file.write("create node failed: connection not avaialable".encode('utf-8')) print 'create node failed: connection not avaialable' return except Exception, e: self.error_file.write('Search failed: {%s} { %s } \n' % (wikiPage.getTitle(), e.message)) print 'create node failed: %s %s' % (e.message,e.args) pass
def getTestedNeo4jDB(graphDBurl, graphDbCredentials): '''Gets a Neo4j url and returns a GraphDatabaseService to the database after having performed some trivial tests''' try: if graphDbCredentials: authenticate(*graphDbCredentials) graphDb = Graph(graphDBurl) #just fetch a Node to check we are connected #even in DRY RUN we should check Neo4j connectivity #but not in OFFLINE MODE if not OFFLINE_MODE: _ = iter(graphDb.match(limit=1)).next() except StopIteration: pass except SocketError as ex: raise DbNotFoundException(ex, "Could not connect to Graph DB %s." % graphDBurl) if not DRY_RUN and not OFFLINE_MODE: try: test_node = Node("TEST", data="whatever") graphDb.create(test_node) graphDb.delete(test_node) except Exception as ex: raise DBInsufficientPrivileges(\ "Failed on trivial operations in DB %s." % graphDBurl) return graphDb
def save_neo4j(): graph = Graph(host="127.0.0.1", user="******", password="******") print("正在创建游戏发行商节点。。") # 创建发行商节点 productor_names = set(data['product_designer'].tolist()) for productor_name in tqdm(productor_names): productor_node = Node("Productor", name=productor_name) graph.create(productor_node) # 创建游戏节点以及与发行商的关系 print("正在创建游戏节点以及与发行商的关系。。") matcher = NodeMatcher(graph) for i in tqdm(data.index): row = dict(zip(keys, data.loc[i].values[0:-1])) row['game_id'] = int(row['game_id']) row['game_rating'] = int(row['game_rating']) # 游戏节点 productor_name = row.pop('product_designer') productor_node = matcher.match("Productor", name=productor_name).first() game_node = Node("Game") game_node.update(row) # 建立关系 relation = Relationship(productor_node, "发行", game_node) node_re = productor_node | game_node | relation graph.create(node_re)
def createGraph(self): """ form: (self) --> print description: function that creates the neo4j graph exemple: >>> graphWN..createGraph() creating graph... graph created """ print "creating graph..." graph = Graph() graph.delete_all() for synset in self.synset2synonym: word_node = Node("word", literal=self.synset2word[synset]) #print synset, self.synset2synonym[synset] #if graph.find(self.synset2word[synset])!=None: #print "Exist" #word_node=graph.find_one("word", 'literal', self.synset2word[synset]) #print word_node synset_node = Node("synset", name=synset) word_has_synset = Relationship(word_node, "has_synset", synset_node) if self.synset2synonym[synset][0] != '_EMPTY_': for synonym in self.synset2synonym[synset]: word_syn = Node("word", literal=synonym) synset_has_synonym = Relationship(synset_node, "has_synonym", word_syn) graph.create(synset_has_synonym) graph.create(word_has_synset) print "graph created"
class Neo4jGraph: def __init__(self, uri: str, auth: Tuple[str, str] ): self._graph = Graph(uri=uri, auth=auth) def commit_relation(self, src: Dict[str, str], rel: Dict[str, str], dst: Dict[str, str], ) -> None: srckind = src['kind'] srcnode = Node( srckind, **{k: v for k, v in src.items() if k != 'kind'}) dstkind = dst['kind'] dstnode = Node( dstkind, **{k: v for k, v in dst.items() if k != 'kind'}) relkind = rel['kind'] relationship = Relationship(srcnode, relkind, dstnode, **{k: v for k, v in rel.items() if k != 'kind'}) self._graph.merge(srcnode, "Author", "name") self._graph.create(dstnode) self._graph.create(relationship) def run(self, query: str): return self._graph.run(query)
def getTestedNeo4jDB(graphDBurl, graphDbCredentials): '''Gets a Neo4j url and returns a GraphDatabaseService to the database after having performed some trivial tests''' try: if graphDbCredentials: authenticate(*graphDbCredentials) graphDb = Graph(graphDBurl) #just fetch a Node to check we are connected #even in DRY RUN we should check Neo4j connectivity #but not in OFFLINE MODE if not OFFLINE_MODE: _ = iter(graphDb.match(limit=1)).next() except StopIteration: pass except SocketError as ex: raise DbNotFoundException( ex, "Could not connect to Graph DB %s." % graphDBurl) if not DRY_RUN and not OFFLINE_MODE: try: test_node = Node("TEST", data="whatever") graphDb.create(test_node) graphDb.delete(test_node) except Exception as ex: raise DBInsufficientPrivileges(\ "Failed on trivial operations in DB %s." % graphDBurl) return graphDb
def upload(data): from py2neo import Node, Relationship from py2neo import Graph graph = Graph("http://localhost:7474", username="******", password="******") graph.delete_all() nodes = [] for item in data: concept, pronunciation, pos2definition = extract_item_properties(item) node_tmp = Node("Prosthodontics", name=concept) node_tmp.properties["pronunciation"] = pronunciation cnt = 1 for pos2def in pos2definition: node_tmp.properties["pos " + str(cnt)] = pos2def["pos"] # node_tmp.properties["definition "+str(cnt)]=pos2def["definition"] for attribute, value in pos2def["attributes"].iteritems(): node_tmp["def " + str(cnt) + " : " + attribute] = value graph.create(node_tmp) nodes.append(node_tmp) print "nodes create over , relation start to create" for node1 in nodes: properties = node1.properties.keys() for property in properties: if property[8:] == "cross_reference": for node2 in nodes: if node2.properties["name"] == node1[property]: graph.create( Relationship(node1, "cross_reference", node2)) print "graph create over"
def moveProficiencyTable(): # get a list of all unique learners # neo4j graph connector graph = Graph() lids = session.execute( "SELECT DISTINCT learner_id from learnerproficiency") for lid in lids: # get the knowledge state for this guy # <concept-id>,<socre> in schema uid = lid['learner_id'] # create a learner node node = Node("Learner", id=uid) graph.merge(node, "Learner", "id") print("** learner:", uid) profDict = session.execute( "SELECT proficiency from learnerproficiency WHERE learner_id='" + uid + "'")[0]['proficiency'] for cid, score in profDict.items(): print("concept:", cid, "score", score) # create/find concept node node2 = Node("Concept", id=cid) graph.merge(node2, "Concept", "id") # add a relationship with property score graph.create(Relationship(node, "ASSESSED_IN", node2, score=score))
class Test: def __init__(self): self.L1 = ['C语言','数组'] self.g = Graph("http://localhost:7474", username="******", password="******") self.g.delete_all() '''建立节点''' def create_node(self, label, nodes): for node_name in nodes: node = Node(label, name=node_name) self.g.create(node) print(len(nodes)) return def createRels(self, start_node, end_node, edges, rel_type, rel_name): count = 0 p = edges[0] q = edges[1] query = "match(p:%s),(q:%s) where p.name='%s'and q.name='%s' create (p)-[rel:%s{name:'%s'}]->(q)" % (start_node, end_node, p, q, rel_type, rel_name) try: self.g.run(query) count += 1 print(rel_type, count, all) except Exception as e: print(e)
class Data(object): def __init__(self): self.g = Graph(host="127.0.0.1", http_port=7474, user="******", password="******") self.matcher = NodeMatcher(self.g) def clear(self): self.g.delete_all() def first_init(self): self.g.delete_all() matcher = NodeMatcher(self.g) with open('aaa.txt', encoding='utf-8') as f: for i in f.readlines(): i = i.strip() r = m.match(i) if r: print(r.groups()) #('A1 -> A2', '是') or ('A1', '潮流收敛调整') r2 = m2.match(r.group(1)) if r2: #print(r2.groups()) #('A1', 'A3') n1 = matcher.match(r2.group(1)).first() n2 = matcher.match(r2.group(2)).first() tempN = Relationship(n1, r.group(2), n2) else: tempN = Node(r.group(1), name=r.group(2)) self.g.create(tempN)
def createRelationships(): global relationships graph = Graph('http://localhost:7474/db/data') for r in relationships: NodeA = graph.find_one(r["start"]["collection"],property_key = "_id", property_value = str(r["start"]["_id"])) NodeB = graph.find_one(r["end"]["collection"],property_key = "_id", property_value = str(r["end"]["_id"])) graph.create(rel(NodeA,r["name"],NodeB))
def moveRelevancyTableAll(): graph = Graph() # get a list of all unique learners lids = session.execute( "SELECT DISTINCT learner_id from learnerconceptrelevance") for lid in uids: # get the knowledge state for this guy # <concept-id>,<score> in schema uids = [lid['learner_id'] for lid in lids] node = Node("Learner", id=uid) graph.merge(node, "Learner", "id") print("** learner:", uid) relDict = session.execute( "SELECT relevance from learnerconceptrelevance WHERE learner_id='" + uid + "'")[0]['relevance'] for cid, score in relDict.items(): #print("concept:",cid,"score",score) # create a node, if it does not exist # else, merge with it node2 = Node("Concept", id, cid) graph.merge(node2, "Concept", "id") # add a relationship with property score graph.create(Relationship(node2, "RELEVANT_TO", node, score=score))
class MedicalGraph: def __init__(self): self.g = Graph( host='127.0.0.1', http_port=7474, user='******', password='******') def generate_nodes(self, nodes): for node in nodes: neo4j_node = Node(node[2], name=node[1], id=node[0]) self.g.create(neo4j_node) def generate_relation(self, relations): for relation in relations: startName = relation[0] endName = relation[1] startType = relation[2] endType = relation[3] relType = relation[4] query = "match(p:`%s`),(q:`%s`) where p.name='%s'and q.name='%s' create (p)-[rel:%s]->(q)" % ( startType, endType, startName, endName, relType) try: self.g.run(query) except Exception as e: print(e)
def merge(self, graph: Graph): """ :code:`MERGE` the relationship in the graph. :param graph: A py2neo.Graph instance. """ if not self.exists(graph): graph.create(self._relationship)
def merge(self, graph: Graph) -> None: """ :code:`MERGE` the node in the graph. :param graph: A py2neo.Graph instance. """ if not self.exists(graph): graph.create(self._node)
def createGraph(): global relationships graph = Graph() graph.delete_all() visualize() for relationship in relationships: graph.create(relationship)
class BaikeGraph: def __init__(self): cur_dir = '/'.join(os.path.abspath(__file__).split('/')[:-1]) self.cur_dir = cur_dir self.g = Graph( host="10.37.2.248", http_port=7474, user="******", password="******") self.matcher = NodeMatcher(self.g) #self.rel_matcher = RelationshipMatcher(self.g) def nodeExist(self,lable,iid): dict_id = {"iid":iid} m = self.matcher.match(lable, **dict_id).first() if m is None: return False else: return True def nodeExist_new(self, lable,iid): query = "MATCH (n:%s) where n.iid='%s' RETURN n"%(lable,iid) try: m = self.g.run(query).data() if m: return True else: return False except Exception as e: print(e) return False def create_baike_node(self, baike_infos): node = Node("Baike6", **baike_infos) if not self.g.exists(node): self.g.create(node) return def relExist(self,start_iid,rel,end_iid): query = "MATCH p=(n:Baike4{vid:'%s'})-[r:`%s`]->(m:Baike4{vid:'%s'}) return p"%(start_iid,rel,end_iid) try: m=self.g.run(query).data() if m: return True else: return False except Exception as e: print(e) return False def create_relationship(self, start_node_vid, start_node_iid,end_node_vid, end_node_iid,rel, start_node="Baike6", end_node="Baike6"): query = "match(p:%s),(q:%s) where p.vid='%s' and p.iid='%s' and q.vid='%s' and q.iid='%s' merge (p)-[rel:%s{name:'%s'}]->(q)" % ( start_node, end_node, start_node_vid, start_node_iid, end_node_vid,end_node_iid,rel, rel) try: self.g.run(query) except Exception as e: print(e) return
def put_data_frame_in_db(df): graph = Graph(password="******") for row in df.itertuples(): user = Node('User', id=row.user.item()) deal = Node('Deal', id=row.deal.item()) graph.merge(user) graph.merge(deal) user.push() graph.create(Relationship(user, "rates", deal, rating=row.rating))
def craw(slef, root_url): html_content = slef.download(root_url) soup1 = BeautifulSoup(html_content, 'html.parser', from_encoding='utf-8') #links_1 = soup1.find('div',id="allCategoryHeader").find_all('li',class_="stitle") links_1 = soup1.find_all('li', class_="stitle") print len(links_1) print type(links_1) graphDB = Graph("http://127.0.0.1:7474", username="******", password="******") for link in links_1: try: data_1 = link.find('a').find('h4').get_text() print(data_1) links_2 = link.find('div', class_="category").find_all('dl') for d_1 in links_2: try: data_2 = d_1.find('dt').find('a').get_text() print(data_2) links_3 = d_1.find('dd').find_all('a') for e_m in links_3: try: data_3 = e_m.get_text() url_3 = e_m['href'] ''' driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "vnique")) # basic auth with: driver = GraphDatabase.driver('bolt://localhost', auth=basic_auth("<user>", "<pwd>")) db = driver.session() db.run("CREATE ({name}:Category_111 {name: {name}, url: {url}})",{"name": data_3, "url": url_3}) db.close() ''' node = Node("Category_111", name=data_3, url=url_3, completed=0) graphDB.create(node) print(data_3) print(url_3) except Exception as err: print(err) continue except Exception as err: print(err) continue except Exception as err: print(err) continue
def change(): if request.method == 'POST': graph = Graph('http://localhost:7474', username='******', password='******') user_change1 = request.form.get("change_company") user_change2 = request.form.get("change_position") user_change3 = request.form.get("change_name") user_change4 = request.form.get("new_name") # user_changeposition = Position.match(graph, user_change2).first() # user_changenode1 = Shareholder.match(graph, user_changerel1).first() # user_changenode2 = Company.match(graph, user_changerel2).first() matcher = NodeMatcher(graph) company_node = matcher.match("Company", name=user_change1).first() position_node = matcher.match("Position", name=user_change2, company=user_change1).first() name_node = matcher.match("name", name=user_change3).first() graph.delete(name_node) new_node = Node("name", name=user_change4) new_rel = Relationship(new_node, "属于", position_node) total_change = new_rel | new_node graph.create(total_change) ''' # 单节点对应属性修改 if len(user_change1) > 0: if user_change2 == "height": user_changenode.height = user_change3 if user_change2 == "age": user_changenode.age = user_change3 graph.push(user_changenode) # 节点关系修改 if len(user_changerel1) > 0: if user_changerel3 == "经营": user_changenode1.manager.remove(user_changenode2) graph.push(user_changenode1) relationship = Relationship(x1, user_changerel4, x2) graph.create(relationship) if user_changerel3 == "股东": user_changenode1.shareholder.remove(user_changenode2) graph.push(user_changenode1) relationship = Relationship(x1, user_changerel4, x2) graph.create(relationship) ''' return render_template('change.html')
class Person(object): def __init__(self): self.graph = Graph() #do nothing def createPerson(self, name, fullname, occupation, homepage, wikipedia_link): person = Node('Person', name=name, fullname=fullname, occupation=occupation, homepage=homepage, wikipedia_link=wikipedia_link) self.graph.create(person) return person
class Neo4j: def __init__(self, **kwargs): self.kwargs = {k: v for k,v in kwargs.items()} self.graph = Graph(self.kwargs.get("neo4j_url", "bolt:localhost:7687"), auth=('neo4j', '')) self.data_object = self.kwargs.get('data_object') # method calls if self.kwargs.get('graph_init'): self.db_config() def db_config(self): """ create data model indices and constraints (i.e. unique indices) """ print("***** Create database constraints *****") self.graph.run('MATCH (n) DETACH DELETE(n)') # delete all objects from the self.graph.run('CREATE CONSTRAINT ON (s:Senator) ASSERT s.id IS UNIQUE') self.graph.run('CREATE CONSTRAINT ON (b:Bill) ASSERT b.roll_call IS UNIQUE') self.graph.run('CREATE CONSTRAINT ON (t:Tweet) ASSERT t.id IS UNIQUE') self.create_senators(self.data_object.senators) self.create_bills(self.data_object.bills) self.create_relations(self.data_object.bill_vote_results) def query(self, cypher_query): try: return self.graph.run(cypher_query).to_table() except Exception as e: if self.kwargs.get('debug'): print(traceback.format_exc()) def create_senators(self, senators): """ create senator graph objects in neo4j """ print("***** creating senator nodes *****") [self.graph.create(Node('Senator', senator['party'], id=senator['id'], first_name=senator['first_name'], last_name=senator['last_name'], gender=senator['gender'], party=senator['party'], twitter_account=senator['twitter_account'], state=senator['state'])) for senator in senators] def create_bills(self, bills): """ create bill graph objects in neo4j """ print("***** creating bill nodes *****") [self.graph.create(Node('Bill', bill['result'], name=bill['id'], roll_call=bill['roll_call'], title=bill['title'], date=bill['date'], d_yes=bill['d_yes'], d_no=bill['d_no'], r_yes=bill['r_yes'], r_no=bill['r_no'], i_yes=bill['i_yes'], i_no=bill['i_no'])) for bill in bills] def create_relations(self, relation): """ create graph relationship of (senator)-[:votes]->(bill) """ print("***** creating relationships *****") for rel in relation: roll_call = rel['roll_call'] yes_votes = rel['yes'] no_votes = rel['no'] for vote in yes_votes: cmd = "MATCH (b:Bill {roll_call:{roll_call}}), (s:Senator {id:{vote}}) CREATE (s)-[:voted_yes]->(b)" self.graph.run(cmd, roll_call=roll_call, vote=vote) for vote in no_votes: cmd = "MATCH (b:Bill {roll_call:{roll_call}}), (s:Senator {id:{vote}}) CREATE (s)-[:voted_no]->(b)" self.graph.run(cmd, roll_call=roll_call, vote=vote)
def updateEntityLabel(entityMap): graph = Graph('http://localhost:7474', username='******', password='******') res = graph.run("MATCH (n {name:\"" + entityMap.get('entity') + "\"}) SET n.newLabel=\"" + entityMap.get('entity_type') + "\" RETURN n").data() if len(res) <= 0: a = Node(entityMap.get('entity_type'), name=entityMap.get('entity')) graph.create(a) print("插入新实体") else: print("修改完成")
def create_category_using_csv(self, filename): graph = Graph(self.url + '/db/data/') with open(filename, mode='r') as csv_file: csv_reader = csv.reader(csv_file) line_count = 0 for i, row in enumerate(csv_reader): if i == 0: continue category_name = row[0] category = Node("Yelp_Category", Category_name=category_name) graph.create(category)
def add2neo(courses): authenticate("localhost:7474", 'neo4j', 'admin') graph = Graph() for course in courses: name = course.prefix+course.number for pre in course.prereqs: print 'adding rel', name, pre try: graph.create(Relationship(courseNodes[name]['node'], 'REQUIRES', courseNodes[pre]['node'])) except: print 'could not add', name, pre
def main1(): authenticate("localhost:7474", "neo4j", "1234") graph = Graph(GRAPH_CONNECTION_STRNIG) graph.delete_all() banana = Node("Fruit", name="banana", colour="yellow", tasty=True) graph.create(banana) t = graph.merge_one("Fruit", 'name', 'apple') t['colour'] = 'green' t['tasty'] = True t.push()
class AgoraOrganization(object): def __init__(self): self.name = None self.unique_id = None self.mission_statement = None self.email = None self.is_open = False self.is_invite_only = False self.website = None self.graph_db = Graph() @property def org_node(self): return self.graph_db.find_one(AgoraLabel.ORGANIZATION, property_key='name', property_value=self.name) @property def org_members(self): """ list of the members of the organization :return: list of tuple of member name, email """ org_members_nodes = self.graph_db.match(start_node=self.org_node, rel_type=AgoraRelationship.MEMBER_OF, end_node=None) org_members_list = [] for item in org_members_nodes: org_members_list.append((item.end_node["name"], item.end_node["email"])) return org_members_list def create_organization(self): """ create a new organization :return: py2neo Node """ self.unique_id = str(uuid.uuid4()) new_org_properties = { "name": self.name, "mission_statement": self.mission_statement, "unique_id": self.unique_id, "email": self.email, "is_open": self.is_open, "is_invite_only": self.is_invite_only, "website": self.website} new_org_node = Node.cast(AgoraLabel.ORGANIZATION, new_org_properties) self.graph_db.create(new_org_node) return new_org_node
def createRelationshipWithProperties(): print("Start - Creating Relationships") # Authenticate the user using py2neo.authentication # Ensure that you change the password 'sumit' as per your database configuration. py2neo.authenticate("localhost:7474", "neo4j", "sumit") # Connect to Graph and get the instance of Graph graph = Graph("http://localhost:7474/db/data/") # Create Node with Properties amy = Node("FEMALE", name="Amy") # Create one more Node with Properties kristine = Node("FEMALE",name="Kristine") # Create one more Node with Properties sheryl = Node("FEMALE",name="Sheryl") #Define an Object of relationship which depicts the relationship between Amy and Kristine #We have also defined the properties of the relationship - "since=2005" #By Default the direction of relationships is left to right, i.e. the --> kristine_amy = Relationship(kristine,"FRIEND",amy,since=2005) #This relationship is exactly same as the earlier one but here we are using "Rev" #"py2neo.Rev = It is used to define the reverse relationship (<--) between given nodes amy_sheryl=Relationship(amy,Rev("FRIEND"),sheryl,since=2001) #Finally use graph Object and Create Nodes and Relationship #When we create Relationship between, then Nodes are also created. resultNodes = graph.create(kristine_amy,amy_sheryl) #Print the results (relationships) print("Relationship Created - ",resultNodes)
def reblogs_into_neo4j(reblogs): graph = Graph() everyone = set() for reblog in reblogs: everyone.add(reblog['reblogger']) everyone.add(reblog['reblogged_from']) nodes = { name: Node("User", name=name) for name in everyone } for reblog in reblogs: print("Creating (%s)-[:RBF]->(%s)" % (reblog['reblogger'], reblog['reblogged_from'])) reblogger = nodes[reblog['reblogger']] reblogged_from = nodes[reblog['reblogged_from']] cxn = Relationship(reblogger, "REBLOGGED_FROM", reblogged_from) graph.create(cxn)
def save_publications_list(publications): """ Method to save all the publications to neo4j database """ graph = Graph() for publication in publications: publication_node = Node("Publication") for key in publication: if key not in ['_id', 'authorsSearched', 'identifiers', 'rank', 'work-citation-type']: publication_node.properties[key] = publication[key] graph.create(publication_node) print 'Inserted Publication: ' + publication['doi']
class Build_Configuration: def __init__(self): self.graph = Graph() self.graph.delete_all() self.namespace = ["Start"] self.parent_node = [] def check_duplicates(self, label, name): # print "label",label,name if self.graph.find_one(label, property_key="name", property_value=name) != None: raise ValueError("Duplicate Node", label, name) def get_namespace(self, name): print self.namespace, name temp = copy.deepcopy(self.namespace) temp.append(name) return_value = "/".join(temp) return return_value def get_parent_node(self): return self.parent_node[-1] def pop_namespace(self): del self.namespace[-1] del self.parent_node[-1] # concept of namespace name is a string which ensures unique name # the name is essentially the directory structure of the tree def construct_node(self, push_namespace, relationship, label, name, properties): namespace = self.get_namespace(name) self.check_duplicates(label, name=namespace) node = Node(label) node.properties["namespace"] = namespace node.properties["name"] = name for i in properties.keys(): node.properties[i] = properties[i] self.graph.create(node) if len(self.parent_node) != 0: relation_enity = Relationship(self.get_parent_node(), relationship, node) self.graph.create(relation_enity) if push_namespace == True: self.namespace.append(name) self.parent_node.append(node)
def connect(login_, password_, host_port_): # global graph allows to get the same graph every time init_db() is called authenticate(host_port_, login_, password_) if host_port_ == "localhost:7474": graph_con = Graph() else: graph_con = Graph("http://"+host_port_+"/db/data/") node = Node("TestNodeToRemove", name="Alice", age="23", eyes="blue") try: graph_con.create(node) graph_con.delete(node) except Unauthorized: graph_con = None return graph_con
def saveComponent(properties, label, price, voorraad,link, winkel): graph = Graph("http://localhost:7484/db/data/") modelID = properties['ModelID'] if bool(graph.cypher.execute_one('match (n) where n.ModelID = "{}" return n'.format(modelID))): cn = graph.cypher.execute_one('match (n) where n.ModelID = "{}" return n'.format(modelID)) else: cn = Node(label) for i in properties: cn.properties[i] = properties[i] graph.create(cn) cn.add_labels('Component') cn.push() rel = Relationship(cn, 'SOLD_AT', winkel, Price=price, inStock=voorraad, productUrl=link) graph.create(rel) saveMetaData(winkel.properties['Name'], properties['Name'], modelID, price, properties['Merk'],'www.Informatique.nl', label) time.sleep(3)
class Neo4JClient: """Client for Neo4J""" def __init__(self): authenticate("localhost:7474", secrets.NEO4J_USERNAME , secrets.NEO4J_PASSWORD) self.graph = Graph("http://localhost:7474/db/data/") def create_user_node(self, user_dict): mykeys = ['name', 'id_str', 'description', 'screen_name'] user_dict = {k: v for (k, v) in user_dict.items() if k in mykeys} user_node = Node('Person', user_dict['name'], **user_dict) self.graph.create(user_node) return user_node def find_user_node(self, key, value): return self.graph.find_one('Person', property_key=key, property_value=value) def create_rel(self, start_node, end_node, rel_type): return self.graph.create((start_node, rel_type, end_node)) def update_user_node(id, user_dict): pass
def save_authors_list(authors): """ Method to save all the authors to neo4j database """ graph = Graph() for author in authors: author_node = Node("Author") if 'firstname' in author: author_node.properties["firstname"] = author["firstname"] if 'lastname' in author: author_node.properties["lastname"] = author["lastname"] if 'fullname' in author: author_node.properties["fullname"] = author["fullname"] if 'orcid' in author: author_node.properties["orcid"] = author["orcid"] if 'othernames' in author: author_node.properties["othernames"] = author["othernames"] graph.create(author_node) print 'Inserted Author: ' + author['orcid']
class CreerNumerosNeo4J(CreerNumeros): ''' Creer des numeros dans les voies d'une commune donnees pour Neo4J match (v:Voie)-[]-(c:Commune) where v.libelle = "RUE DES PINSONS" and c.libcom <> "TOULOUSE" return c,v match (v:Voie)-[]-(c:Commune) where v.libelle = "RTE DE LAVALETTE" and c.libcom = "BEAUPUY" return c,v self.graph.cypher.execute("") ''' def __init__(self, nums_dans_rue): self.graph = Graph(os.environ["NEO4JDBSTR"]) super(CreerNumerosNeo4J, self).__init__(nums_dans_rue) def peuple(self, commune): voiesdelacommune = self.voies[commune] nbvoiesdelacommune = len(self.voies[commune]) placecommune = list(self.communes).index(commune)+1 nbcommunes = len(self.communes) cpt = 0 for v in voiesdelacommune: #if not re.compile(".*D59.*").match(v.libelle): continue numero = Numero(7, v.libelle, v.sti, v.rivoli[:-1], v.rivoli, None, None) cpt += 1 ## Pour contourner un bug d'espace dans le fichier d'open data.. if numero.lib_off[0] == " ": numero.lib_off = numero.lib_off[1:] try: ## On recupere le noeud de rue qui va recevoir ce numero. Ne retourne normalement qu'une rue donc on selectionne le premier et unique resultat avec , #voie, = self.graph.cypher.execute( "match (v:Voie { libelle:'%s' })-[:ORGANISE]-(c:Commune {libcom:'%s'}) return v" % (numero.lib_off, commune) ) voie, = self.graph.cypher.execute( "match (v:Voie { libelle:'%s' })-[:ORGANISE]-(c:Commune {libcom:'%s'}) return v" % (numero.lib_off, commune) ) except ValueError as e: print("WARNING;%s; La voie <%s> de la commune <%s> n'existe pas." % (cpt,numero.lib_off,commune) ) print("%s" % (e) ) continue for n in self.nums_dans_rue: numero.no = n newnum = Node.cast("Numero", numero.to_dict() ) #print( " %s ; %s ; %s\n%s" % (n,type(newnum),newnum,voie) ) ## Le noeud Numero est crée en meme temps que la relation arete = self.graph.create( Relationship(voie['v'],"COMPORTE",newnum) ) #if n % 50 == 0 : print( "%s ; %s ; %s" % (n,type(newnum),newnum) ) #if n % 50 == 0 : print( " %s" % (arete) ) if cpt %10 == 0 : print( "Voie %s/%s de la commune %s / %s" % (cpt,nbvoiesdelacommune, placecommune, nbcommunes) ) def close(self): #self.graph.close() pass
class Friends(object): def __init__(self, uri, username, password): self.neo = Graph(uri) self.uri = uri self.username = username self.password = password def create_person(self, name): node = Node("Person", name=name) self.neo.create(node) return node def make_mutual_friends(self, node1, node2): relationship = Relationship(node1, "FRIENDS_WITH", node2) relationship2 = Relationship(node2, "FRIENDS_WITH", node1) self.neo.create(relationship) self.neo.create(relationship2) def suggestions_for(self, node): returnType = "node" payload = { "order": "breadth_first", "uniqueness": "node_global", "relationships": { "type": "FRIENDS_WITH", "direction": "in" }, "return_filter" : { "body" : "position.length() == 2;", "language" : "javascript" }, "max_depth": 2 } payload = json.dumps(payload) headers = { "Accept": "application/json; charset=UTF-8", "Authorization": "Basic bmVvNGo6cGFzc3dvcmQ=", "Content-Type": "application/json" } uri = self.uri + "node/" + str(node._id) + "/traverse/" + returnType res = requests.post(uri, data=payload, headers=headers).json() recommendations_list = [] for el in res: recommendations_list.append(el["data"]["name"]) recommendations = ', '.join(recommendations_list) return recommendations def reset(self): self.neo.delete_all()
def createNodeWithLabelPropertiesWithCast(): print("Start - Creating Node with Label and Properties") # Authenticate the user using py2neo.authentication # Ensure that you change the password 'sumit' as per your database configuration. py2neo.authenticate("localhost:7474", "neo4j", "sumit") # Connect to Graph and get the instance of Graph graph = Graph("http://localhost:7474/db/data/") #Define a LIST of Labels labels = [ 'FirstLabel' ,'SecondLabel' ] #Define a DICTIONARY properties = {'name':'MyPythonNode2', 'neo4j_version':'2.2'} #CAST the node and invoke graph.create method. node = Node.cast(labels,properties) resultNode, = graph.create(node) print("Node - ", resultNode) print("End - Creating Node with Label and Properties")
def main2(): authenticate("localhost:7474", "neo4j", "1234") graph = Graph(GRAPH_CONNECTION_STRNIG) graph.delete_all() banana = Node("Fruit", name="banana", colour="yellow", tasty=True) graph.create(banana) t = graph.merge_one("Fruit", 'name', 'apple') t['colour'] = 'green' t['tasty'] = True t.push() alice = Node("Person", name="Alice") bob = Node("Person", name="Bob") alice_knows_bob = Relationship(alice, "KNOWS", bob, since=1999) graph.create(alice) graph.create(bob) graph.create(alice_knows_bob)
class EncumbranceLoader(object): def __init__(self): authenticate("localhost:7474", "neo4j", "1111") self.graph_db = Graph(graph_DB_url) self.api_url = "http://127.0.0.1:8000/api/v1/encumbrance/?format=json" self.statement = "MERGE (n:Encumbrance {NStatement:{NS}, id: {ID}, DateStatement:{DS}, Date:{D}," \ "AddedInfo:{AI}}) RETURN n" def whipUp(self): objects = json.load(urlopen(self.api_url))["objects"] for object in objects: args = {} args["ID"]=object["id"] args["NS"]=object["NStatement"] args["DS"]=object["DateStatement"] args["D"]=object["Date"] args["AI"]=object["AddedInfo"] db = self.graph_db.cypher.begin() db.append(self.statement, args) db.commit() enc = getEncumbrance(id=int(args["ID"])) obj = getObject(id=int(object["Obj"]["id"])) docBase = getDocumentBase(id=int(object["DocBase"]["id"])) if obj != None: self.graph_db.create(rel(enc.one, "HAVE_OBJECT", obj.one)) if docBase != None: self.graph_db.create(rel(enc.one, "HAVE_DOCUMENT", docBase.one)) for sp in object["SPerson"]: s_p = getPerson(id=int(sp["id"])) self.graph_db.create(rel(enc.one, "HAVE_DEPTOR", s_p.one)) for wp in object["WPerson"]: w_p = getPerson(id=int(wp["id"])) self.graph_db.create(rel(enc.one, "HAVE_WEIGHT", w_p.one))
def test_example_code_runs(self): from py2neo import Graph from py2neo.ext.calendar import GregorianCalendar graph = Graph() time_index = graph.legacy.get_or_create_index(neo4j.Node, "TIME") calendar = GregorianCalendar(time_index) alice, birth, death = graph.create( {"name": "Alice"}, (0, "BORN", calendar.day(1800, 1, 1)), (0, "DIED", calendar.day(1900, 12, 31)) ) assert birth.end_node["year"] == 1800 assert birth.end_node["month"] == 1 assert birth.end_node["day"] == 1 assert death.end_node["year"] == 1900 assert death.end_node["month"] == 12 assert death.end_node["day"] == 31
def creatNodeWithLabelProperties(): print("Start - Creating Node with Label and Properties") # Authenticate the user using py2neo.authentication # Ensure that you change the password 'sumit' as per your database configuration. py2neo.authenticate("localhost:7474", "neo4j", "sumit") # Connect to Graph and get the instance of Graph graph = Graph("http://localhost:7474/db/data/") # Create Nodes, where all positional arguments to constructor is Label. # And rest (keyword arguments) are Node Properties. #Below is a Node with 1 Label and 2 properties node1 = Node("FirstLabel", name="MyPythonNode1", neo4j_version="2.2") #Below is a Node with 2 Label and 2 properties node2 = Node("FirstLabel", "SecondLabel",name="MyPythonNode2", neo4j_version="2.2") #Now Use object of graph to create Node, the return type is a python tuple #Multiple Nodes can be created in a single Graph command resultNodes = graph.create(node1, node2) #Iterate Over Tuple and print all the values in the Tuple for index in range(len(resultNodes)): print("Created Node - ", index, ", ", resultNodes[index]) print("End - Creating Node with Label and Properties")
id = str(uuid.uuid4()) email = "*****@*****.**" name = 'Amy' new_user_properties = { "name": name, "mission_statement": "Use the Agora to learn all the things.", "id": id, "email": email.lower(), "is_mentor": True, "is_tutor": True, "is_visible": True, "is_available_for_in_person": True, "is_admin": False} new_user_node = Node.cast(AgoraLabel.USER, new_user_properties) try: graph_db.create(new_user_node) except: print 'Node found' user_node = graph_db.find_one(AgoraLabel.USER, property_key='email', property_value=email.lower()) print user_node["email"] user = AgoraUser() user.email = email user.get_user() print user.user_interests interest = AgoraInterest() interest.name = 'Music'
from py2neo import Node, Graph, Relationship # Get the IP address of the docker container "neo4j" import json, subprocess host = json.loads(subprocess.check_output(["docker", "inspect", "neo4j"]))[0]["NetworkSettings"]["IPAddress"] # Make the client graph = Graph("http://%s:7474/db/data/" % host) # Make some nodes alice = Node("Person", name="Alice") bob = Node("Person", name="Bob") cleo = Node("Pet", name="Cleo", species="Dog") dan = Node("Person", name="Dan") graph.create(Relationship(alice, "KNOWS", bob)) graph.create(Relationship(bob, "KNOWS", alice)) graph.create(Relationship(bob, "KNOWS", dan)) graph.create(Relationship(alice, "OWNS", cleo)) print "All people:" for record in graph.cypher.execute("MATCH (p:Person) RETURN p.name AS name"): print(record.name) print "People who know someone who know Dan:" for record in graph.cypher.execute("MATCH (p)-[:KNOWS]->(q)-[:KNOWS]->(r {name:'Dan'}) RETURN p.name AS name"): print(record.name)
def update_info_and_links(): print 'updating show info' authenticate("localhost:7474", "neo4j", "1234") graph = Graph(GRAPH_CONNECTION_STRNIG) results = graph.cypher.stream("match (s:Show) return id(s) as eid,s.id") start_id = 0 for record in results: if int(record['s.id']) < start_id: continue node_show = graph.node(record['eid']) result_dict = {} success = True while success: try: show_info_e_list = requests.get( 'http://services.tvrage.com/feeds/full_show_info.php?sid={0}'.format(node_show['id'])) result_dict = xmltodict.parse(show_info_e_list.text) omdb_show_info = requests.get( 'http://www.omdbapi.com/?t={0}&y=&plot=full&r=json'.format(node_show['name'])) dict_omdb_show_info = json.loads(omdb_show_info.text) if dict_omdb_show_info['Response'] == 'True': for key, value in dict_omdb_show_info.iteritems(): node_show[key] = value success = False except ValueError as e: logger.exception("Value error") continue except Exception as e: logger.exception("Some network issue: will try again") success = True print str(node_show['name']) # info node_show['started'] = result_dict['Show'].get('started', None) node_show['ended'] = result_dict['Show'].get('ended', None) node_show['image'] = result_dict['Show'].get('image', None) node_show['status'] = result_dict['Show'].get('status', None) node_show.push() #Country from_country = result_dict['Show'].get('origin_country', 'unknown') node_country = graph.merge_one("Country", 'country', from_country) node_country.push() show_from_country = Relationship(node_show, "from", node_country) graph.create(show_from_country) #Genres if result_dict['Show'].get('genres', None) is not None: genre_list = [] if type(result_dict['Show']['genres']['genre']) is list: genre_list = result_dict['Show']['genres']['genre'] else: genre_list.append(result_dict['Show']['genres']['genre']) for genre in genre_list: node_genre = graph.merge_one("Genre", 'name', genre) node_genre.push() show_of_genre = Relationship(node_show, "of genre", node_genre) graph.create(show_of_genre) """ try: print node_show['started'] a = node_show['started'].split("/") if int(a[len(a)-1]) < 2000: continue except Exception: continue """ #Seasons season_list = [] if result_dict['Show'].get('Episodelist', None) is None: continue if type(result_dict['Show']['Episodelist']['Season']) is list: season_list = result_dict['Show']['Episodelist']['Season'] else: season_list.append(result_dict['Show']['Episodelist']['Season']) for season in season_list: node_season = Node.cast('Season', {'no': season['@no']}) graph.create(node_season) show_season = Relationship(node_show, "has", node_season) graph.create(show_season) #Episodes episode_list = [] if type(season['episode']) is list: episode_list = season['episode'] else: episode_list.append(season['episode']) count = 1 for episode in episode_list: node_episode = Node.cast('Episode', { 'airdate': episode.get('airdate', None), 'epnum': count, 'screencap': episode.get('screencap', None), 'title': episode.get('title', None) }) graph.create(node_episode) success = True while success: try: omdb_episode_info = requests.get('http://www.omdbapi.com/?t={0}&Season={1}&Episode={2}' .format(node_show['name'], node_season['no'], node_episode['epnum'])) dict_omdb_episode_info = json.loads(omdb_episode_info.text) if dict_omdb_episode_info['Response'] == 'True': for key, value in dict_omdb_episode_info.iteritems(): node_episode[key] = value node_episode.push() success = False except ValueError as e: logger.exception("Value error") continue except Exception as e: logger.exception("Some network issue: will try again") success = True try: search = node_show['name'] + ' s' + str(node_season['no']).zfill(2) + 'e' + str(node_episode['epnum']).zfill(2) print search #links search_numbers = [3552639851, 8556419051, 2649486255, 7079685853, 8416818254, 1870757059, 1731156253, 4545021852, 6021755051, 8975221455] for n in search_numbers: links_from_google = requests.get( 'https://www.googleapis.com/customsearch/v1element?key=AIzaSyCVAXiUzRYsML1Pv6RwSG1gunmMikTzQqY&rsz=small&num=10&hl=en&prettyPrint=false&source=gcsc&gss=.com&sig=cb6ef4de1f03dde8c26c6d526f8a1f35&cx=partner-pub-2526982841387487:{1}' '&q={0}&googlehost=www.google.com&oq={0}'.format(search, n)) dict_from_google = json.loads(links_from_google.text) for result in dict_from_google['results']: node_link = Node.cast('Link', { 'host': result.get('visibleUrl', None), 'url': result['url'] }) graph.create(node_link) link_episode = Relationship(node_episode, "has", node_link) graph.create(link_episode) except Exception, err: logger.exception("error grom google part") show_episode = Relationship(show_season, "has", node_episode) graph.create(show_episode) count = count + 1