Advertisement
codkelden

PointAPI

Mar 6th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.81 KB | None | 0 0
  1. import requests
  2.  
  3. class PointError(Exception):
  4.     pass
  5.  
  6.  
  7. class PointAPIError(PointError):
  8.     def __init__(self, error=None, json_error=None, *args, **kwargs):
  9.         self.json_error = json_error
  10.         self.error = error
  11.         PointError.__init__(self, *args, **kwargs)
  12.         if json_error:
  13.             self.message = "Error occured: {}".format(self.json_error['error'])
  14.         else:
  15.             self.message = "Error occured with HTTP status code {} and \
  16.            message '{}'".format(self.error['code'], self.error['message'])
  17.         print(self.message)
  18.  
  19.  
  20.  
  21. class Point(object):
  22.     def __init__(self, user, password,login=True):
  23.         self.API='http://point.im/api/'
  24.         self.user = user
  25.         self.password = password
  26.         self.session = requests.Session()
  27.         self.cookies = None
  28.         self.token = None
  29.         self.csrf_token = None
  30.         if login:
  31.             self.login()
  32.  
  33.  
  34.     def login(self):
  35.         try:
  36.             login_values = {'login' : self.user, 'password' : self.password}
  37.             req = requests.request('POST', "https://point.im/api/login", data=login_values)
  38.             req_json = req.json()
  39.             if 'error' in req_json:
  40.                 raise PointAPIError(json_error=req_json)
  41.         except PointAPIError:
  42.             return 0
  43.         else:
  44.             print(req.json())
  45.             self.token, self.csrf_token = req.json()['token'], req.json()['csrf_token']
  46.             self.cookies = {'user': self.token}
  47.             self.csrf_token = req.json()['csrf_token']
  48.             print('Logged in.')
  49.             return 1
  50.  
  51.  
  52.     def logout(self):
  53.         url = 'https://point.im/api/logout'
  54.         params = {'csrf_token': self.csrf_token}
  55.         r = requests.post(url=url, cookies=self.cookies, data=params)
  56.         print(r.json())
  57.         return r
  58.  
  59.  
  60.     def api_request(self, url, cookies=None, schema='https://', point='point.im/',params=None):
  61.         """
  62.        Принимает параметр url без ведущего слеша
  63.        """
  64.         try:
  65.             if not cookies:
  66.                 cookies = self.cookies
  67.             url = ''.join( [ schema, point, url ] )
  68.             print('URL2',url)
  69.             r = requests.request('GET', url=url, cookies=cookies, params=params)
  70.             print(r.status_code)
  71.             if r.status_code not in (404,):
  72.                 return r
  73.             else:
  74.                 raise PointAPIError(error={'code': '404', 'message':'Not Found'})
  75.         except PointAPIError as e:
  76.             raise
  77.  
  78.  
  79.  
  80.     def make_post(self, text, tag, private, cookies=None):
  81.         """tag = ['tag1', 'tag2']
  82.        """
  83.         try:
  84.             cookies = self.cookies
  85.             if not cookies:
  86.                 print('Not logged in')
  87.                 return 1
  88.             params = {
  89.                 'text':text,
  90.                 'tag': tag
  91.             }
  92.             url = 'https://point.im/api/post'
  93.             headers = {'X-CSRF': '<{0}>'.format(self.cookies['user'])}
  94.             print(headers)
  95.             r = requests.post(url=url, cookies=cookies, headers=headers, data=params)
  96.             print(r.headers)
  97.             return 0
  98.         except:
  99.             pass
  100.  
  101.  
  102.     def GetUser(self, user_login=None):
  103.         try:
  104.             if not user_login:
  105.                 url = 'api/me'
  106.             else:
  107.                 url = 'api/user/{0}'.format(user_login)
  108.             print("URL: ", url)
  109.             response = self.api_request(url)
  110.             if 'code' in response:
  111.                 raise PointAPIError(error=response)
  112.             #return [ response.json() ]
  113.         except PointAPIError:
  114.             #raise
  115.             pass
  116.         else:
  117.             return [ response.json() ]
  118.        
  119.  
  120.     def GetSubscriptions(self, user_login):
  121.         try:
  122.             url = '{0}user/{1}/subscriptions'.format(self.API, user_login)
  123.             print("URL: ", url)
  124.             response = self.api_request(url)
  125.             if 'code' in response:
  126.                 raise PointAPIError(erorr=response)
  127.             return response.json()
  128.         except:
  129.             raise
  130.  
  131.  
  132.     def GetSubscribers(self, user_login):
  133.         try:
  134.             url = '{0}user/{1}/subscribers'.format(self.API, user_login)
  135.             print("URL: ", url)
  136.             response = self.api_request(url)
  137.             if 'code' in response:
  138.                 raise PointAPIError(error=response)
  139.             return response.json()
  140.         except:
  141.             raise
  142.  
  143.     def GetPost(self, post):
  144.         try:
  145.             url = 'api/post/{0}'.format(post)
  146.             print("URL: ", url)
  147.             response = self.api_request(url)
  148.             print(response)
  149.             if 'code' in response:
  150.                 raise PointAPIError(error=response)
  151.             return response.json()
  152.         except:
  153.             raise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement