Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """Module for IQ option api."""
- import time
- import json
- import logging
- import requests
- import threading
- from iqoption_api.login import Login
- from iqoption_api.websocket import Websocket
- from iqoption_api.ssid import Ssid
- from iqoption_api.subscribe import Subscribe
- from iqoption_api.unsubscribe import UnSubscribe
- from iqoption_api.setactives import SetActives
- from iqoption_api.buy import Buy
- from iqoption_api.buyback import Buyback
- from iqoption_api.servicesubscribe import ServiceSubscribe
- from iqoption_api.appinit import AppInit
- from iqoption_api.getregdata import GetRegdata
- from iqoption_api.balancechange import BalanceChange
- from iqoption_api.auth import Auth
- from iqoption_api.appinit import AppInit
- from iqoption_api.optioninitall import OptionInitAll
- from iqoption_api.dobuyback import DoBuyBack
- from iqoption_api.ma import MA
- from iqoption_api.candles import Candles
- # InsecureRequestWarning: Unverified HTTPS request is being made.
- # Adding certificate verification is strongly advised.
- # See: https://urllib3.readthedocs.org/en/latest/security.html
- requests.packages.urllib3.disable_warnings()
- class IQOptionAPI(object):
- """Class for communication with IQ option api."""
- def __init__(self, host, username, password):
- """
- :param str host: The hostname or ip address of a IQ option server.
- :param str username: The username of a IQ option server.
- :param str password: The password of a IQ option server.
- """
- self.https_url = "https://%s/api" % host
- self.wss_url = "wss://%s/echo/websocket" % host
- self.websocket = None
- self.session = requests.Session()
- self.session.verify = False
- self.session.trust_env = False
- self.username = username
- self.password = password
- self.login_id = None
- self.balances = []
- self.hztoken = None
- self.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'}
- self.cookies = None
- self.ma = MA()
- self.candles_history = None
- def set_session_cookie(self, val):
- for k,v in self.session.cookies.iteritems():
- if not isinstance(v, str):
- self.session.cookies[k] = str(v)
- requests.utils.add_dict_to_cookiejar(self.session.cookies, val)
- def prepare_url(self, resource):
- """
- Construct url from resource url.
- :param resource: :class:`Resource
- <iqoption_api.resource.Resource>`.
- :returns: The full url to IQ option http resource.
- """
- return '/'.join((self.https_url, resource.url))
- def send_http_request(self, resource, method, data=None, params=None, headers=None):
- #pylint: disable=too-many-arguments
- """
- Send http request to IQ option server.
- :param resource: :class:`Resource <iqoption_api.resource.Resource>`.
- :param str method: The HTTP request method.
- :param dict data: (optional) The HTTP request data.
- :param dict params: (optional) The HTTP request params.
- :param dict headers: (optional) The HTTP request headers.
- :returns: :class:`Response <requests.Response>`.
- """
- url = self.prepare_url(resource)
- #print url
- logger = logging.getLogger(__name__)
- logger.debug(url)
- response = self.session.request(method=method,
- url=url,
- data=data,
- params=params,
- headers=self.headers)
- logger.debug(response)
- logger.debug(response.text)
- logger.debug(response.headers)
- logger.debug(response.cookies)
- response.raise_for_status()
- self.cookies = response.headers['Set-Cookie']
- #print response.cookies
- return response
- def send_wss_request(self, name, msg):
- """
- Send wss request to IQ option server.
- :param chanel: :class:`Chanel <iqoption_api.chanel.Chanel>`.
- :param dict data: The websocket request data.
- :returns:
- """
- data = json.dumps(dict(name=name,
- msg=msg))
- print data
- self.websocket.send(data)
- @property
- def serviceSubscribe(self):
- """
- Property for get IQ option login resource.
- :returns: :class:`Login
- <iqoption_api.login.Login>`.
- """
- return ServiceSubscribe(self)
- @property
- def optioninitall(self):
- """
- Property for get IQ option login resource.
- :returns: :class:`Login
- <iqoption_api.login.Login>`.
- """
- return OptionInitAll(self)
- @property
- def candles(self):
- """
- Property for get IQ option login resource.
- :returns: :class:`Login
- <iqoption_api.login.Login>`.
- """
- return Candles(self)
- @property
- def login(self):
- """
- Property for get IQ option login resource.
- :returns: :class:`Login
- <iqoption_api.login.Login>`.
- """
- return Login(self)
- @property
- def dobuyback(self):
- """
- Property for get IQ option login resource.
- :returns: :class:`Login
- <iqoption_api.login.Login>`.
- """
- return DoBuyBack(self)
- @property
- def appinit(self):
- """
- Property for get IQ option login resource.
- :returns: :class:`Login
- <iqoption_api.login.Login>`.
- """
- return AppInit(self)
- @property
- def auth(self):
- """
- Property for get IQ option login resource.
- :returns: :class:`Login
- <iqoption_api.login.Login>`.
- """
- return Auth(self)
- @property
- def ssid(self):
- """
- Property for get IQ option websocket ssid chanel.
- :returns: :class:`Ssid
- <iqoption_api.ssid.Ssid>`.
- """
- return Ssid(self)
- @property
- def getregdata(self):
- """
- Property for get IQ option websocket ssid chanel.
- :returns: :class:`Ssid
- <iqoption_api.ssid.Ssid>`.
- """
- return GetRegdata(self)
- @property
- def balancechange(self):
- """
- Property for get IQ option login resource.
- :returns: :class:`Login
- <iqoption_api.login.Login>`.
- """
- return BalanceChange(self)
- @property
- def subscribe(self):
- """
- Property for get IQ option websocket subscribe chanel.
- :returns: :class:`Subscribe
- <iqoption_api.subscribe.Subscribe>`.
- """
- return Subscribe(self)
- @property
- def unsubscribe(self):
- """
- Property for get IQ option websocket unsubscribe chanel.
- :returns: :class:`unsubscribe
- <iqoption_api.unsubscribe.UnSubscribe>`.
- """
- return UnSubscribe(self)
- @property
- def setactives(self):
- """
- Property for get IQ option websocket setactives chanel.
- :returns: :class:`setactives
- <iqoption_api.setactives.SetActives>`.
- """
- return SetActives(self)
- @property
- def buy(self):
- """
- Property for get IQ option websocket buy chanel.
- :returns: :class:`buy
- <iqoption_api.buy.Buy>`.
- """
- return Buy(self)
- # @property
- # def balance(self):
- # """
- # Property for get IQ option websocket buy chanel.
- # :returns: :class:`buy
- # <iqoption_api.buy.Buy>`.
- # """
- # return Balance(self)
- @property
- def buyback(self):
- """
- Property for get IQ option websocket buy chanel.
- :returns: :class:`buy
- <iqoption_api.buy.Buy>`.
- """
- return Buyback(self)
- def _thread(self):
- """Method for websocket thread."""
- self.websocket.run_forever()
- def connect(self):
- """Method for connection to api."""
- # response = self.appinit()
- # print response.cookies.get_dict
- response = self.login(self.username, self.password)
- # q = response.headers['Set-Cookie'].split(";")[0].split('=')[1]
- # ssid = q
- ssid = response.cookies["ssid"]
- self.login_id = response.cookies["login_id"]
- #self.set_session_cookie({"ssid": ssid, "login_id": self.login_id})
- response = self.getregdata()
- __uat = response.cookies['__uat']
- self.set_session_cookie({"platform": "9", "platform_version": "387.2.4fc", "is_regulated": '0', "authorization":"1", "ru_restricted_popup_shown": "1", "aff": "1503", "afftrack": "home"})
- #print self.session.cookies
- # headers = [
- # ('Origin', 'https://iqoption.com'),
- # ('Cookie',"ssid="+ssid),
- # ('Cookie',"login_id="+self.login_id),
- # ('Cookie',"__uat="+__uat),
- # ('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'),
- # ]
- # headers = [
- # ('Origin', 'https://iqoption.com'),
- # ('Cookie',"ssid="+ssid+"; login_id="+self.login_id+"; __uat="+__uat+"; platform=9; platform_version=387.2.4fc; authorization=1;"),
- # ('User-Agent', '222Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36')
- # ]
- # headers = [
- # ('Cookie',"ssid="+ssid+"; login_id="+self.login_id+"; __uat="+__uat)
- # ]
- #получаем текущий баланс
- # response = self.balance()
- # json_data = json.loads(response.text)
- # print json_data['user_balance']
- #получаем текущий баланс
- response = self.appinit()
- json_data = json.loads(response.text)
- #print json_data['user_balance']
- #получаем список балансов
- response = self.optioninitall()
- json_data = json.loads(response.text)
- #print json_data
- #меняем на баланс 12085209
- response = self.balancechange(12085209)
- json_data = json.loads(response.text)
- #print json_data
- # response = self.auth()
- # json_data = json.loads(response.text)
- # print json_data['result']
- websocket = Websocket(self.wss_url)
- websocket.connect()
- self.websocket = websocket
- websocket_thread = threading.Thread(target=self._thread)
- websocket_thread.daemon = True
- websocket_thread.start()
- self.ssid(ssid)
- self.subscribe("deposited")
- self.subscribe("activeScheduleChange")
- self.subscribe("tradersPulse")
- self.subscribe("activeCommissionChange")
- self.serviceSubscribe()
- # self.subscribe("activeCommissionChange")
- # self.unsubscribe("tournamentChange")
- self.unsubscribe("iqguard")
- self.unsubscribe("signal")
- self.unsubscribe("feedRecentBets")
- self.unsubscribe("feedRecentBets2")
- self.unsubscribe("feedTopTraders2")
- self.unsubscribe("feedRecentBetsMulti")
- self.unsubscribe("tournament")
- # self.subscribe("loyalty")
- # self.setactives([1])
- # self.buyback(1,3)
- time.sleep(3)
- self.setactives([1])
- self.buyback(1,3)
- self.unsubscribe("tournamentChange")
- # response = self.balancelist()
- # json_data = json.loads(response.text)
- # balances = json_data['result']['profile']['balances']
- # for i in balances:
- # _balance = dict(id=i[u'id'], type=i[u'type'])
- # self.balances.append(_balance)
- i = 0
- count_candles_get = 0
- while True:
- #self.websocket
- # if self.websocket.success == False:
- # time.sleep(3)
- # i = 0
- #time.sleep(1)
- if i != 1:
- if(self.websocket.show_value != None and self.websocket.skey != None):
- #self.ssid(self.websocket.ssid)
- #self.buy(self.websocket.time, self.websocket.show_value, self.websocket.skey)
- self.candles(1,60,(self.websocket.time + (60-(self.websocket.time%60)))-3600,self.websocket.time)
- i = 1
- # if i == 1:
- # if (self.websocket.time%60 == 0):
- # self.candles(1,60,(self.websocket.time + (60-(self.websocket.time%60)))-60,self.websocket.time)
- if self.websocket.success == True and self.websocket.option_id != None:
- print self.websocket.option_id
- self.websocket.success = False
- response = self.dobuyback(self.websocket.option_id)
- json_data = json.loads(response.text)
- print json_data
- if self.websocket.candles != None and count_candles_get < self.websocket.candles_cout:
- #print self.websocket.candles
- self.candles_history = self.websocket.candles
- self.ma.rsi(self.websocket.candles)
- count_candles_get = count_candles_get + 1
- # print self.websocket.message
- # print '####'
- # print self.websocket.skey
- # self.buy(self.websocket.time, self.websocket.show_value)
- q = IQOptionAPI('iqoption.com', 1, 2)
- q.connect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement