Advertisement
Guest User

Untitled

a guest
Jul 12th, 2016
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.92 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """Module for IQ option api."""
  3.  
  4. import time
  5. import json
  6. import logging
  7. import requests
  8. import threading
  9.  
  10. from iqoption_api.login import Login
  11. from iqoption_api.websocket import Websocket
  12. from iqoption_api.ssid import Ssid
  13. from iqoption_api.subscribe import Subscribe
  14. from iqoption_api.unsubscribe import UnSubscribe
  15. from iqoption_api.setactives import SetActives
  16. from iqoption_api.buy import Buy
  17. from iqoption_api.buyback import Buyback
  18. from iqoption_api.servicesubscribe import ServiceSubscribe
  19. from iqoption_api.appinit import AppInit
  20. from iqoption_api.getregdata import GetRegdata
  21. from iqoption_api.balancechange import BalanceChange
  22. from iqoption_api.auth import Auth
  23. from iqoption_api.appinit import AppInit
  24. from iqoption_api.optioninitall import OptionInitAll
  25. from iqoption_api.dobuyback import DoBuyBack
  26. from iqoption_api.ma import MA
  27. from iqoption_api.candles import Candles
  28. # InsecureRequestWarning: Unverified HTTPS request is being made.
  29. # Adding certificate verification is strongly advised.
  30. # See: https://urllib3.readthedocs.org/en/latest/security.html
  31. requests.packages.urllib3.disable_warnings()
  32.  
  33.  
  34. class IQOptionAPI(object):
  35. """Class for communication with IQ option api."""
  36.  
  37. def __init__(self, host, username, password):
  38. """
  39. :param str host: The hostname or ip address of a IQ option server.
  40. :param str username: The username of a IQ option server.
  41. :param str password: The password of a IQ option server.
  42. """
  43. self.https_url = "https://%s/api" % host
  44. self.wss_url = "wss://%s/echo/websocket" % host
  45. self.websocket = None
  46. self.session = requests.Session()
  47. self.session.verify = False
  48. self.session.trust_env = False
  49. self.username = username
  50. self.password = password
  51.  
  52. self.login_id = None
  53. self.balances = []
  54. self.hztoken = None
  55. 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'}
  56. self.cookies = None
  57. self.ma = MA()
  58. self.candles_history = None
  59.  
  60.  
  61. def set_session_cookie(self, val):
  62. for k,v in self.session.cookies.iteritems():
  63. if not isinstance(v, str):
  64. self.session.cookies[k] = str(v)
  65. requests.utils.add_dict_to_cookiejar(self.session.cookies, val)
  66.  
  67. def prepare_url(self, resource):
  68. """
  69. Construct url from resource url.
  70.  
  71. :param resource: :class:`Resource
  72. <iqoption_api.resource.Resource>`.
  73.  
  74. :returns: The full url to IQ option http resource.
  75. """
  76. return '/'.join((self.https_url, resource.url))
  77.  
  78. def send_http_request(self, resource, method, data=None, params=None, headers=None):
  79. #pylint: disable=too-many-arguments
  80. """
  81. Send http request to IQ option server.
  82.  
  83. :param resource: :class:`Resource <iqoption_api.resource.Resource>`.
  84. :param str method: The HTTP request method.
  85. :param dict data: (optional) The HTTP request data.
  86. :param dict params: (optional) The HTTP request params.
  87. :param dict headers: (optional) The HTTP request headers.
  88.  
  89. :returns: :class:`Response <requests.Response>`.
  90. """
  91. url = self.prepare_url(resource)
  92. #print url
  93.  
  94. logger = logging.getLogger(__name__)
  95.  
  96. logger.debug(url)
  97.  
  98. response = self.session.request(method=method,
  99. url=url,
  100. data=data,
  101. params=params,
  102. headers=self.headers)
  103. logger.debug(response)
  104. logger.debug(response.text)
  105. logger.debug(response.headers)
  106. logger.debug(response.cookies)
  107. response.raise_for_status()
  108.  
  109. self.cookies = response.headers['Set-Cookie']
  110. #print response.cookies
  111. return response
  112.  
  113. def send_wss_request(self, name, msg):
  114. """
  115. Send wss request to IQ option server.
  116.  
  117. :param chanel: :class:`Chanel <iqoption_api.chanel.Chanel>`.
  118. :param dict data: The websocket request data.
  119.  
  120. :returns:
  121. """
  122. data = json.dumps(dict(name=name,
  123. msg=msg))
  124. print data
  125. self.websocket.send(data)
  126.  
  127. @property
  128. def serviceSubscribe(self):
  129. """
  130. Property for get IQ option login resource.
  131.  
  132. :returns: :class:`Login
  133. <iqoption_api.login.Login>`.
  134. """
  135. return ServiceSubscribe(self)
  136.  
  137. @property
  138. def optioninitall(self):
  139. """
  140. Property for get IQ option login resource.
  141.  
  142. :returns: :class:`Login
  143. <iqoption_api.login.Login>`.
  144. """
  145. return OptionInitAll(self)
  146.  
  147. @property
  148. def candles(self):
  149. """
  150. Property for get IQ option login resource.
  151.  
  152. :returns: :class:`Login
  153. <iqoption_api.login.Login>`.
  154. """
  155. return Candles(self)
  156.  
  157. @property
  158. def login(self):
  159. """
  160. Property for get IQ option login resource.
  161.  
  162. :returns: :class:`Login
  163. <iqoption_api.login.Login>`.
  164. """
  165. return Login(self)
  166.  
  167. @property
  168. def dobuyback(self):
  169. """
  170. Property for get IQ option login resource.
  171.  
  172. :returns: :class:`Login
  173. <iqoption_api.login.Login>`.
  174. """
  175. return DoBuyBack(self)
  176.  
  177. @property
  178. def appinit(self):
  179. """
  180. Property for get IQ option login resource.
  181.  
  182. :returns: :class:`Login
  183. <iqoption_api.login.Login>`.
  184. """
  185. return AppInit(self)
  186.  
  187. @property
  188. def auth(self):
  189. """
  190. Property for get IQ option login resource.
  191.  
  192. :returns: :class:`Login
  193. <iqoption_api.login.Login>`.
  194. """
  195. return Auth(self)
  196.  
  197. @property
  198. def ssid(self):
  199. """
  200. Property for get IQ option websocket ssid chanel.
  201.  
  202. :returns: :class:`Ssid
  203. <iqoption_api.ssid.Ssid>`.
  204. """
  205. return Ssid(self)
  206. @property
  207. def getregdata(self):
  208. """
  209. Property for get IQ option websocket ssid chanel.
  210.  
  211. :returns: :class:`Ssid
  212. <iqoption_api.ssid.Ssid>`.
  213. """
  214. return GetRegdata(self)
  215.  
  216. @property
  217. def balancechange(self):
  218. """
  219. Property for get IQ option login resource.
  220.  
  221. :returns: :class:`Login
  222. <iqoption_api.login.Login>`.
  223. """
  224. return BalanceChange(self)
  225.  
  226. @property
  227. def subscribe(self):
  228. """
  229. Property for get IQ option websocket subscribe chanel.
  230.  
  231. :returns: :class:`Subscribe
  232. <iqoption_api.subscribe.Subscribe>`.
  233. """
  234. return Subscribe(self)
  235.  
  236. @property
  237. def unsubscribe(self):
  238. """
  239. Property for get IQ option websocket unsubscribe chanel.
  240.  
  241. :returns: :class:`unsubscribe
  242. <iqoption_api.unsubscribe.UnSubscribe>`.
  243. """
  244. return UnSubscribe(self)
  245.  
  246. @property
  247. def setactives(self):
  248. """
  249. Property for get IQ option websocket setactives chanel.
  250.  
  251. :returns: :class:`setactives
  252. <iqoption_api.setactives.SetActives>`.
  253. """
  254. return SetActives(self)
  255.  
  256. @property
  257. def buy(self):
  258. """
  259. Property for get IQ option websocket buy chanel.
  260.  
  261. :returns: :class:`buy
  262. <iqoption_api.buy.Buy>`.
  263. """
  264. return Buy(self)
  265.  
  266. # @property
  267. # def balance(self):
  268. # """
  269. # Property for get IQ option websocket buy chanel.
  270.  
  271. # :returns: :class:`buy
  272. # <iqoption_api.buy.Buy>`.
  273. # """
  274. # return Balance(self)
  275.  
  276. @property
  277. def buyback(self):
  278. """
  279. Property for get IQ option websocket buy chanel.
  280.  
  281. :returns: :class:`buy
  282. <iqoption_api.buy.Buy>`.
  283. """
  284. return Buyback(self)
  285.  
  286. def _thread(self):
  287. """Method for websocket thread."""
  288. self.websocket.run_forever()
  289.  
  290. def connect(self):
  291. """Method for connection to api."""
  292.  
  293. # response = self.appinit()
  294. # print response.cookies.get_dict
  295.  
  296. response = self.login(self.username, self.password)
  297.  
  298. # q = response.headers['Set-Cookie'].split(";")[0].split('=')[1]
  299. # ssid = q
  300.  
  301. ssid = response.cookies["ssid"]
  302. self.login_id = response.cookies["login_id"]
  303.  
  304.  
  305. #self.set_session_cookie({"ssid": ssid, "login_id": self.login_id})
  306.  
  307. response = self.getregdata()
  308. __uat = response.cookies['__uat']
  309.  
  310. 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"})
  311.  
  312.  
  313. #print self.session.cookies
  314.  
  315.  
  316. # headers = [
  317. # ('Origin', 'https://iqoption.com'),
  318. # ('Cookie',"ssid="+ssid),
  319. # ('Cookie',"login_id="+self.login_id),
  320. # ('Cookie',"__uat="+__uat),
  321. # ('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'),
  322. # ]
  323.  
  324. # headers = [
  325. # ('Origin', 'https://iqoption.com'),
  326. # ('Cookie',"ssid="+ssid+"; login_id="+self.login_id+"; __uat="+__uat+"; platform=9; platform_version=387.2.4fc; authorization=1;"),
  327. # ('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')
  328. # ]
  329.  
  330. # headers = [
  331. # ('Cookie',"ssid="+ssid+"; login_id="+self.login_id+"; __uat="+__uat)
  332. # ]
  333.  
  334.  
  335. #получаем текущий баланс
  336. # response = self.balance()
  337. # json_data = json.loads(response.text)
  338. # print json_data['user_balance']
  339.  
  340.  
  341.  
  342. #получаем текущий баланс
  343. response = self.appinit()
  344. json_data = json.loads(response.text)
  345. #print json_data['user_balance']
  346. #получаем список балансов
  347.  
  348. response = self.optioninitall()
  349. json_data = json.loads(response.text)
  350. #print json_data
  351.  
  352. #меняем на баланс 12085209
  353. response = self.balancechange(12085209)
  354. json_data = json.loads(response.text)
  355. #print json_data
  356.  
  357. # response = self.auth()
  358. # json_data = json.loads(response.text)
  359. # print json_data['result']
  360.  
  361.  
  362. websocket = Websocket(self.wss_url)
  363.  
  364. websocket.connect()
  365.  
  366. self.websocket = websocket
  367.  
  368. websocket_thread = threading.Thread(target=self._thread)
  369. websocket_thread.daemon = True
  370. websocket_thread.start()
  371.  
  372. self.ssid(ssid)
  373.  
  374. self.subscribe("deposited")
  375. self.subscribe("activeScheduleChange")
  376. self.subscribe("tradersPulse")
  377. self.subscribe("activeCommissionChange")
  378. self.serviceSubscribe()
  379. # self.subscribe("activeCommissionChange")
  380. # self.unsubscribe("tournamentChange")
  381. self.unsubscribe("iqguard")
  382. self.unsubscribe("signal")
  383. self.unsubscribe("feedRecentBets")
  384. self.unsubscribe("feedRecentBets2")
  385. self.unsubscribe("feedTopTraders2")
  386. self.unsubscribe("feedRecentBetsMulti")
  387. self.unsubscribe("tournament")
  388. # self.subscribe("loyalty")
  389. # self.setactives([1])
  390. # self.buyback(1,3)
  391.  
  392. time.sleep(3)
  393. self.setactives([1])
  394. self.buyback(1,3)
  395.  
  396. self.unsubscribe("tournamentChange")
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403. # response = self.balancelist()
  404. # json_data = json.loads(response.text)
  405.  
  406. # balances = json_data['result']['profile']['balances']
  407. # for i in balances:
  408. # _balance = dict(id=i[u'id'], type=i[u'type'])
  409. # self.balances.append(_balance)
  410.  
  411. i = 0
  412. count_candles_get = 0
  413. while True:
  414. #self.websocket
  415. # if self.websocket.success == False:
  416. # time.sleep(3)
  417. # i = 0
  418. #time.sleep(1)
  419. if i != 1:
  420. if(self.websocket.show_value != None and self.websocket.skey != None):
  421. #self.ssid(self.websocket.ssid)
  422. #self.buy(self.websocket.time, self.websocket.show_value, self.websocket.skey)
  423. self.candles(1,60,(self.websocket.time + (60-(self.websocket.time%60)))-3600,self.websocket.time)
  424. i = 1
  425.  
  426. # if i == 1:
  427. # if (self.websocket.time%60 == 0):
  428. # self.candles(1,60,(self.websocket.time + (60-(self.websocket.time%60)))-60,self.websocket.time)
  429.  
  430. if self.websocket.success == True and self.websocket.option_id != None:
  431. print self.websocket.option_id
  432. self.websocket.success = False
  433. response = self.dobuyback(self.websocket.option_id)
  434. json_data = json.loads(response.text)
  435. print json_data
  436.  
  437. if self.websocket.candles != None and count_candles_get < self.websocket.candles_cout:
  438. #print self.websocket.candles
  439. self.candles_history = self.websocket.candles
  440. self.ma.rsi(self.websocket.candles)
  441. count_candles_get = count_candles_get + 1
  442.  
  443. # print self.websocket.message
  444. # print '####'
  445. # print self.websocket.skey
  446. # self.buy(self.websocket.time, self.websocket.show_value)
  447.  
  448. q = IQOptionAPI('iqoption.com', 1, 2)
  449. q.connect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement