From 4021175d2d621af16b5ece24ccb87e135d7b4680 Mon Sep 17 00:00:00 2001 From: Kagamin Date: Sun, 23 Nov 2008 16:32:02 +0300 Subject: [PATCH] Wakaba plugin (base) --- daemon/board.py | 31 ++++++++++++++++---------- daemon/chan.py | 9 ++++++++ daemon/client.py | 38 ++++++++++++++++---------------- daemon/my_thread.py | 20 +++++++++++++++++ daemon/plugins/__init__.py | 0 daemon/plugins/plugin_wakaba.py | 41 +++++++++++++++++++++++------------ daemon/plugins/wakaba_thread.py | 48 +++++++++++++++++++++++++++++++++++++++++ daemon/post.py | 13 +++++++++++ daemon/services.py | 16 +++++++------- 9 files changed, 165 insertions(+), 51 deletions(-) rewrite daemon/board.py (67%) rewrite daemon/client.py (66%) create mode 100644 daemon/my_thread.py create mode 100644 daemon/plugins/__init__.py rewrite daemon/plugins/plugin_wakaba.py (84%) create mode 100644 daemon/plugins/wakaba_thread.py create mode 100644 daemon/post.py diff --git a/daemon/board.py b/daemon/board.py dissimilarity index 67% index 41229dd..1fadc7f 100644 --- a/daemon/board.py +++ b/daemon/board.py @@ -1,11 +1,20 @@ -class Board: - - def __init__(self, name): - self.name = name - self.threads = [] - - def watch(self): - pass - - def unwatch(self): - pass +import dbus.service + +INTERFACE = 'py.chans.dbus' + +class Board(dbus.service.Object): + + def __init__(self, bus_name, obj_path, name, base_uri): + dbus.service.Object.__init__(self, bus_name, obj_path) + self.bus_name = bus_name + self.obj_path = obj_path + self.name = name + self.base_uri = base_uri + self.threads = [] + + @dbus.service.method(dbus_interface=INTERFACE, in_signature='', out_signature='as') + def get_threads_ids(self): + ids = [] + for thread in self.threads: + ids.append(thread.id) + return ids diff --git a/daemon/chan.py b/daemon/chan.py index fb6c985..ab90919 100644 --- a/daemon/chan.py +++ b/daemon/chan.py @@ -1,5 +1,7 @@ import dbus.service +INTERFACE = 'py.chans.dbus' + class Chan(dbus.service.Object): def __init__(self, bus_name, obj_path, name, base_uri, boards): @@ -8,6 +10,13 @@ class Chan(dbus.service.Object): self.base_uri = base_uri self.boards = boards + @dbus.service.method(dbus_interface=INTERFACE, in_signature='', out_signature='as') + def get_boards(self): + boards = [] + for board in self.boards: + boards.append(board.name) + return boards + def watch(self): pass diff --git a/daemon/client.py b/daemon/client.py dissimilarity index 66% index 6faf42e..dc6f0ea 100644 --- a/daemon/client.py +++ b/daemon/client.py @@ -1,18 +1,20 @@ -import dbus -import sys - -INTERFACE = 'py.chans.dbus' -OBJ_PATH = '/pool' - -############################################### -bus = dbus.SessionBus() - -try: - object_chanpool = bus.get_object(INTERFACE, OBJ_PATH) -except dbus.DBusException: - sys.stderr.write('dbus isn\'t running\n') - sys.exit(1) - -chanPool = dbus.Interface(object_chanpool, INTERFACE) - -print chanPool.get_chans() +import dbus +import sys + +INTERFACE = 'py.chans.dbus' + +############################################### +bus = dbus.SessionBus() + +board_object = bus.get_object(INTERFACE, '/pool/2ch/r') +board = dbus.Interface(board_object, INTERFACE) +board.refresh() +ids = board.get_threads_ids() +print '###THREADS IN /pool/2ch/r###' +print ids + +thread_object = bus.get_object(INTERFACE, '/pool/2ch/r/' + ids[0]) +thread = dbus.Interface(thread_object, INTERFACE) +thread.refresh() +print '###POSTS IN /pool/2ch/r/%s###' %ids[0] +print thread.get_posts_dicts() diff --git a/daemon/my_thread.py b/daemon/my_thread.py new file mode 100644 index 0000000..6c3ea0d --- /dev/null +++ b/daemon/my_thread.py @@ -0,0 +1,20 @@ +import dbus.service + +INTERFACE = 'py.chans.dbus' + +class Thread(dbus.service.Object): + + def __init__(self, bus_name, obj_path, board, id): + dbus.service.Object.__init__(self, bus_name, obj_path) + self.bus_name = bus_name + self.obj_path = obj_path + self.board = board + self.id = id + self.posts = [] + + @dbus.service.method(dbus_interface=INTERFACE, in_signature='', out_signature='aa{ss}') + def get_posts_dicts(self): + posts_dicts = [] + for post in self.posts: + posts_dicts.append(post.post_dict) + return posts_dicts diff --git a/daemon/plugins/__init__.py b/daemon/plugins/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/daemon/plugins/plugin_wakaba.py b/daemon/plugins/plugin_wakaba.py dissimilarity index 84% index 46a71f1..55eea5b 100644 --- a/daemon/plugins/plugin_wakaba.py +++ b/daemon/plugins/plugin_wakaba.py @@ -1,14 +1,27 @@ -import dbus.service -from board import Board - -class MetaBoard(Board, dbus.service.Object): - - def __init__(self, bus_name, obj_path, name): - dbus.service.Object.__init__(self, bus_name, obj_path) - Board.__init__(self, name) - - def watch(self): - pass - - def unwatch(self): - pass +import dbus.service + +from board import Board +from plugins.wakaba_thread import WakabaThread + +import urllib +import re + +INTERFACE = 'py.chans.dbus' + +class MetaBoard(Board): + + def __init__(self, bus_name, obj_path, name, base_uri): + Board.__init__(self, bus_name, obj_path, name, base_uri) + + @dbus.service.method(dbus_interface=INTERFACE, in_signature='', out_signature='') + def refresh(self): + def get_threads_ids(): + data = urllib.urlopen('%s/%s/' %(self.base_uri, self.name)).read() + threads = re.findall('
', data) + return threads + + self.threads = [] + + threads_ids = get_threads_ids() + for thread_id in threads_ids: + self.threads.append( WakabaThread(bus_name=self.bus_name, obj_path=self.obj_path +'/'+ thread_id, board=self, id=thread_id) ) diff --git a/daemon/plugins/wakaba_thread.py b/daemon/plugins/wakaba_thread.py new file mode 100644 index 0000000..8417f1f --- /dev/null +++ b/daemon/plugins/wakaba_thread.py @@ -0,0 +1,48 @@ +import dbus.service + +from my_thread import Thread +from post import Post + +import urllib +import re + +INTERFACE = 'py.chans.dbus' + +class WakabaThread(Thread): + + def __init__(self, bus_name, obj_path, board, id): + Thread.__init__(self, bus_name, obj_path, board, id) + + @dbus.service.method(dbus_interface=INTERFACE, in_signature='', out_signature='') + def refresh(self): + def get_posts_dicts(): + def parse_msg_table(post_data): + post = {} + post['id'] = re.findall('', post_data)[0] + post['date'] = '' + post['name'] = '' + post['title'] = '' + post['is_sage'] = '' + img_src = re.findall('', post_data) + if img_src: + post['img_src'] = img_src[0] + else: + post['img_src'] = '' + post['text'] = re.findall('
(.*?)
', post_data)[0] + return post + + def get_msg_tables(data): + return re.findall('
(.*?)
', data) + + data = urllib.urlopen('%s/%s/res/%s.html' %(self.board.base_uri, self.board.name, self.id)).read() + + first_post = re.findall('(