Ejemplo n.º 1
0
    def get(self, tag_id1, tag_id2):
        req = "MATCH (find:tag {tag_id: %d}) RETURN find" % tag_id1
        result = neo4j.query_neo4j(req)
        tag1 = result.single()['find'].properties

        req = "MATCH (find:tag {tag_id: %d}) RETURN find" % tag_id2
        result = neo4j.query_neo4j(req)
        tag2 = result.single()['find'].properties

        response = {}
        if tag1['tag_id'] <= tag2['tag_id']:
            response['tag_src'] = tag1
            response['tag_dst'] = tag2
        else:
            response['tag_src'] = tag2
            response['tag_dst'] = tag1

        req = "match (t1: tag {tag_id: %d})<-[:REFERS_TO]-(a: annotation)-[:ANNOTATES]->(e) " % tag_id1
        req += "match (e)<-[:ANNOTATES]-(a2: annotation)-[:REFERS_TO]->(t2: tag {tag_id: %d}) " % tag_id2
        req += "match (e)<-[:AUTHORSHIP]-(u: user) "
        req += "return distinct t1.tag_id, CASE e.post_id when null then e.comment_id else e.post_id end as id, CASE e.post_id WHEN null THEN 'comment' ELSE 'post' END as entity_type, e.timestamp as timestamp, e.label as label, u.user_id as user_id, u.label as user_label, t2.tag_id ORDER BY e.timestamp DESC"
        result = neo4j.query_neo4j(req)

        tags = []
        for record in result:
            tags.append({'id': record['id'], "entity_type": record['entity_type'], "timestamp": record['timestamp'], 'label': record['label'], 'user_id': record['user_id'], 'user_label': record['user_label']}) 

        response['list'] = tags
        try:
            tags
        except ResultError:
            return makeResponse("ERROR",500)
        return makeResponse(response, 200)
    def create_tags(self):
        query_neo4j("CREATE CONSTRAINT ON (t:tag) ASSERT t.tag_id IS UNIQUE")
        print('Import tags')
        Continue = True
        page_val = 0
        while Continue:
            tag_url = config['importer_discourse']['abs_path'] + config[
                'importer_discourse']['codes_rel_path'] + ".json?api_key=" + config[
                    'importer_discourse'][
                        'admin_api_key'] + "&api_username="******"&per_page=5000&page=" + str(
                                    page_val)
            not_ok = True
            while not_ok:
                try:
                    tag_req = requests.get(tag_url)
                except:
                    print('request problem on tag page ' + str(page_val))
                    time.sleep(2)
                    continue
                try:
                    tag_json = tag_req.json()
                except:
                    print("failed read tag on page " + str(page_val))
                    time.sleep(2)
                    continue
                not_ok = False

            # get all tags
            for tag in tag_json:
                # create tag if not existing
                if not (tag['id'] in self.existing_elements['tags']):
                    # handle multilingual values
                    if len(tag['names']) < 1:
                        continue
                    tag['name'] = tag['names'][0]['name'].lower()
                    for tmp_i in range(len(tag['names'])):
                        if tag['names'][tmp_i]['locale'] == 'en':
                            tag['name'] = tag['names'][tmp_i]['name'].lower()
                            break

                    if not (tag['name'] in self.tags):
                        self.createTag(tag['id'], tag['name'])
                        self.map_tag_to_tag[tag['id']] = tag['id']
                        self.tags[tag['name']] = tag['id']
                    else:
                        # if duplicate using mapping
                        #                        tag_n = self.existing_elements['tags'][self.tags[tag['name'].lower()]]
                        self.map_tag_to_tag[tag['id']] = self.tags[tag['name']]
#                    self.existing_elements['tags'][tag['id']] = tag_n
                    self.existing_elements['tags'].append(tag['id'])

                # no need to create tag hierarchy as the route does not give ancestry info

            if len(tag_json) == 5000:
                page_val += 1
            else:
                Continue = False
                break
Ejemplo n.º 3
0
    def get(self, user_id):
        # Get user properties
        req = "MATCH (find:user {user_id: %d}) RETURN find" % user_id
        result = neo4j.query_neo4j(req)
        user = result.single()['find'].properties
        # Get user's posts
        req = "MATCH (find:user {user_id: %d})" % user_id
        req += " MATCH (find)-[:AUTHORSHIP]->(p:post)"
        req += ' RETURN p.post_id AS post_id, p.label AS post_label, p.timestamp AS timestamp ORDER BY p.timestamp DESC'
        result = neo4j.query_neo4j(req)
        posts = []
        posts_id = []

        for record in result:
            try:
                if record['post_id'] and record['post_id'] not in posts_id:
                    post = {}
                    post['post_id'] = record['post_id']
                    post['label'] = record['post_label']
                    post['timestamp'] = record['timestamp']
                    posts.append(post)
                    posts_id.append(post['post_id'])
            except KeyError:
                pass
        # Get user's comments
        req = "MATCH (find:user {user_id: %d})" % user_id
        req += " MATCH (find)-[:AUTHORSHIP]->(c:comment)"
        req += " OPTIONAL MATCH (c)-[:COMMENTS]->(p:post)"
        req += ' RETURN c.comment_id AS comment_id, c.label AS comment_label, c.timestamp AS timestamp, p.post_id AS comment_parent_post_id, p.label AS comment_parent_post_label ORDER BY c.timestamp DESC'
        result = neo4j.query_neo4j(req)
        comments_id = []
        comments = []

        for record in result:
            try:
                if record['comment_id'] and record['comment_id'] not in comments_id:
                    comment = {}
                    comment['comment_id'] = record['comment_id']
                    comment['label'] = record['comment_label']
                    comment['timestamp'] = record['timestamp']
                    comment['comment_parent_post_id'] = record['comment_parent_post_id']
                    comment['comment_parent_post_label'] = record['comment_parent_post_label']
                    comments.append(comment)
                    comments_id.append(comment['comment_id'])
            except KeyError:
                pass

        try:
            user
        except NameError:
            return makeResponse("ERROR : Cannot find user with user_id: %d" % user_id, 204)
        user['posts'] = posts
        user['comments'] = comments
        return makeResponse(user, 200)
    def create_tags(self):
        query_neo4j("CREATE CONSTRAINT ON (t:tag) ASSERT t.tag_id IS UNIQUE")
        print('Import tags')
        Continue = True
        page_val = 0
        while Continue:
            tag_url = config['importer_discourse']['abs_path']+config['importer_discourse']['codes_rel_path']+".json?per_page=5000&page="+str(page_val)
            headers = {'Api-Key': config['importer_discourse']['admin_api_key']}
            not_ok = True
            while not_ok:
                try:
                    tag_req = requests.get(tag_url, headers=headers)
                except:
                    print('request problem on tag page '+str(page_val))
                    time.sleep(2)
                    continue
                try:
                    tag_json = tag_req.json()
                except:
                    print("failed read tag on page "+str(page_val))
                    time.sleep(2)
                    continue
                not_ok = False

            # get all tags
            for tag in tag_json:
                # create tag if not existing
                if not(tag['id'] in self.existing_elements['tags']):

                    # Use the English code name if available, otherwise the first code name.
                    english_name = next((tag_name['name'] for tag_name in tag['names'] if tag_name['locale'] == 'en'), '')
                    tag['name'] = tag['names'][0]['name'] if english_name == '' else english_name

                    if not(tag['name'].lower() in self.tags):
                        self.createTag(tag['id'], tag['name'].lower())
                        self.map_tag_to_tag[tag['id']] = tag['id']
                        self.tags[tag['name'].lower()] = tag['id']
                    else:
                    # if duplicate using mapping
#                        tag_n = self.existing_elements['tags'][self.tags[tag['name'].lower()]]
                        self.map_tag_to_tag[tag['id']] = self.tags[tag['name'].lower()]
#                    self.existing_elements['tags'][tag['id']] = tag_n
                    self.existing_elements['tags'].append(tag['id'])

                # no need to create tag hierarchy as the route does not give ancestry info

            if len(tag_json) == 5000:
                page_val += 1
            else:
                Continue = False
                break
Ejemplo n.º 5
0
    def get(self, annotation_id):
        req = "MATCH (find:annotation {annotation_id: %d})" % annotation_id
        req += "MATCH (find)-[:ANNOTATES]->(x)"
        req += 'RETURN find, labels(x) as test'
        result = neo4j.query_neo4j(req)
        #annotation = result.single()['find'].properties
        annotateComment = False
        for record in result:
            annotation = record['find'].properties
            try:
                if "comment" in record["test"]:
                    annotateComment = True
            except KeyError:
                return makeResponse(
                    "ERROR : Impossible to identify 'entity_type' for annotation with aid: %d"
                    % annotation_id, 205)

        if annotateComment:
            req = "MATCH (find:annotation {annotation_id: %d})-[:REFERS_TO]->(t: tag)  " % annotation_id
            req += "MATCH (find)-[:ANNOTATES]->(c:comment)"
            req += "MATCH (c)<-[:AUTHORSHIP]-(u:user)"
            req += 'RETURN find, c.comment_id as entity_id, c.title as entity_title, c.timestamp as entity_timestamp, u.user_id as user_id, u.name as user_name, "comment" as entity_type, t.tag_id as tag_id, t.label as tag_label ORDER BY c.timestamp DESC'
        else:
            req = "MATCH (find:annotation {annotation_id: %d})-[:REFERS_TO]->(t: tag) " % annotation_id
            req += "MATCH (find)-[:ANNOTATES]->(p:post)"
            req += "MATCH (p)<-[:AUTHORSHIP]-(u:user)"
            req += 'RETURN find, p.post_id as entity_id, p.title as entity_title, p.timestamp as entity_timestamp, u.user_id as user_id, u.name as user_name, "post" as entity_type, t.tag_id as tag_id, t.label as tag_label ORDER BY p.timestamp DESC'

        result = neo4j.query_neo4j(req)
        for record in result:
            try:
                annotation['user_id'] = record['user_id']
                annotation['user_name'] = record['user_name']
                annotation['entity_id'] = record['entity_id']
                annotation['entity_title'] = record['entity_title']
                annotation['entity_timestamp'] = record['entity_timestamp']
                annotation['entity_type'] = record['entity_type']
                annotation['tag_id'] = record['tag_id']
                annotation['tag_label'] = record['tag_label']
            except KeyError:
                return makeResponse(
                    "ERROR : Cannot find annotation with aid: %d" %
                    annotation_id, 203)

        try:
            annotation
        except NameError:
            return makeResponse(
                "ERROR : Cannot find annotation with aid: %d" % annotation_id,
                204)
        return makeResponse(annotation, 200)
Ejemplo n.º 6
0
    def get(self):
        req = "MATCH (p:post) WHERE NOT (p)<-[:ANNOTATES]- (: annotation) RETURN p.post_id AS post_id,p.label AS label, p.timestamp AS timestamp ORDER BY timestamp DESC"
        result = neo4j.query_neo4j(req)
        posts = []
        for record in result:
            posts.append({'post_id': record['post_id'], "label": record['label'], "timestamp": record['timestamp']})
        
        req = "MATCH (c:comment) WHERE NOT (c)<-[:ANNOTATES]- (: annotation) RETURN c.comment_id AS comment_id, c.label AS label, c.timestamp AS timestamp ORDER BY timestamp DESC"
        result = neo4j.query_neo4j(req)
        comments = []
        for record in result:
            comments.append({'comment_id': record['comment_id'], "label": record['label'], "timestamp": record['timestamp']})

        return makeResponse({'posts': posts, "comments": comments}, 200)
Ejemplo n.º 7
0
 def get(self, user_id):
     req = "MATCH (a:annotation)<-[:AUTHORSHIP]-(:user {user_id: %d}) RETURN count(a) AS nb_annotations" % user_id
     result = neo4j.query_neo4j(req)
     try:
         return makeResponse(result.single()['nb_annotations'], 200)
     except ResultError:
         return makeResponse("ERROR", 500)
Ejemplo n.º 8
0
 def get(self, comment_id):
     req = "MATCH (a:annotation)-[:ANNOTATES]->(:comment {comment_id: %d}) RETURN count(a) AS nb_annotations" % comment_id
     result = neo4j.query_neo4j(req)
     try:
         return makeResponse(result.single()['nb_annotations'], 200)
     except ResultError:
         return makeResponse("ERROR", 500)
Ejemplo n.º 9
0
 def get(self):
     req = "MATCH (p:post) <-[:AUTHORSHIP]- (u: user) RETURN p.post_id AS post_id,p.label AS post_label, u.user_id AS user_id, u.label AS user_label, p.timestamp AS timestamp ORDER BY timestamp DESC LIMIT 5"
     result = neo4j.query_neo4j(req)
     posts = []
     for record in result:
         posts.append({'post_id': record['post_id'], "post_label": record['post_label'], "user_id": record['user_id'], "user_label": record['user_label'], "timestamp": record['timestamp']})
     return makeResponse(posts, 200)
Ejemplo n.º 10
0
 def get(self):
     req = "MATCH (find:annotation) -[:ANNOTATES]-> (:post) RETURN find.annotation_id AS annotation_id, find.quote AS quote"
     result = neo4j.query_neo4j(req)
     annots = []
     for record in result:
         annots.append({'annotation_id': record['annotation_id'], "quote": record['quote']})
     return makeResponse(annots, 200)
Ejemplo n.º 11
0
 def get(self):
     req = "MATCH (:post) RETURN count(*) AS nb_posts"
     result = neo4j.query_neo4j(req)
     try:
         return makeResponse(result.single()['nb_posts'], 200)
     except ResultError:
         return makeResponse("ERROR", 500)
Ejemplo n.º 12
0
 def get(self, parent_tag_id):
     req = "MATCH (t:tag {tag_id : %d})<-[:IS_CHILD]-(:tag) RETURN count(*) AS nb_child" % parent_tag_id
     result = neo4j.query_neo4j(req)
     try:
         return makeResponse(result.single()['nb_child'], 200)
     except ResultError:
         return makeResponse("ERROR", 500)
Ejemplo n.º 13
0
 def get(self, user_id):
     req = "MATCH (find:user {user_id: %d}) RETURN find" % user_id
     result = neo4j.query_neo4j(req)
     try:
         return makeResponse(result.single()['find'].properties, 200)
     except ResultError:
         return makeResponse("ERROR : Cannot find user with uid: %d" % user_id, 204)
Ejemplo n.º 14
0
 def get(self, author_id):
     req = "MATCH (author:user {user_id : %d})-[:AUTHORSHIP]->(:post) RETURN count(*) AS nb_posts" % author_id
     result = neo4j.query_neo4j(req)
     try:
         return makeResponse(result.single()['nb_posts'], 200)
     except ResultError:
         return makeResponse("ERROR", 500)
Ejemplo n.º 15
0
 def get(self, parent_tag_id):
     req = "MATCH (parent:tag {tag_id : %d})<-[:IS_CHILD]-(child:tag) RETURN child" % parent_tag_id
     result = neo4j.query_neo4j(req)
     tags = []
     for record in result:
         tags.append(record['child'].properties)
     return makeResponse(tags, 200)
Ejemplo n.º 16
0
    def get(self):
        parser.add_argument('uid', action='append')
        args = parser.parse_args()

        if args['uid']:
            req = "MATCH (n:post_type)<-[r:TYPE_IS]-(p:post) "
            req += addTimeFilter()
            for user in args['uid']:
                req += "OPTIONAL MATCH (n)<-[r%s:TYPE_IS]-(p:post)<-[]-(u%s:user {uid: %s}) " % (
                    user, user, user)
            req += "RETURN n, count(r) AS nb_posts"
            for user in args['uid']:
                req += ", count(r%s) AS u%s_posts" % (user, user)
        else:
            req = "MATCH (n:post_type)<-[r:TYPE_IS]-(p:post) "
            req += addTimeFilter()
            req += "RETURN n, count(r) AS nb_posts"
        result = neo4j.query_neo4j(req)
        labels = []
        data = [[]]
        if args['uid']:
            for user in args['uid']:
                data.append([])
        for record in result:
            labels.append(record['n'].properties['name'])
            data[0].append(record['nb_posts'])
            if args['uid']:
                count = 1
                for user in args['uid']:
                    data[count].append(record['u%s_posts' % user])
                    count += 1
        return makeResponse({'labels': labels, 'data': data}, 200)
Ejemplo n.º 17
0
 def get(self):
     importer = ImportFromJson(False)
     # no user update
     # first update tag
     req = requests.get(config['importer_edgeryders']['json_tags_path'])
     json_file = req.json()
     importer.create_tags(json_file)
     # then the rest
     updateList = ['post', 'comment', 'annotation']
     for elem in updateList:
         req = "MATCH (n:" + elem + ") RETURN max(n.timestamp) AS max"
         result = neo4j.query_neo4j(req)
         try:
             most_recent = time.gmtime(int(result.single()['max']) / 1000)
         except ResultError:
             print("Problem from neo4j request.")
         since_str = time.strftime('%Y%m%d', most_recent)
         req = requests.get(config['importer_edgeryders']['json_' + elem +
                                                          's_path'] +
                            "?since=" + since_str)
         json_file = req.json()
         if elem == 'post':
             importer.create_posts(json_file)
         if elem == 'comment':
             importer.create_comments(json_file)
         if elem == 'annotation':
             importer.create_annotations(json_file)
     return makeResponse(importer.end_import(), 200)
Ejemplo n.º 18
0
 def get(self, author_id):
     req = "MATCH (author:user {user_id: %d})-[:AUTHORSHIP]->(p:post) RETURN p" % author_id
     req += addargs()
     result = neo4j.query_neo4j(req)
     posts = []
     for record in result:
         posts.append(record['p'].properties)
     return makeResponse(posts, 200)
Ejemplo n.º 19
0
 def get(self, post_type):
     req = "MATCH (find:post {type: '%s'}) RETURN find" % post_type
     req += addargs()
     result = neo4j.query_neo4j(req)
     posts = []
     for record in result:
         posts.append(record['find'].properties)
     return makeResponse(posts, 200)
Ejemplo n.º 20
0
 def get(self, post_id):
     result = neo4j.query_neo4j(
         "MATCH (find:post {post_id: %d}) RETURN find" % post_id)
     try:
         return makeResponse(result.single()['find'].properties, 200)
     except ResultError:
         return makeResponse(
             "ERROR : Cannot find post with pid: %d" % post_id, 204)
Ejemplo n.º 21
0
 def get(self, comment_id):
     req = "MATCH (find:comment {comment_id: %d}) RETURN find" % comment_id
     result = neo4j.query_neo4j(req)
     try:
         return makeResponse(result.single()['find'].properties, 200)
     except ResultError:
         return makeResponse(
             "ERROR : Cannot find comment with cid: %d" % comment_id, 204)
Ejemplo n.º 22
0
 def get(self, comment_id):
     req = "MATCH (c:comment)-[:COMMENTS]->(comment:comment { comment_id: %d}) RETURN c" % comment_id
     req += addargs()
     result = neo4j.query_neo4j(req)
     comments = []
     for record in result:
         comments.append(record['c'].properties)
     return makeResponse(comments, 200)
Ejemplo n.º 23
0
 def get(self):
     req = "MATCH (n:user) RETURN n.user_id AS user_id, n.label AS label"
     req += addargs()
     result = neo4j.query_neo4j(req)
     users = []
     for record in result:
         users.append({'user_id': record['user_id'], "label": record['label']})
     return makeResponse(users, 200)
Ejemplo n.º 24
0
 def get(self, tag_id):
     result = neo4j.query_neo4j(
         "MATCH (find:tag {tag_id: %d}) RETURN find" % tag_id)
     try:
         return makeResponse(result.single()['find'].properties, 200)
     except ResultError:
         return makeResponse(
             "ERROR : Cannot find tag with tid: %d" % tag_id, 204)
Ejemplo n.º 25
0
 def get(self):
     req = "MATCH (t:tag) RETURN t.tag_id AS tag_id, t.label AS label"
     req += addargs()
     result = neo4j.query_neo4j(req)
     tags = []
     for record in result:
         tags.append({'tag_id': record['tag_id'], "label": record['label']})
     return makeResponse(tags, 200)
Ejemplo n.º 26
0
 def get(self):
     req = "MATCH (p:post) RETURN p.post_id AS post_id, p.title AS title"
     req += addargs()
     result = neo4j.query_neo4j(req)
     posts = []
     for record in result:
         posts.append({'post_id': record['post_id'], "title": record['title']})
     return makeResponse(posts, 200)
Ejemplo n.º 27
0
 def get(self):
     req = "MATCH (a:annotation)-[:REFERS_TO]->(t:tag) MATCH (a)-[:ANNOTATES]-(x) RETURN a.annotation_id AS annotation_id, a.quote AS quote, t.tag_id AS tag_id, CASE x.post_id WHEN null THEN 'comment' ELSE 'post' END AS entity_type, CASE x.post_id WHEN null THEN x.comment_id ELSE x.post_id END AS entity_id"
     req += addargs()
     result = neo4j.query_neo4j(req)
     annots = []
     for record in result:
         annots.append({'annotation_id': record['annotation_id'], "quote": record['quote'], "tag_id": record['tag_id'], "entity_type": record["entity_type"], "entity_id": record["entity_id"]})
     return makeResponse(annots, 200)
Ejemplo n.º 28
0
 def get(self, comment_id):
     req = "MATCH (c:comment {comment_id: %d})<-[:ANNOTATES]-(a:annotation) RETURN a" % comment_id
     req += addargs()
     result = neo4j.query_neo4j(req)
     annots = []
     for record in result:
         annots.append(record['a'].properties)
     return makeResponse(annots, 200)
Ejemplo n.º 29
0
 def get(self, user_id):
     req = "MATCH (:user {user_id: %d})-[:AUTHORSHIP]->(a:annotation) RETURN a" % user_id
     req += addargs()
     result = neo4j.query_neo4j(req)
     annots = []
     for record in result:
         annots.append(record['a'].properties)
     return makeResponse(annots, 200)
Ejemplo n.º 30
0
 def get(self, annot_id):
     result = neo4j.query_neo4j(
         "MATCH (find:annotation {annotation_id: %d}) RETURN find" %
         annot_id)
     try:
         return makeResponse(result.single()['find'].properties, 200)
     except ResultError:
         return makeResponse(
             "ERROR : Cannot find annotation with id: %d" % annot_id, 204)