Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: ecbb7735c5
Fetching contributors…

Cannot retrieve contributors at this time

147 lines (119 sloc) 4.593 kB
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Tristan Fischer (
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <>.
import simplejson as json
from urllib import urlencode, quote_plus
from urllib2 import urlopen, Request, HTTPError, URLError
API_URL = ''
class NetworkError(Exception):
class ApiError(Exception):
class DokuMonsterApi():
USER_AGENT = 'XBMC DokuMonsterApi'
def __init__(self):
self.show_only_online = 1
self.default_count = 50
def get_tags(self):
params = {
'limit': self.default_count,
'sort': 'count'
json_data = self.__api_call(path='get_tags', params=params)
return [{
'id': genre.get('id'),
'name': genre.get('name'),
'count': genre.get('count'),
} for genre in json_data.get('items', [])]
def get_popular_docus(self):
return self._get_items(sort='views', sortorder='desc')
def get_top_docus(self):
return self._get_items(sort='fire', sortorder='desc')
def get_newest_docus(self):
return self._get_items(sort='id', sortorder='desc')
def get_docus_by_initial(self, initial):
return self._get_items(sort='title', initial=initial, sortorder='asc')
def get_docus_by_tag(self, tag_id):
return self._get_items(tag_id=tag_id)
def get_docu(self, docu_id):
params = {'id': docu_id}
json_data = self.__api_call(path='get_item', params=params)
return json_data.get('items', [])[0]
def _get_items(self, **kwargs):
params = {}
valid_kwargs = (
'sort', 'tag_id', 'query', 'initial', 'online',
'sortorder', 'limit', 'offset'
for key, val in kwargs.items():
if key in valid_kwargs:
params[key] = val
if not 'limit' in params:
params['limit'] = self.default_count
#if not 'online' in params:
# params['online'] = self.show_only_online
json_data = self.__api_call(path='get_items', params=params)
items = json_data.get('items', [])
total_count = int(json_data.get('total_count', 0))
return items, total_count
def __api_call(self, path, params=None):
url = API_URL + path
if params:
url += '?%s' % urlencode(params)
req = Request(url)
req.add_header('User Agent', self.USER_AGENT)
log('Opening URL: %s' % url)
response = urlopen(req).read()
except HTTPError, error:
raise NetworkError('HTTPError: %s' % error)
except URLError, error:
raise NetworkError('URLError: %s' % error)
log('got %d bytes' % len(response))
json_data = json.loads(response)
if not json_data.get('status', {}).get('success'):
err = json_data.get('status', {}).get('error')
log('Error: %s' % repr(err))
raise ApiError(err)
return json_data
def log(msg):
print '[DokuMonsterApi]: %s' % msg
if __name__ == '__main__':
# API testing
api = DokuMonsterApi()
print 'Testing get_tags'
tags = api.get_tags()
assert tags
for tag in tags[0:2]:
print '<Tag:%s>' % repr(tag)
print 'Testing get_newest_docus'
docus = api.get_newest_docus()
assert docus
for docu in docus[0:2]:
print '<Docu:%s>' % repr(docu)
print 'Testing get_popular_docus'
docus = api.get_popular_docus()
assert docus
for docu in docus[0:2]:
print '<Docu:%s>' % repr(docu)
print 'Testing get_top_docus'
docus = api.get_top_docus()
assert docus
for docu in docus[0:2]:
print '<Docu:%s>' % repr(docu)
Jump to Line
Something went wrong with that request. Please try again.