Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- class PointError(Exception):
- pass
- class PointAPIError(PointError):
- def __init__(self, error=None, json_error=None, *args, **kwargs):
- self.json_error = json_error
- self.error = error
- PointError.__init__(self, *args, **kwargs)
- if json_error:
- self.message = "Error occured: {}".format(self.json_error['error'])
- else:
- self.message = "Error occured with HTTP status code {} and " \
- "message '{}'".format(self.error['code'], self.error['message'])
- print(self.message)
- class Point(object):
- def __init__(self, user, password,login=True):
- self.API='http://point.im/api/'
- self.user = user
- self.password = password
- self.session = requests.Session()
- self.cookies = None
- self.token = None
- self.csrf_token = None
- if login:
- self.login()
- def login(self):
- try:
- login_values = {'login' : self.user, 'password' : self.password}
- req = requests.request('POST', "https://point.im/api/login", data=login_values)
- req_json = req.json()
- if 'error' in req_json:
- raise PointAPIError(json_error=req_json)
- except PointAPIError:
- return 0
- else:
- print(req.json())
- self.token, self.csrf_token = req.json()['token'], req.json()['csrf_token']
- self.cookies = {'user': self.token}
- self.csrf_token = req.json()['csrf_token']
- print('Logged in.')
- return 1
- def logout(self):
- url = 'https://point.im/api/logout'
- params = {'csrf_token': self.csrf_token}
- r = requests.post(url=url, cookies=self.cookies, data=params)
- print(r.json())
- return r
- def api_request(self, url, cookies=None, schema='https://', point='point.im/',params=None):
- """
- Принимает параметр url без ведущего слеша
- """
- try:
- if not cookies:
- cookies = self.cookies
- url = ''.join( [ schema, point, url ] )
- print('URL2',url)
- r = requests.request('GET', url=url, cookies=cookies, params=params)
- print(r.status_code)
- if r.status_code not in (404,):
- return r
- else:
- pass
- # raise PointAPIError(error={'code': '404', 'message':'Not Found'})
- # return {'code': '404', 'message':'Not Found'}
- except PointAPIError as e:
- # raise
- pass
- def make_post(self, text, tag, private, cookies=None):
- """tag = ['tag1', 'tag2']
- """
- try:
- cookies = self.cookies
- if not cookies:
- print('Not logged in')
- return 1
- params = {
- 'text':text,
- 'tag': tag
- }
- url = 'https://point.im/api/post'
- headers = {'X-CSRF': '<{0}>'.format(self.cookies['user'])}
- print(headers)
- r = requests.post(url=url, cookies=cookies, headers=headers, data=params)
- print(r.headers)
- return 0
- except:
- pass
- def GetUser(self, user_login=None):
- try:
- if not user_login:
- url = 'api/me'
- else:
- url = 'api/user/login/{}'.format(user_login)
- print("URL: ", url)
- response = self.api_request(url)
- if response is None:
- return
- if 'code' in response:
- pass
- # return PointAPIError(error=response)
- # return [ response.json() ]
- except PointAPIError:
- #raise
- pass
- #else:
- # return [ response.json() ]
- #print(response.json())
- return response.json()
- def GetSubscriptions(self, user_login):
- try:
- url = '{0}user/{1}/subscriptions'.format(self.API, user_login)
- print("URL: ", url)
- response = self.api_request(url)
- if 'code' in response:
- raise PointAPIError(erorr=response)
- return response.json()
- except:
- raise
- def GetSubscribers(self, user_login):
- try:
- url = '{0}user/{1}/subscribers'.format(self.API, user_login)
- print("URL: ", url)
- response = self.api_request(url)
- if 'code' in response:
- raise PointAPIError(error=response)
- return response.json()
- except:
- raise
- def GetPost(self, post):
- try:
- url = 'api/post/{0}'.format(post)
- print("URL: ", url)
- response = self.api_request(url)
- print(response)
- if 'code' in response:
- raise PointAPIError(error=response)
- return response.json()
- except:
- raise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement