Skip to content

Instantly share code, notes, and snippets.

@maxisoft
Created October 27, 2013 12:05
Show Gist options
  • Save maxisoft/7181074 to your computer and use it in GitHub Desktop.
Save maxisoft/7181074 to your computer and use it in GitHub Desktop.
Alpha
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
How to :
Replace the Header value with yours,
open a browser tab and open the javascript console ("alt+caps+j") then type:
console.log(navigator.userAgent)
copy the resulting ligne
paste it @ line 60 instead of XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Getting The Token,
open a browser tab go to http://gameminer.net/ and log-in
open the javascript console then type:
$.getScript("//cdn.jsdelivr.net/jquery.cookie/1.3.1/jquery.cookie.js", function(){console.log($.cookie("token"))})
copy the resulting ligne
paste it @ line 61 instead of YYYYYYYYYYYYYYYYYYYYYYYYY
Then start the python script and enjoy it :)
LICENSE
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
"""
__author__ = 'maxisoft'
import sys
import urllib
import urllib2
import re
import cookielib
import pickle
import copy
import json
import time
from functools import total_ordering
import pyquery
@total_ordering
class GiveAway(object):
MAX_PAGE = 3
USE_MY_GOLD = False
MY_HEADER = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
MY_TOKEN = "YYYYYYYYYYYYYYYYYYYYYYYYY"
def __init__(self, price, mtype, xsrf, action_url, entries, timesec):
#TODO private or property
self.xsrf = xsrf
self.mtype = mtype
self.price = price
self.actionurl = action_url
self.entries = entries
self.timesec = timesec
self.priorindex = self.compute_prior_index()
def __hash__(self):
return hash(self.actionurl)
#should never equal other
def __eq__(self, other):
assert isinstance(other, GiveAway)
return self.actionurl == other.actionurl
# 0 => best
def compute_prior_index(self):
ret = (self.entries * 2) ** 5
ret += self.price * 10
ret += self.timesec ** 2
return ret
def __lt__(self, other):
assert isinstance(other, GiveAway)
return self.priorindex < other.priorindex
@staticmethod
def parsetime(s):
s = unicode(s).strip()
minutes_found = "minutes" in s
if u"less than a minute" in s:
return 1
if minutes_found and "hour" in s:
st = s.split(' ')
h, _, m, _ = st
h = int(h) * 60 * 60
m = int(m) * 60
return h + m
if minutes_found:
return int(s.split(' ')[0]) * 60
return None
class Coal:
pass
class Gold:
pass
class MyRequest(urllib2.Request):
"""
urllib2.Request with other header default value.
"""
request_headers = {
"User-Agent": GiveAway.MY_HEADER,
"Accept-Language": r"fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4",
"Accept": r"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Connection": r"keep-alive",
"Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3"
}
def __init__(self, url, data=None, headers=request_headers, origin_req_host=None, unverifiable=False):
urllib2.Request.__init__(self, url, data, headers, origin_req_host, unverifiable)
class StringCookieJar(cookielib.CookieJar):
def __init__(self, string=None, policy=None):
cookielib.CookieJar.__init__(self, policy)
if string:
self._cookies = pickle.loads(string)
def dump(self):
return pickle.dumps(self._cookies)
def getCookies(self):
return self._cookies
class BotUrlOpener(object):
def __init__(self, proxy=None, stringcookie=None):
self.state = "build"
self.handler = []
self.cookiejar = StringCookieJar()
self.add_handler(urllib2.HTTPCookieProcessor(self.cookiejar))
self.proxy = proxy
if self.proxy:
if not isinstance(self.proxy, urllib2.ProxyHandler):
self.proxy = urllib2.ProxyHandler(proxy)
self.add_handler(self.proxy)
self.__urlo = urllib2.build_opener(*self.handler)
self.open = self.__urlo.open
self.state = "run"
def clear_cookies(self):
self.cookiejar.clear()
def add_handler(self, h):
assert self.state == "build"
self.handler.append(h)
def get_ip(self):
data = str(self.open('http://checkip.dyndns.com/').read())
return re.compile(r'Address: (\d+\.\d+\.\d+\.\d+)').search(data).group(1)
class GameMinerBot:
def __init__(self):
self.urlo = BotUrlOpener()
def login(self, account=GiveAway.MY_TOKEN):
request = MyRequest("http://gameminer.net/")
url = self.urlo.open(request, timeout=100)
url.read(1)
c = copy.copy(b.urlo.cookiejar.getCookies()['gameminer.net']['/']['lang'])
c.name = "token"
c.value = account
self.urlo.cookiejar.getCookies()['gameminer.net']['/']['token'] = c
request = MyRequest("http://gameminer.net/")
url = self.urlo.open(request, timeout=100)
self.page = url.read()
return "Messages" in self.page
def vote(self):
page = 0
lastgiveaways = True
allgiveaways = set()
base_url = r"http://gameminer.net/?type=any&enter_price=on&filter_entered=on&q=&sortby=finish&order=asc&sandbox_page={0}&&coal_page={0}"
while lastgiveaways and page < GiveAway.MAX_PAGE:
page += 1
request = MyRequest(base_url.format(page))
url = self.urlo.open(request, timeout=100)
self.page = url.read()
d = pyquery.PyQuery(self.page)
giveaways = set()
for giveaway in (d(".giveaway")).items():
#print "---------------------------"*2, giveaway, "---------------------------"*2
price = giveaway.attr['data-price']
price = int(price)
mtype = None
tmp = giveaway.attr['data-type']
if tmp == "coal":
mtype = Coal
elif tmp == "gold":
if not GiveAway.USE_MY_GOLD:
continue
mtype = Gold
else:
continue
btn_value = None
xsrf = None
action_url = None
try:
item = giveaway(".giveaway__action > form").items().next()
action_url = item.attr['action']
if not unicode(action_url).startswith(u"/giveaway/enter/"):
continue
tmp = item('input').items()
xsrf = tmp.next()
submit = tmp.next()
xsrf = xsrf.attr['value']
btn_value = submit.attr['value']
if unicode(btn_value).lower() != u'Join giveaway'.lower():
continue
except:
continue
entries = giveaway('p>span.g-white').items().next().html() # TODO better selector
entries = int(entries)
if entries < 0:
continue
timestr = giveaway('.giveaway__timeleft').items().next()
timestr("span").remove()
timestr = timestr.html()
timesec = GiveAway.parsetime(timestr)
if timesec is None:
continue
giveaways.add(GiveAway(price, mtype, xsrf, action_url, entries, timesec))
lastgiveaways = giveaways
allgiveaways |= giveaways # union
freegiveaways = {giveaway for giveaway in allgiveaways if giveaway.price == 0}
allgiveaways -= freegiveaways # difference
proceed_giveaways = freegiveaways
if allgiveaways:
best = min(allgiveaways)
proceed_giveaways.add(best)
ret = 0
for giveaway in proceed_giveaways:
data = urllib.urlencode({"_xsrf": giveaway.xsrf, "json": "true"})
request = MyRequest("http://gameminer.net" + giveaway.actionurl, data)
try:
url = self.urlo.open(request)
j = json.load(url)
except urllib2.URLError: # most of the time because can't join giveaway
pass
else:
ret += 1
finally:
time.sleep(1.5) # don't spam :)
return ret
if __name__ == "__main__":
#TODO getopt
b = GameMinerBot()
if not b.login():
print("Can't Login !")
sys.exit()
cnt = b.vote()
if cnt:
print("Joined %i giveaway(s) :)" % cnt)
else:
print ("No giveaways :(")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment