Created
October 3, 2012 18:17
-
-
Save orng/3828743 to your computer and use it in GitHub Desktop.
Revisions
-
orng created this gist
Oct 3, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,110 @@ #!/usr/bin/python # -*- coding: utf-8 -*- # Author: Orn Gudjonsson # Methods for voting for an artist on ruv.is/topp30 #NOTE: if you vote to often your ip address will be blocked. # you could try changing the script to automatically vote for your artist once every # hour or so, but I have no idea if that works. from lxml import etree import urllib2 import random import sys import re NUMBERS = { u'núll': 0, u'einn': 1, u'tveir': 2, u'þrír': 3, u'fjórir': 4, u'fimm': 5, u'sex': 6, u'sjö': 7, u'átta': 8, u'níu': 9, } #stolen from http://stackoverflow.com/a/846931/611292 def win32_unicode_argv(): """Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode strings. Versions 2.x of Python don't support Unicode in sys.argv on Windows, with the underlying Windows API instead replacing multi-byte characters with '?'. """ from ctypes import POINTER, byref, cdll, c_int, windll from ctypes.wintypes import LPCWSTR, LPWSTR GetCommandLineW = cdll.kernel32.GetCommandLineW GetCommandLineW.argtypes = [] GetCommandLineW.restype = LPCWSTR CommandLineToArgvW = windll.shell32.CommandLineToArgvW CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)] CommandLineToArgvW.restype = POINTER(LPWSTR) cmd = GetCommandLineW() argc = c_int(0) argv = CommandLineToArgvW(cmd, byref(argc)) if argc.value > 0: # Remove Python executable and commands if present start = argc.value - len(sys.argv) return [argv[i] for i in xrange(start, argc.value)] sys.argv = win32_unicode_argv() BASE_URL = 'http://www.ruv.is/topp30' POST_URL = 'http://servefir.ruv.is/topp30/vista.php' VOTE_URL = 'lag_unid%5B%5D={id}&skjal%5B%5D=ip&skjal%5B%5D=poll&skjal%5B%5D=nylog&handvirkt=nylogipogpoll&nl_flytjandi=&nl_lag=&nafn=&simi=&email=&ca={captcha}' parser = etree.HTMLParser() def findId(name): page = urllib2.urlopen(BASE_URL) root = etree.fromstring(page.read().decode('utf-8'), parser) checkboxes = root.findall('.//ul[@class="kosning"]') for checkbox in checkboxes: label = checkbox.find('.//label[@class="spilun_flytjandi"]') bandName = label.text if label is not None else "" if bandName.lower() == name.lower(): voteId = label.get('id')[3:] return voteId print etree.tostring(root) return None def getCaptcha(): a = random.randint(1,8) b = random.randint(1,6) return a + b def vote(name): voteId = findId(name) if voteId is None: print 'Tokst ekki ad finna hljomsveitina %s' % name return None voteCaptcha = getCaptcha() data = VOTE_URL.format(id=voteId, captcha=voteCaptcha) resp = urllib2.urlopen(POST_URL, data) return resp if __name__ == '__main__': args = sys.argv if args[1] is not None: resp = vote(args[1]) if resp is not None: blockString = 'loka fyrir kosningu' respString = resp.read().decode('utf-8') blocked = re.search(blockString, respString) if blocked is not None: print 'Your ip address has been blocked!' else: print respString else: print "You need to supply the name of an artist to vote for!"