|
| 1 | +import requests |
| 2 | +import collections |
| 3 | +import hashlib |
| 4 | +import urllib |
| 5 | + |
| 6 | +class Vbulletin: |
| 7 | + |
| 8 | + def __init__(self, client_options): |
| 9 | + self.client_options = client_options |
| 10 | + self.init_api() |
| 11 | + |
| 12 | + def init_api(self): |
| 13 | + # Initialize connection using init_info |
| 14 | + params = { |
| 15 | + 'api_s': self.client_options['apikey'], |
| 16 | + 'clientname': self.client_options['clientname'], |
| 17 | + 'clientversion': self.client_options['clientversion'], |
| 18 | + 'platformname': self.client_options['platformname'], |
| 19 | + 'platformversion': self.client_options['platformversion'], |
| 20 | + 'uniqueid': self.client_options['uniqueid'] |
| 21 | + } |
| 22 | + self.init_info = self.make_request(params, 'api_init', 'GET') |
| 23 | + |
| 24 | + |
| 25 | + def to_qs(self, params): |
| 26 | + od = collections.OrderedDict(sorted(params.items())) |
| 27 | + ret = '' |
| 28 | + for i, item in enumerate(od.iteritems()): |
| 29 | + ret = ret + item[0] + "=" + urllib.quote_plus(item[1]) |
| 30 | + if i != (len(od.items()) - 1): |
| 31 | + ret = ret + '&' |
| 32 | + return ret |
| 33 | + |
| 34 | + def create_sign(self, params): |
| 35 | + params_qs = self.to_qs(params) |
| 36 | + unsigned = params_qs + self.init_info['apiaccesstoken'] + str(self.init_info['apiclientid']) + self.init_info['secret'] + self.client_options['apikey'] |
| 37 | + return hashlib.md5(unsigned).hexdigest() |
| 38 | + |
| 39 | + |
| 40 | + def utf8encode(self, params): |
| 41 | + encoded = {} |
| 42 | + for key, value in params.iteritems(): |
| 43 | + encoded[key] = value.encode('utf8') |
| 44 | + return encoded |
| 45 | + |
| 46 | + |
| 47 | + def make_request(self, params, api_m, method): |
| 48 | + params = self.utf8encode(params) |
| 49 | + params['api_m'] = api_m |
| 50 | + |
| 51 | + # Request must be signed for every call other than api_init |
| 52 | + if api_m != 'api_init' and self.init_info != None: |
| 53 | + # Sign must not contain api_c and api_s so we add those parameters later |
| 54 | + # For a post, do not sign any request parameters |
| 55 | + if method == 'POST': |
| 56 | + params['api_sig'] = self.create_sign({}) |
| 57 | + |
| 58 | + elif method == 'GET': |
| 59 | + params['api_sig'] = self.create_sign(params) |
| 60 | + |
| 61 | + params['api_c'] = str(self.init_info['apiclientid']) |
| 62 | + params['api_s'] = self.init_info['apiaccesstoken'] |
| 63 | + |
| 64 | + params_qs = urllib.urlencode(params) |
| 65 | + if method == 'POST': |
| 66 | + r = requests.post(self.client_options['forumurl'] + '/api.php', data=params) |
| 67 | + elif method == 'GET': |
| 68 | + r = requests.get(self.client_options['forumurl'] + '/api.php?' + params_qs) |
| 69 | + else: |
| 70 | + raise Exception("Only POST and GET supported for now, but others are easy to add") |
| 71 | + |
| 72 | + return r.json() |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + def login(self, username, password): |
| 77 | + # login using login_login |
| 78 | + params = { |
| 79 | + 'vb_login_password': password, |
| 80 | + 'vb_login_username': username |
| 81 | + } |
| 82 | + result = self.make_request(params, 'login_login', 'POST') |
| 83 | + if 'session' not in result or 'userid' not in result['session'] or int(result['session']['userid']) == 0: |
| 84 | + raise Exception("Login failed", result) |
| 85 | + |
| 86 | + def post_new_thread(self, forum_id, subject, message): |
| 87 | + # Create a new thread using newthread_postthread |
| 88 | + params = { |
| 89 | + 'forumid': forum_id, |
| 90 | + 'message': message, |
| 91 | + 'subject': subject |
| 92 | + } |
| 93 | + result = self.make_request(params, 'newthread_postthread', 'POST') |
| 94 | + #print result |
| 95 | + |
| 96 | + if 'show' in result and 'threadid' in result['show']: |
| 97 | + return result['show']['threadid'] |
| 98 | + else: |
| 99 | + raise Exception("Could not create new thread.", result) |
0 commit comments