Skip to content

Instantly share code, notes, and snippets.

@TkTech
Created December 8, 2012 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TkTech/4240880 to your computer and use it in GitHub Desktop.
Save TkTech/4240880 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf8 -*-
import re
from collections import namedtuple
import requests
BukkitPluginUser = namedtuple('BukkitPluginUser', [
'name',
'url'
])
_bukkit_plugin_category = namedtuple('BukkitPluginCategory', [
'slug',
'name',
'icon_path',
'icon_name'
])
class BukkitPluginCategory(_bukkit_plugin_category):
def icon(self, width=32, height=32):
return '{icon_path}{width}x{height}{icon_name}.-m1.png'.format(
icon_path=self.icon_path,
width=width,
height=height,
icon_name=self.icon_name
)
class BukkitPlugin(object):
def __init__(self, j):
self._j = j
@property
def last_updated(self):
return self._j.get('last_updated')
@property
def title(self):
return self._j.get('title')
@property
def users(self):
for user in self._j.get('users', []):
yield BukkitPluginUser(**user)
@property
def curseforge_slug(self):
return self._j.get('curseforge_slug')
@property
def categories(self):
for category in self._j.get('categories', []):
yield BukkitPluginCategory(*category)
@property
def url(self):
return 'http://dev.bukkit.org/server-mods/{slug}'.format(
slug=self.curseforge_slug
)
class BukkitPBO(object):
"""
Utility class for interacting with the Bukkit plugins website.
"""
_TOKEN_R = re.compile(r'aJt[^=]*=[^"]"([^"]+)')
_DATA_URL = 'http://plugins.bukkit.org/curseforge/data.php'
@classmethod
def token(cls):
"""
Returns the token embedded in the page required to make the
data.php request.
"""
# Not entirely sure what the point of this is - some cheezy attempt
# at "thwarting" crawlers?
r = requests.get('http://plugins.bukkit.org/curseforge/')
token = cls._TOKEN_R.search(r.content)
if token:
return token.group(1)
@classmethod
def plugins(cls, page=1, title='', tag='all', author=''):
token = cls.token()
max_page = 2
while page < max_page:
r = requests.post(cls._DATA_URL, params={
'title': title,
'tag': tag,
'author': author,
'pageno': page,
'j': token
}, headers={
'Host': 'plugins.bukkit.org',
'Referer': 'http://plugins.bukkit.org/curseforge/',
'Origin': 'http://plugins.bukkit.org',
'Content-Type': 'application/x-www-form-urlencoded',
'X-Requested-With': 'XMLHttpRequest'
})
j = r.json
max_page = j['subinfo']['pagemax']
for result in j['realdata']:
yield BukkitPlugin(result)
page += 1
# Example:
for plugin in BukkitPBO.plugins():
for category in plugin.categories:
print category.icon()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment