Skip to content
This repository was archived by the owner on Mar 2, 2022. It is now read-only.

Commit 49520ba

Browse files
Adding Wikia Wrapper
Co-Authored-By: PolarisLo <polarislo@users.noreply.github.com>
1 parent 769ace8 commit 49520ba

File tree

5 files changed

+664
-0
lines changed

5 files changed

+664
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
__pycache__/

__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .wikia import *
2+
from .exceptions import *
3+
4+
__author__ = "NotThatSiri"
5+
__version__ = "0.0.1"

exceptions.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
Global wikia exception and warning classes.
3+
"""
4+
5+
import sys
6+
7+
8+
ODD_ERROR_MESSAGE = "This shouldn't happen. Please report on GitHub: https://github.com/NotThatSiri/DLD-Bot"
9+
class WikiaException(Exception):
10+
"""Base Wikia exception class."""
11+
12+
def __init__(self, error):
13+
self.error = error
14+
15+
def __unicode__(self):
16+
return "An unknown error occured: \"{0}\". Please report it on GitHub!".format(self.error)
17+
18+
if sys.version_info > (3, 0):
19+
def __str__(self):
20+
return self.__unicode__()
21+
22+
else:
23+
def __str__(self):
24+
return self.__unicode__().encode('utf8')
25+
26+
27+
class PageError(WikiaException):
28+
"""Exception raised when no Wikia matched a query."""
29+
30+
def __init__(self, pageid=None, *args):
31+
if pageid:
32+
self.pageid = pageid
33+
else:
34+
self.title = args[0]
35+
36+
def __unicode__(self):
37+
if hasattr(self, 'title'):
38+
return u"\"{0}\" does not match any pages. Try another query!".format(self.title)
39+
else:
40+
return u"Page id \"{0}\" does not match any pages. Try another id!".format(self.pageid)
41+
42+
43+
class DisambiguationError(WikiaException):
44+
"""
45+
Exception raised when a page resolves to a Disambiguation page.
46+
The `options` property contains a list of titles
47+
of Wikia pages that the query may refer to.
48+
.. note:: `options` does not include titles that do not link to a valid Wikia page.
49+
"""
50+
51+
def __init__(self, title, may_refer_to):
52+
self.title = title
53+
self.options = may_refer_to
54+
55+
def __unicode__(self):
56+
return u"\"{0}\" may refer to: \n{1}".format(self.title, '\n'.join(self.options))
57+
58+
59+
class RedirectError(WikiaException):
60+
"""Exception raised when a page title unexpectedly resolves to a redirect."""
61+
62+
def __init__(self, title):
63+
self.title = title
64+
65+
def __unicode__(self):
66+
return u"\"{0}\" resulted in a redirect. Set the redirect property to True to allow automatic redirects.".format(self.title)
67+
68+
69+
class HTTPTimeoutError(WikiaException):
70+
"""Exception raised when a request to the Mediawiki servers times out."""
71+
72+
def __init__(self, query):
73+
self.query = query
74+
75+
def __unicode__(self):
76+
return u"Searching for \"{0}\" resulted in a timeout. Try again in a few seconds, and make sure you have rate limiting set to True.".format(self.query)

util.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sys
2+
import functools
3+
4+
def debug(fn):
5+
def wrapper(*args, **kwargs):
6+
print(fn.__name__, 'called!')
7+
print(sorted(args), tuple(sorted(kwargs.items())))
8+
res = fn(*args, **kwargs)
9+
print(res)
10+
return res
11+
return wrapper
12+
13+
14+
class cache(object):
15+
16+
def __init__(self, fn):
17+
self.fn = fn
18+
self._cache = {}
19+
functools.update_wrapper(self, fn)
20+
21+
def __call__(self, *args, **kwargs):
22+
key = str(args) + str(kwargs)
23+
if key in self._cache:
24+
ret = self._cache[key]
25+
else:
26+
ret = self._cache[key] = self.fn(*args, **kwargs)
27+
28+
return ret
29+
30+
def clear_cache(self):
31+
self._cache = {}
32+
33+
34+
# from http://stackoverflow.com/questions/3627793/best-output-type-and-encoding-practices-for-repr-functions
35+
def stdout_encode(u, default='UTF8'):
36+
encoding = sys.stdout.encoding or default
37+
if sys.version_info > (3, 0):
38+
return u.encode(encoding).decode(encoding)
39+
return u.encode(encoding)

0 commit comments

Comments
 (0)