Peerbet python bot


SUBMITTED BY: Guest

DATE: Nov. 27, 2013, 6:51 p.m.

FORMAT: Text only

SIZE: 11.8 kB

HITS: 953

  1. import cookielib, urllib2,urllib,random,time,sys,math
  2. #peerbet login username and password (They will be echoed to the screen!)
  3. username='yourusername'
  4. password='yourpassword'
  5. #numraffles: number of raffles to create
  6. #max_lose: max losing streak before quitting
  7. #satoshi: price of single ticket
  8. #ret_ratio: return ratio
  9. #buy_tickets: number of tickets to buy
  10. #Examples:
  11. #Thousand Mode (5000/10500)
  12. #numraffles=10
  13. #max_lose=9
  14. #satoshi=2
  15. #ret_ratio=2.1
  16. #buy_tickets=5000
  17. #Hundred Mode (50/105)
  18. #numraffles=10
  19. #max_lose=9
  20. #satoshi=100
  21. #ret_ratio=2.1
  22. #buy_tickets=50
  23. #100/110
  24. #numraffles=20
  25. #max_lose=2
  26. #satoshi=100
  27. #ret_ratio=1.1
  28. #buy_tickets=100
  29. #Set to 0 if you want to monitor old raffles
  30. ignore=1
  31. #price of single ticket after the first loss (FIXME)
  32. #satoshi*=random.uniform(2, 3)
  33. satoshidbl=satoshi
  34. #not recommended to modify anything after this line
  35. #Dont tread on me
  36. args_num=len(sys.argv)
  37. print 'args_num = ' + str(args_num)
  38. #if args_num==1:
  39. # sys.exit()
  40. sessionkey=str()
  41. if args_num>1:
  42. if sys.argv[1] is not None:
  43. sessionkey=str(sys.argv[1])
  44. total_tickets=int(math.floor(float(ret_ratio)*float(buy_tickets)))
  45. ticketpricebase=float(satoshi)/100000000
  46. ticketpricedbl=float(satoshidbl)/100000000
  47. stoppage=ticketpricebase*total_tickets
  48. rlose_amt=ticketpricebase*float(buy_tickets)
  49. #rwin_amt=(total_tickets-buy_tickets)*ticketpricebase
  50. rwin_amt=ticketpricebase*float(total_tickets)
  51. ticketprice=ticketpricebase
  52. sell_tickets=total_tickets-buy_tickets
  53. print 'Key: ' + str(sessionkey)
  54. print 'Username: ' + str(username)
  55. print 'password: ' + str(password)
  56. print 'numraffles: ' + str(numraffles)
  57. print 'max_lose: ' + str(max_lose)
  58. print 'satoshi: ' + str(satoshi)
  59. print 'satoshidbl: ' + str(satoshidbl)
  60. print 'ret_ratio: ' + str(ret_ratio)
  61. print 'buy_tickets: ' + str(buy_tickets)
  62. print 'sell_tickets: ' + str(sell_tickets)
  63. print 'total_tickets: ' + str(total_tickets)
  64. print 'ticketpricebase: ' + str('%.8f' % ticketpricebase)
  65. print 'ticketpricedbl: ' + str('%.8f' % ticketpricedbl)
  66. print 'total_tickets_price: ' + str('%.8f' % (float(total_tickets) * ticketpricebase))
  67. print 'stoppage: ' + str('%.8f' % stoppage)
  68. print 'rlose_amt: ' + str('%.8f' % rlose_amt)
  69. print 'rwin_amt: ' + str('%.8f' % rwin_amt)
  70. print 'ticketprice: ' + str('%.8f' % ticketprice)
  71. print 'Win percentage: ' + str((float(buy_tickets)/float(total_tickets))*float(100))
  72. print 'Lose percentage: ' + str((float(sell_tickets)/float(total_tickets))*float(100))
  73. print 'Sleeping for 10 sec check the values, break if incorrect'
  74. time.sleep(10)
  75. cj = cookielib.CookieJar()
  76. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  77. if args_num==1:
  78. params = urllib.urlencode({'username': str(username), 'password': str(password)})
  79. print "https://peerbet.org/api.php?method=login&%s" % params
  80. r = opener.open("https://peerbet.org/api.php?method=login&%s" % params)
  81. loginsession=r.read()
  82. print loginsession
  83. loginsession=eval(loginsession)
  84. sessionkey=str(loginsession['key'])
  85. keyparam = urllib.urlencode({'key':sessionkey})
  86. lose_count=0;
  87. raffle_id_list = []
  88. raffleid=0
  89. lastmessageid=0
  90. #Uncomment to allow getting my raffle count
  91. """
  92. time.sleep(random.uniform(5, 7))
  93. f = opener.open("https://peerbet.org/api.php?method=getmyrafflecount&%s" % keyparam)
  94. rafflecount=f.read()
  95. print rafflecount
  96. rafflecount=eval(rafflecount)
  97. print 'My Raffles Count:' + str(rafflecount['count'])
  98. """
  99. #allow monitoring open raffles
  100. myraffles=[]
  101. if ignore==0:
  102. time.sleep(random.uniform(5, 7))
  103. params = urllib.urlencode({'key':sessionkey})
  104. print "https://peerbet.org/api.php?method=getmyraffles&%s" % params
  105. f = opener.open("https://peerbet.org/api.php?method=getmyraffles&%s" % params)
  106. rr=f.read()
  107. print rr
  108. myraffles=eval(rr)
  109. #status "open" - there are unsold tickets in the game, "waiting" - waiting for the next Bitcoin block, "win" - current user won the game, "lose" - current user lost the game, "finished" if the raffle is finished but current user did not participated
  110. for m in myraffles:
  111. if m['status']=='open':
  112. if m['tickets_total'] == str(total_tickets):
  113. raffle_id_list.append(m['raffle_id'])
  114. for raffleid in raffle_id_list:
  115. print 'Open raffle: ' + str(raffleid)
  116. print 'Total already open: ' + str(len(raffle_id_list))
  117. time.sleep(random.uniform(2, 3))
  118. totalLoss=0
  119. totalWin=0
  120. while True:
  121. params = urllib.urlencode({'key':sessionkey})
  122. print "https://peerbet.org/api.php?method=getuserinfo&%s" % params
  123. f = opener.open("https://peerbet.org/api.php?method=getuserinfo&%s" % params)
  124. userinfo=f.read()
  125. print userinfo
  126. userinfo=eval(userinfo)
  127. balance=float(userinfo['balance'])
  128. print 'balance: ' + str('%.8f' % balance)
  129. if lose_count>=max_lose:
  130. print 'lose count greater than max lose'
  131. break
  132. if stoppage>=balance:
  133. print 'stoppage greater than balance'
  134. break
  135. else:
  136. #params = urllib.urlencode({'key':sessionkey, 'tickets': total_tickets, 'price': ticketprice,'expire':'1','password':'rafpassword','instant':'1'})
  137. #for x in range(0, 3):
  138. while len(raffle_id_list) < numraffles:
  139. #time.sleep(random.uniform(60*60, 2*60*60))
  140. sleeptime=random.uniform(5, 10)
  141. print 'sleeping for ' + str(sleeptime)
  142. time.sleep(sleeptime)
  143. print 'Create new Raffle'
  144. params = urllib.urlencode({'key':sessionkey, 'tickets': str(total_tickets), 'price': str('%.8f' % ticketprice),'instant':'0'})
  145. print 'https://peerbet.org/api.php?method=createraffle&%s' % params
  146. raffle=opener.open('https://peerbet.org/api.php?method=createraffle&%s' % params)
  147. raf= raffle.read()
  148. print raf
  149. raf=eval(raf)
  150. raffleid=raf['raffle_id']
  151. raffle_id_list.append(raffleid)
  152. print 'Buy tickets in new Raffle'
  153. params = urllib.urlencode({'key':sessionkey, 'raffle': raffleid, 'tickets': buy_tickets})
  154. print 'https://peerbet.org/api.php?method=buytickets&%s' % params
  155. bought=opener.open('https://peerbet.org/api.php?method=buytickets&%s' % params)
  156. bou=bought.read()
  157. print bou
  158. balance=balance-rlose_amt
  159. print 'balance: ' + str('%.8f' % balance)
  160. ticketprice=ticketpricebase
  161. #bou=eval(bou)
  162. while len(raffle_id_list) >= numraffles:
  163. temp_raffle_list=[]
  164. for raffleid in raffle_id_list:
  165. temp_raffle_list.append(raffleid)
  166. for raffleid in temp_raffle_list:
  167. time.sleep(random.uniform(5, 7))
  168. params = urllib.urlencode({'key':sessionkey, 'raffle': raffleid})
  169. print 'https://peerbet.org/api.php?method=getraffleinfo&%s' % params
  170. f=opener.open('https://peerbet.org/api.php?method=getraffleinfo&%s' % params)
  171. raffle=f.read()
  172. print raffle
  173. raffle=eval(raffle)
  174. print 'Raffle Id: ' + str(raffle['raffle_id']) + ' Ticket price: ' + str(raffle['ticket_price']) + ' Tickets Total: ' + str(raffle['tickets_total']) + ' Tickets Sold: ' + str(raffle['tickets_sold']) + ' My Tickets: ' + str(raffle['my_tickets_count']) + ' My List: ' + str(raffle['my_ticket_list']) + ' Status: ' + str(raffle['status'])
  175. if raffle['status']=='win':
  176. ticketprice=ticketpricebase
  177. lose_count=0
  178. totalWin+=1
  179. print 'Win! Winning ticket: ' + str(raffle['winning_ticket'])
  180. balance=balance+rwin_amt
  181. raffle_id_list.remove(raffleid)
  182. elif raffle['status']=='lose':
  183. if lose_count==0:
  184. ticketprice=ticketpricedbl
  185. else:
  186. ticketprice=ticketpricebase
  187. lose_count+=1
  188. totalLoss+=1
  189. print 'Lose! Winning ticket: ' + str(raffle['winning_ticket'])
  190. raffle_id_list.remove(raffleid)
  191. elif raffle['status']=='finished':
  192. raffle_id_list.remove(raffleid)
  193. elif raffle['status']=='waiting':
  194. raffle_id_list.remove(raffleid)
  195. winB=((float(totalWin)*rwin_amt)-(float(totalWin)*rlose_amt))
  196. loseB=float(totalLoss)*rlose_amt
  197. diffB=winB-loseB
  198. totPlayed=totalLoss+totalWin
  199. print 'Balance: ' + str('%.8f' % balance) + ' Lose Count : ' + str(lose_count) + ' TotalWin : ' + str(totalWin) + ' (' + str('%.8f' % winB) + ') TotalLoss : ' + str(totalLoss) + ' (' + str('%.8f' % loseB) + ') Diff (' + str('%.8f' % diffB) + ') Open tickets ' + str(len(raffle_id_list))
  200. if totPlayed>0:
  201. print 'Percentage Win : ' + str((float(totalWin)/float(totPlayed))*float(100))
  202. print 'Percentage Lose: ' + str((float(totalLoss)/float(totPlayed))*float(100))
  203. else:
  204. print 'Percentage: Undefined'
  205. print 'Balance in game: ' + str('%.8f' % (float(len(raffle_id_list))*rlose_amt))
  206. """
  207. if lastmessageid == 0:
  208. params = urllib.urlencode({'key':sessionkey})
  209. else:
  210. params = urllib.urlencode({'key':sessionkey, 'lastmessageid': lastmessageid})
  211. print "https://peerbet.org/api.php?method=getchatmessages&%s" % params
  212. f = opener.open("https://peerbet.org/api.php?method=getchatmessages&%s" % params)
  213. chat=f.read()
  214. chat=eval(chat)
  215. chat=chat['message_list']
  216. chat=eval(chat)
  217. #status "open" - there are unsold tickets in the game, "waiting" - waiting for the next Bitcoin block, "win" - current user won the game, "lose" - current user lost the game, "finished" if the raffle is finished but current user did not participated
  218. for c in chat:
  219. lastmessageid=c['message_id']
  220. print c['bot'] + ' ' + c['posted_by'] + ':' + c['message_body']
  221. """
  222. if len(raffle_id_list) >= numraffles:
  223. sleeptime=random.uniform(60, 2*60)
  224. print 'Waiting ' + str(sleeptime) + 's ...'
  225. time.sleep(sleeptime)
  226. else:
  227. sleeptime=random.uniform(10, 30)
  228. print 'Waiting ' + str(sleeptime) + 's ...'
  229. time.sleep(sleeptime)

comments powered by Disqus