Skip to content

Commit

Permalink
removed ConstantType and associated cruft
Browse files Browse the repository at this point in the history
  • Loading branch information
infothrill committed Oct 28, 2013
1 parent 247c0ab commit 8e4c50e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 133 deletions.
16 changes: 8 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ Usage
=====
.. code-block:: python
from bitsnoop import Fakeskan, FAKESCAN
from bitsnoop import fakeskan
fakeskan = Fakeskan() # create a fakeskan object with default URL
fk = fakeskan.Fakeskan() # create a fakeskan object with default URL
if fakeskan("43DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A") == FAKESCAN.CODE.VERIFIED:
if fk("43DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A") == fakeskan.VERIFIED:
print("This torrent is verified!")
# since bitsnoop.com implements some form of rate limiting on queries,
# we provide a minimal caching interface:
from bitsnoop import FakeskanCached
from bitsnoop import fakeskan
cache = {}
fakeskan = FakeskanCached(cache=cache)
if fakeskan("43DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A") == FAKESCAN.CODE.VERIFIED:
fk = fakeskan.FakeskanCached(cache=cache)
if fk("43DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A") == fakeskan.VERIFIED:
print("This torrent is verified!")
# Alternatively, the cache can be a shelve object for example:
import shelve
cache = shelve.open("fakeskan")
fakeskan = FakeskanCached(cache=cache)
if fakeskan("43DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A") == FAKESCAN.CODE.VERIFIED:
fk = fakeskan.FakeskanCached(cache=cache)
if fk("43DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A") == fakeskan.VERIFIED:
print("This torrent is verified!")
# beware of thread-seafety with the cache object. shelve is a toy!
Expand Down
2 changes: 0 additions & 2 deletions bitsnoop/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# -*- coding: utf-8 -*-

from .fakeskan import Fakeskan, FakeskanCached, FAKESKAN
54 changes: 0 additions & 54 deletions bitsnoop/constant_type.py

This file was deleted.

50 changes: 23 additions & 27 deletions bitsnoop/fakeskan.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import requests

from bitsnoop.constant_type import Constants


if sys.version_info >= (3, 0):
unicode = str
Expand All @@ -18,24 +16,21 @@
FAKESKAN_URL = "http://bitsnoop.com/api/fakeskan.php"


class FAKESKAN(Constants):
class CODE:
ERROR = 0
NOTFOUND = 1
VERIFIED = 2
GOOD = 3
NONE = 4
BAD = 5
FAKE = 6
ERROR = 0
NOTFOUND = 1
VERIFIED = 2
GOOD = 3
NONE = 4
BAD = 5
FAKE = 6

class DESCRIPTION:
ERROR = "hash not specified or internal error has occured. If you did specify hash, retry request in 10-15 minutes"
NOTFOUND = "torrent does not exist in index"
VERIFIED = "torrent is verified"
GOOD = "torrent has some 'good' votes, not verified yet"
NONE = "no votes or too little of them to decide"
BAD = "some 'bad' votes, not fake yet"
FAKE = "torrent is fake"
ERROR_DESCRIPTION = "hash not specified or internal error has occured. If you did specify hash, retry request in 10-15 minutes"
NOTFOUND_DESCRIPTION = "torrent does not exist in index"
VERIFIED_DESCRIPTION = "torrent is verified"
GOOD_DESCRIPTION = "torrent has some 'good' votes, not verified yet"
NONE_DESCRIPTION = "no votes or too little of them to decide"
BAD_DESCRIPTION = "some 'bad' votes, not fake yet"
FAKE_DESCRIPTION = "torrent is fake"


class FakeskanCached(object):
Expand Down Expand Up @@ -68,7 +63,7 @@ def call(self, key):
logging.info("no fakeskan cache for '%s': querying!", key)
self._cache[key] = (fakeskan(key), datetime.now())
entry = self._cache[key]
if entry[0] == FAKESKAN.CODE.ERROR:
if entry[0] == ERROR:
# don't cache errors
del self._cache[key]
return entry
Expand All @@ -84,6 +79,7 @@ class Fakeskan(object):
api URL. Essentially, this is functionally the same than
from functools import partial
fakeskan = partial(fakeskan, url=FAKESKAN_URL)
but doesn't suffer from implementation issues
'''
def __init__(self, url=FAKESKAN_URL):
self._url = url
Expand All @@ -102,13 +98,13 @@ def fakeskan(btih, url=FAKESKAN_URL):
assert type(btih) in (str, unicode), "the provided bittorrent info hash must be a string, not %s!" % str(type(btih))
assert len(btih) == 40, "Length of bittorrent info hash should be 40 but isn't: '%s'" % btih
_fakeskan_map = {
"ERROR": FAKESKAN.CODE.ERROR,
"NOTFOUND": FAKESKAN.CODE.NOTFOUND,
"VERIFIED": FAKESKAN.CODE.VERIFIED,
"GOOD": FAKESKAN.CODE.GOOD,
"NONE": FAKESKAN.CODE.NONE,
"BAD": FAKESKAN.CODE.BAD,
"FAKE": FAKESKAN.CODE.FAKE,
"ERROR": ERROR,
"NOTFOUND": NOTFOUND,
"VERIFIED": VERIFIED,
"GOOD": GOOD,
"NONE": NONE,
"BAD": BAD,
"FAKE": FAKE,
}
headers = {'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
r = requests.get(url, params={"hash": btih, "json": "1"}, headers=headers, timeout=120)
Expand Down
28 changes: 0 additions & 28 deletions bitsnoop/tests/constant_type.py

This file was deleted.

28 changes: 14 additions & 14 deletions bitsnoop/tests/fakeskan.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)

from bitsnoop import Fakeskan, FakeskanCached, FAKESKAN
from bitsnoop import fakeskan
from .server import BitsnoopFakeSkanApp


Expand All @@ -22,18 +22,18 @@ def setUpClass(cls):
cls.server.start()

def test_fakeskan(self):
fakeskan = Fakeskan(self.url)
fk = fakeskan.Fakeskan(self.url)
test_data = {
"03DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A": FAKESKAN.CODE.ERROR,
"13DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A": FAKESKAN.CODE.NOTFOUND,
"23DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A": FAKESKAN.CODE.VERIFIED,
"33DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A": FAKESKAN.CODE.GOOD,
"43DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A": FAKESKAN.CODE.NONE,
"53DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A": FAKESKAN.CODE.BAD,
"63DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A": FAKESKAN.CODE.FAKE,
"03DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A": fakeskan.ERROR,
"13DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A": fakeskan.NOTFOUND,
"23DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A": fakeskan.VERIFIED,
"33DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A": fakeskan.GOOD,
"43DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A": fakeskan.NONE,
"53DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A": fakeskan.BAD,
"63DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A": fakeskan.FAKE,
}
for key in test_data:
self.assertEqual(test_data[key], fakeskan(key))
self.assertEqual(test_data[key], fk(key))

@classmethod
def tearDownClass(cls):
Expand All @@ -49,8 +49,8 @@ def test_fakeskan_cache(self):
# ever making any http connections
cache = {
"43DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A":
(FAKESKAN.CODE.GOOD, datetime.datetime.now())
(fakeskan.GOOD, datetime.datetime.now())
}
fakeskan = FakeskanCached(cache, url="http://nonexistant.example.com/")
result = fakeskan("43DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A")
self.assertEqual(FAKESKAN.CODE.GOOD, result)
fk = fakeskan.FakeskanCached(cache, url="http://nonexistant.example.com/")
result = fk("43DBF6EBC059CD97ACAE7CAF308A0E050A7EC51A")
self.assertEqual(fakeskan.GOOD, result)

0 comments on commit 8e4c50e

Please sign in to comment.