Skip to content

Commit

Permalink
Merge pull request #4 from LuqueDaniel/2.1
Browse files Browse the repository at this point in the history
2.1
  • Loading branch information
LuqueDaniel committed Oct 14, 2013
2 parents 22792a0 + 982d28f commit beade1f
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 39 deletions.
36 changes: 30 additions & 6 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@ Pybooru - Library for Danbooru API.
========================================================================
Pybooru is a library for Python for access to API Danbooru / Moebooru based sites.

Version: **2.0**<br />
Version: **2.1**<br />
Licensed under: **MIT License**

Dependencies.
-------------
- Python: >= 2.7
- [Simplejson](https://pypi.python.org/pypi/simplejson/) (Optional).

Installation.
------------------------------------------------------------------------
For installation Pybooru.
Pypi - Python Package Index:
[Pybooru on Pypi](https://pypi.python.org/pypi/Pybooru/).
```bash
sudo pip install Pybooru
```
or
```bash
sudo easy_install Pybooru
```

Manual installation:
```bash
git clone git://github.com/luquedaniel/pybooru.git
cd pybooru
sudo python setup.py build
sudo python setup.py install
git clone git://github.com/luquedaniel/pybooru.git
cd pybooru
sudo python setup.py build
sudo python setup.py install
```

Example use.
Expand All @@ -31,6 +45,7 @@ for artist in artists:

Login example.
------------------------------------------------------------------------
Default sites:
```python
from pybooru import Pybooru

Expand All @@ -39,5 +54,14 @@ client = Pybooru('Konachan', username='your-username', password='your-password')
client.comments_create(post_id=id, comment_body='Comment content')
```

Other sites:
```python
from pybooru import Pybooru

client = Pybooru('konachan.com', username='your-username', password='your-password', hashString='So-I-Heard-You-Like-Mupkids-?--%s--')

client.comments_create(post_id=id, comment_body='Comment content')
```

Special Thanks to.
------------------------------------------------------------------------
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Pybooru is a library for Python for access to API Danbooru / Moebooru based sites.

Version: 2.1 (stable).

Changelog: https://github.com/LuqueDaniel/pybooru/blob/master/changelog.md

More information: https://github.com/LuqueDaniel/pybooru/blob/master/README.markdown

Github repository: https://github.com/LuqueDaniel/pybooru
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Pybooru - Changelog
===================

. **Pybooru 2.1** - (10/14/2013)
- Added login suppport for any Moebooru based site.
- Fixed a bug: 22792a0
- Added new information in setup.py.
- Small changes.
5 changes: 5 additions & 0 deletions clean_pycs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

find -iname '*.pyc' -delete

fi
5 changes: 3 additions & 2 deletions pybooru/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
Under MIT license.
"""

__author__ = 'Daniel Luque <danielluque14 at gmail dot com>'
__version__ = '2.0'
__author__ = 'Daniel Luque'
__email__ = 'danielluque14 at gmail dot com'
__version__ = '2.1'
__url__ = 'http://github.com/LuqueDaniel/pybooru'

#pybooru imports
Expand Down
8 changes: 4 additions & 4 deletions pybooru/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

#pybooru impost
from .resources import http_status_codes
from .resources import HTTP_STATUS_CODES


class PybooruError(Exception):
Expand All @@ -25,11 +25,11 @@ def __init__(self, msg, http_code=None, url=None):
self.http_code = http_code
self.url = url

if (http_code is not None) and (http_code in http_status_codes) and (
if (http_code is not None) and (http_code in HTTP_STATUS_CODES) and (
url is not None):
self.msg = '%i: %s, %s -- %s -- URL: %s' % (http_code,
http_status_codes[http_code][0],
http_status_codes[http_code][1], self.msg, url)
HTTP_STATUS_CODES[http_code][0],
HTTP_STATUS_CODES[http_code][1], self.msg, url)

def __str__(self):
"""This function return self.msg"""
Expand Down
53 changes: 34 additions & 19 deletions pybooru/pybooru.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,39 @@
#pyborru exceptions imports
from .exceptions import PybooruError
#pybooru resources imports
from .resources import api_base_url
from .resources import site_list
from .resources import API_BASE_URL
from .resources import SITE_LIST


class Pybooru(object):
"""Pybooru class.
init Parameters:
siteName: The site name in site_list.
siteName: The site name in SITE_LIST.
siteURL: URL of based Danbooru site.
username: Your username in site
(Required only for functions that modify the content).
password: Your user password
password: Your user password in plain text.
(Required only for functions that modify the content).
hashString: string that is hashed.
(See the API of the site for more information).
Attributes:
siteName: Return site name.
siteURL: Return URL of based danbooru site.
username: Return user name.
password: Return password.
password: Return password in plain text.
hashString: Return hashString.
"""

def __init__(self, siteName=None, siteURL=None, username=None,
password=None):
password=None, hashString=None):

self.siteName = siteName
self.siteURL = siteURL
self.username = username
self.password = password
self.hashString = hashString

if (siteURL is not None) or (siteName is not None):
if type(siteName) is str:
Expand All @@ -75,8 +79,8 @@ def _site_name(self, siteName):
in the resources module.
"""

if siteName in site_list.keys():
self.siteURL = site_list[siteName]['url']
if siteName in SITE_LIST.keys():
self.siteURL = SITE_LIST[siteName]['url']
else:
raise PybooruError(
'The site name is not valid, use siteURL parameter'
Expand All @@ -101,26 +105,33 @@ def _url_validator(self, url):
self.siteURL = url

def _json_load(self, api_name, params=None):
"""Function for reading and returning JSON response.
"""Function for read and return JSON response.
Parameters:
api_name: The NAME of the API function.
params: The parameters of the API function.
"""

url = self.siteURL + api_base_url[api_name]['url']
url = self.siteURL + API_BASE_URL[api_name]['url']

#Autentication
if api_base_url[api_name]['required_login'] is True:
if self.siteName in site_list.keys():
if API_BASE_URL[api_name]['required_login'] is True:
if (self.siteName in SITE_LIST.keys()) or (self.hashString is not None):
if (self.username is not None) and (self.password is not None):
#Set login parameter
params['login'] = self.username

#Create hashed string
has_string = site_list[self.siteName]['hashed_string'] % (
self.password)

if self.hashString is not None:
try:
has_string = self.hashString % (self.password)
except TypeError:
raise PybooruError('Use "%s" for hashString')
else:
has_string = SITE_LIST[self.siteName]['hashed_string'] % (
self.password)

#Set password_hash parameter
#Convert hashed_string to SHA1 and return hex string
params['password_hash'] = hashlib.sha1(
has_string).hexdigest()
Expand All @@ -129,13 +140,17 @@ def _json_load(self, api_name, params=None):
raise PybooruError('username and password is required')

else:
raise PybooruError('Login in %s unsupported' % self.siteName)
raise PybooruError('Login in %s unsupported, please use hashString' % self.siteName)

#JSON request
try:
#urlopen() from module urllib2
#urlencode() from module urllib
openURL = urlopen(url, urlencode(params))
if params is not None:
#urlopen() from module urllib2
#urlencode() from module urllib
openURL = urlopen(url, urlencode(params))
else:
openURL = urlopen(url)

reading = openURL.read()
#loads() is a function of simplejson module
response = loads(reading)
Expand Down
6 changes: 3 additions & 3 deletions pybooru/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

#site_list
site_list = {
SITE_LIST = {
'konachan': {
'url': 'http://konachan.com',
'hashed_string': 'So-I-Heard-You-Like-Mupkids-?--%s--'},
Expand All @@ -19,7 +19,7 @@


#api_base_url for the API functions
api_base_url = {
API_BASE_URL = {
'posts_list': {
'url': '/post.json',
'required_login': False},
Expand Down Expand Up @@ -144,7 +144,7 @@


#http_status_codes
http_status_codes = {
HTTP_STATUS_CODES = {
200: ('OK', 'Request was successful'),
403: ('Forbidden', 'Access denied'),
404: ('Not Found', 'Not found'),
Expand Down
27 changes: 22 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
#!/usr/bin/env python

#setuptools imports
from setuptools import setup
from setuptools import find_packages

__author__ = 'Daniel Luque <danielluque14 at gmail dot com>'
__version__ = '2.0'
#pybooru imports
import pybooru


with open('README.rst', 'r') as f:
LONG_DESCRIPTION = f.read()


setup(
name='Pybooru',
version=__version__,
author=__author__,
version=pybooru.__version__,
author=pybooru.__author__,
description="Pybooru is a library for Python for access to API Danbooru / Moebooru based sites.",
long_description=LONG_DESCRIPTION,
author_email="danielluque14 at gmail dot com",
url="https://github.com/LuqueDaniel/pybooru",
license="MIT License",
keywords='Pybooru moebooru danbooru API',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Internet'
],
platforms=['any'],
install_requires=['simplejson'],
packages=find_packages(),
include_package_data=True,
install_requires=['simplejson'],
)

0 comments on commit beade1f

Please sign in to comment.