Pages:
Author

Topic: scripting primedice --- getting started - page 3. (Read 4772 times)

member
Activity: 70
Merit: 10
June 17, 2015, 09:27:24 PM
#3
thanks man  Grin
member
Activity: 70
Merit: 10
Loan me, 1-5% interest :P Moooooo
June 17, 2015, 09:17:24 PM
#2
Thank you for writing this and teaching this to everyone! It cleared some things up!
legendary
Activity: 1456
Merit: 1078
I may write code in exchange for bitcoins.
April 23, 2015, 01:45:11 PM
#1
This thread is just to give folks interested in scripting primedice's api a little help to get off the ground and going.  I'm just showing a few shell scripts I've used to log in and send bets etc.  These are expected to be run on a UNIX like machine with grep, sed, curl.

So, first thing you have to do is login and get your access token.  Here's a little script that logs you in and saves your token in a file called "token".  Use this script with your actual password and username as arg1 and arg2:

Code:
#!/bin/bash
#
# log into PD, return access_token
curl -X POST --data "username=$1&password=$2" https://api.primedice.com/api/login 2>/dev/null | sed -e 's/.*access_token":"\(.*\)"}/\1/' > token

cat token

All this does is use curl to POST your login and then filters the output with sed.  The last line just prints the token to STDOUT in case that's useful for another program that calls this.

I'll show you guys one more, you can use this to send a bet using three args, the amount, the target number, and up or down ("<",">").  Note that because < and > are shell redirects for filedescriptors, you'll have to enclose the up/down in quotes.  This is the script:

Code:
#!/bin/bash

amount=$1
target=$2
condition=$3

token=`cat token`

curl -X POST --data "amount=$amount&target=$target&condition=$condition" https://api.primedice.com/api/bet?access_token=$token 2>/dev/null  | sed -e 's/.*"win":\(true\|false\).*/\1/'

Again, the assumption here is that you ran 'login' already and have your access token stored in a file called 'token'.  The script prints out "true" if you won and "false" if you lost.

These basic scripts aren't really even scripts.  They're just one liners to do the work of forumlating the curl command for you and keeping track of your access token.  I've used them to write some betting algorithms in perl.  I don't want to get too far into this, but some folks have asked me via pm for help in this so I thought this might get them started.  If you wanted to write a true bot then you'd want to do something more than use sed to filter all the output from a particular request, you'd want to use a json parser and keep track of all the data that comes back each time and update your local record accordingly.  The idea here is just to get you started if you want to play around with the api but need a few examples to get going.

Hope this helps you guys!  Have fun and safe betting!

EDIT: Realized that I should say that to use this code you'd probably want to cut-n-paste the lines into a file and then save it and make it executable.  For example, if you called my login script "login" then you'd say:

Code:
$ chmod 755 login

And after that you could run it with:

Code:
$ ./login MYUSERNAME MYPASSWORD

And similarly with the "bet" script, save it, make it executable, and call it with three args:

Code:
$ ./bet 10 50 "<"

^^ that's a bet of 10 satoshis on under 50.

Pages:
Jump to: