Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pygephi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from client import GephiClient, GephiFileHandler

from __future__ import print_function, absolute_import
from .client import GephiClient, GephiFileHandler
50 changes: 26 additions & 24 deletions pygephi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,25 @@
Allow a Python script to communicate with Gephi using the Gephi Graph Streaming protocol and plugin.
"""

from __future__ import print_function, absolute_import

__author__ = '[email protected]'

import urllib2
try:
import json
from urllib2 import urlopen
except ImportError:
try:
import simplejson as json
except:
raise "Requires either simplejson or Python 2.6!"

from urllib.request import urlopen
import json
import time
import sys

class JSONClient(object):

def __init__(self, autoflush=False, enable_timestamps=False, process_event_hook=None):
self.data = ""
self.autoflush = autoflush
self.enable_timestamps = enable_timestamps

if enable_timestamps:
def default_peh(event):
event['t'] = int(time.time())
Expand All @@ -49,56 +48,59 @@ def default_peh(event):
self.peh = default_peh
else:
self.peh = lambda e: default_peh(process_event_hook(e))

def flush(self):
if len(self.data) > 0:
self._send(self.data)
self.data = ""

def _send(self, data):
print 'passing'
""" Overwrite in subclass. """
pass

def add_node(self, id, flush=True, **attributes):
self.data += json.dumps(self.peh({"an":{id:attributes}})) + '\r\n'
if(self.autoflush): self.flush()

def change_node(self, id, flush=True, **attributes):
self.data += json.dumps(self.peh({"cn":{id:attributes}})) + '\r\n'
if(self.autoflush): self.flush()

def delete_node(self, id):
self._send(json.dumps(self.peh({"dn":{id:{}}})) + '\r\n')

def add_edge(self, id, source, target, directed=True, **attributes):
attributes['source'] = source
attributes['target'] = target
attributes['directed'] = directed
self.data += json.dumps(self.peh({"ae":{id:attributes}})) + '\r\n'
if(self.autoflush): self.flush()

def delete_edge(self, id):
self._send(json.dumps(self.peh({"de":{id:{}}})) + '\r\n')

def clean(self):
self._send(json.dumps(self.peh({"dn":{"filter":"ALL"}})) + '\r\n')

class GephiClient(JSONClient):

def __init__(self, url='http://127.0.0.1:8080/workspace0', autoflush=False):
JSONClient.__init__(self, autoflush)
self.url = url

def _send(self, data):
conn = urllib2.urlopen(self.url+ '?operation=updateGraph', data)
# For python3 we need to convert data string to bytes
if sys.version_info[0] > 2:
data = bytes(data, encoding='utf-8')
conn = urlopen(self.url+ '?operation=updateGraph', data)
return conn.read()

class GephiFileHandler(JSONClient):

def __init__(self, out, **params):
params['autoflush'] = True
JSONClient.__init__(self, **params)
self.out = out

def _send(self, data):
self.out.write(data)
64 changes: 64 additions & 0 deletions pygephi/websocket_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/python
# coding: utf-8
#
# Copyright (C) 2012 André Panisson
#
# Licensed 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Websocket communication with Gephi using the Gephi Graph-streaming protocol and plugin.

Based on client.py by André Panisson and examples by Matthieu Totet

See also:
* https://github.com/totetmatt/gephi.stream.graph.websocket
* http://matthieu-totet.fr/Koumin/2014/06/15/lets-play-gephi-streaming-api-the-hidden-websocket/
* https://github.com/panisson/pygephi_graphstreaming/network

"""

from __future__ import print_function, absolute_import

__author__ = '[email protected]'

import json
import time
from websocket import create_connection

from .client import GephiClient


class GephiWsClient(GephiClient):
"""
Class for communicating with the Gephi graph-streaming plugin over websocket protocol.
This is slightly faster than REST communication with HTTP requests.
"""
def __init__(self, url='ws://127.0.0.1:8080/workspace0', autoflush=False, autoconnect=True):
GephiClient.__init__(self, url, autoflush)
self.conn = None
if autoconnect:
self.connect()

def connect(self):
self.conn = create_connection(self.url)

def _send(self, data):
if self.conn is None:
self.connect()
try:
return self.conn.send(data)
except ConnectionAbortedError as e:
print("Connection aborted,", e)
print("Retrying connection...")
self.connect()
self.conn.send(data)