Voting part 2

This commit is contained in:
Bastian Kleineidam 2013-04-08 21:20:01 +02:00
parent e762f269b7
commit 4528281ddd
4 changed files with 78 additions and 5 deletions

View file

@ -24,4 +24,4 @@ Freeware = AppName+u""" comes with ABSOLUTELY NO WARRANTY!
This is free software, and you are welcome to redistribute it
under certain conditions. Look at the file `COPYING' within this
distribution."""
VoteUrl = "XXX"

View file

@ -3,8 +3,9 @@
# Copyright (C) 2012-2013 Bastian Kleineidam
import requests
import time
from . import loader
from .util import fetchUrl, fetchUrls, getPageContent, makeSequence
from . import loader, configuration
from .util import (fetchUrl, fetchUrls, getPageContent, makeSequence,
get_system_uid, urlopen)
from .comic import ComicStrip
from .output import out
from .events import getHandler
@ -226,7 +227,13 @@ class _BasicScraper(object):
def vote(self):
"""Cast a public vote for this comic."""
pass # XXX
url = configuration.VoteUrl
uid = get_system_uid()
data = {"comic": self.getName(), "uid": uid}
page = urlopen(url, self.session, data=data, stream=False)
answer = page.text
if "voted" not in answer:
raise ValueError("vote error %r" % answer)
def find_scraperclasses(comic, multiple_allowed=False):

View file

@ -21,6 +21,7 @@ import cgi
import re
import traceback
import time
import subprocess
try:
from HTMLParser import HTMLParser
except ImportError:
@ -59,6 +60,62 @@ if hasattr(requests, 'adapters'):
requests.adapters.DEFAULT_RETRIES = MaxRetries
def get_system_uid():
"""Get a (probably) unique ID to identify a system.
Used to differentiate votes.
"""
try:
if os.name == 'nt':
return get_nt_system_uid()
if sys.platform == 'darwin':
return get_osx_system_uid()
except Exception:
return get_mac_uid()
else:
return get_mac_uid()
def get_nt_system_uid():
"""Get the MachineGuid from
HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\MachineGuid
"""
try:
import _winreg as winreg
except ImportError:
import winreg
lm = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
try:
key = winreg.OpenKey(lm, r"Software\Microsoft\Cryptography")
try:
return winreg.QueryValueEx(key, "MachineGuid")[0]
finally:
key.Close()
finally:
lm.Close()
def get_osx_system_uid():
"""Get the OSX system ID.
$ system_profiler |grep "r (system)"
Serial Number (system): C24E1322XXXX
"""
res = backtick(["system_profile"]).splitlines()
for line in res:
if "r (system)" in line:
return line.split(':', 1)[1].strip()
raise ValueError("Could not find system number in %r" % res)
def get_mac_uid():
"""Get the MAC address of the system."""
import uuid
return uuid.getnode()
def backtick (cmd, encoding='utf-8'):
"""Return decoded output from command."""
data = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
return data.decode(encoding)
def unicode_safe(text, encoding=UrlEncoding, errors='ignore'):
"""Decode text to Unicode if not already done."""
try:

View file

@ -4,7 +4,7 @@
import re
from unittest import TestCase
from dosagelib.util import normaliseURL, unescape, tagre
from dosagelib.util import normaliseURL, unescape, tagre, get_system_uid
class URLTest(TestCase):
@ -50,3 +50,12 @@ class RegexTest(TestCase):
self.assertEqual(match.group(1), value)
else:
self.assertFalse(match, "%s should not match %s" % (matcher.pattern, text))
class UidTest(TestCase):
"""
Tests for unique system IDs.
"""
def test_system_uid(self):
self.assertTrue(get_system_uid())