Skip to content

Instantly share code, notes, and snippets.

Created November 6, 2015 01:13
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import requests
import time
import json
from py2neo import Graph
from py2neo import neo4j
from py2neo.packages.httpstream import http
from py2neo.packages.httpstream import SocketError
def RateLimited(maxPerSecond):
minInterval = 1.0 / float(maxPerSecond)
def decorate(func):
lastTimeCalled = [0.0]
def rateLimitedFunction(*args,**kargs):
elapsed = time.clock() - lastTimeCalled[0]
leftToWait = minInterval - elapsed
if leftToWait>0:
ret = func(*args,**kargs)
lastTimeCalled[0] = time.clock()
return ret
return rateLimitedFunction
return decorate
def get_topics(title, body):
payload = { 'title': title,
'body': body,
'api-token': "YOUR API TOKEN HERE"}
r ="", data=payload)
return r
def get_graph():
# Connect to graph
hostname = ''
port = 7474
password = 'hillary'
neo4j.authenticate('%s:%s' % (hostname, port), 'neo4j', password)
graph = neo4j.Graph("http://%s:%s/db/data/" % (hostname, port))
return graph
def add_topics(topics_json, email_id):
graph = get_graph()
query = """
UNWIND {topics} AS t
t.score AS s,
t.topic AS n
MERGE (e:Email {id: {email_id} })
MERGE (top:Topic {name: n})
MERGE (e)-[:REFERENCES {score: s}]->(top)"""
res = graph.cypher.execute(query, topics=topics_json, email_id=email_id)
def process_text():
response_array = []
graph = get_graph()
cyph = 'MATCH (u:Email) WHERE NOT EXISTS((u)-[:REFERENCES]->()) RETURN u.subject AS subject, u.text AS text, AS id'
res = graph.cypher.execute(cyph)
for record in res:
response_dict = { 'id': }
topics_res = get_topics(record.subject, record.text)
topics_json = topics_res.json()
if 'topics' in topics_json:
response_dict['topics'] = topics_json['topics']
print response_dict
print "\n"
response_array.append( response_dict )
except SocketError:
raise Exception("SocketError trying to connect")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment