Advertisement
Guest User

Untitled

a guest
Feb 16th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.07 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.                 pass
  75.                 # raise PointAPIError(error={'code': '404', 'message':'Not Found'})
  76.                 # return {'code': '404', 'message':'Not Found'}
  77.         except PointAPIError as e:
  78.             # raise
  79.             pass
  80.  
  81.  
  82.  
  83.     def make_post(self, text, tag, private, cookies=None):
  84.         """tag = ['tag1', 'tag2']
  85.        """
  86.         try:
  87.             cookies = self.cookies
  88.             if not cookies:
  89.                 print('Not logged in')
  90.                 return 1
  91.             params = {
  92.                 'text':text,
  93.                 'tag': tag
  94.             }
  95.             url = 'https://point.im/api/post'
  96.             headers = {'X-CSRF': '<{0}>'.format(self.cookies['user'])}
  97.             print(headers)
  98.             r = requests.post(url=url, cookies=cookies, headers=headers, data=params)
  99.             print(r.headers)
  100.             return 0
  101.         except:
  102.             pass
  103.  
  104.  
  105.     def GetUser(self, user_login=None):
  106.         try:
  107.             if not user_login:
  108.                 url = 'api/me'
  109.             else:
  110.                 url = 'api/user/login/{}'.format(user_login)
  111.             print("URL: ", url)
  112.             response = self.api_request(url)
  113.             if response is None:
  114.                 return
  115.             if 'code' in response:
  116.                 pass
  117.                     # return PointAPIError(error=response)
  118.                     # return [ response.json() ]
  119.         except PointAPIError:
  120.             #raise
  121.             pass
  122.         #else:
  123.         #    return [ response.json() ]
  124.         #print(response.json())
  125.         return response.json()
  126.  
  127.  
  128.     def GetSubscriptions(self, user_login):
  129.         try:
  130.             url = '{0}user/{1}/subscriptions'.format(self.API, user_login)
  131.             print("URL: ", url)
  132.             response = self.api_request(url)
  133.             if 'code' in response:
  134.                 raise PointAPIError(erorr=response)
  135.             return response.json()
  136.         except:
  137.             raise
  138.  
  139.  
  140.     def GetSubscribers(self, user_login):
  141.         try:
  142.             url = '{0}user/{1}/subscribers'.format(self.API, user_login)
  143.             print("URL: ", url)
  144.             response = self.api_request(url)
  145.             if 'code' in response:
  146.                 raise PointAPIError(error=response)
  147.             return response.json()
  148.         except:
  149.             raise
  150.  
  151.     def GetPost(self, post):
  152.         try:
  153.             url = 'api/post/{0}'.format(post)
  154.             print("URL: ", url)
  155.             response = self.api_request(url)
  156.             print(response)
  157.             if 'code' in response:
  158.                 raise PointAPIError(error=response)
  159.             return response.json()
  160.         except:
  161.             raise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement