From ea36dd2639b7b562aee9aa587489be9bf9bc4c73 Mon Sep 17 00:00:00 2001 From: zyp Date: Thu, 11 May 2006 16:34:59 +0000 Subject: [project @ zyp-20060511163459-2e238e10d316fe12] [project @ 29] Adapted pyanidb to distutils. --- anidb.py | 95 -------------------------------------------------- anidb_add | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++ anidb_add.py | 85 --------------------------------------------- ed2k/Makefile | 27 --------------- ed2k/__init__.py | 11 ------ ed2k/ed2k.cpp | 68 ------------------------------------ ed2k/ed2k.h | 19 ---------- ed2k/ed2k_wrapper.cpp | 10 ------ pyanidb.conf | 2 +- pyanidb/__init__.py | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 9 +++++ 11 files changed, 201 insertions(+), 316 deletions(-) delete mode 100644 anidb.py create mode 100644 anidb_add delete mode 100644 anidb_add.py delete mode 100644 ed2k/Makefile delete mode 100644 ed2k/__init__.py delete mode 100644 ed2k/ed2k.cpp delete mode 100644 ed2k/ed2k.h delete mode 100644 ed2k/ed2k_wrapper.cpp create mode 100644 pyanidb/__init__.py create mode 100755 setup.py diff --git a/anidb.py b/anidb.py deleted file mode 100644 index 1a36ec3..0000000 --- a/anidb.py +++ /dev/null @@ -1,95 +0,0 @@ -import socket, time - -protover = 3 -client = 'pyanidb' -clientver = 2 - -states = { - 'unknown': 0, - 'hdd': 1, - 'cd': 2, - 'deleted': 3, - 'shared': 4, - 'release': 5 -} - -class AniDBError(Exception): - pass - -class AniDBTimeout(AniDBError): - pass - -class AniDBLoginError(AniDBError): - pass - -class AniDBUserError(AniDBLoginError): - pass - -class AniDBReplyError(AniDBError): - pass - -class AniDBUnknownFile(AniDBError): - pass - -class AniDB: - def __init__(self, username, password, localport = 1234, server = ('api.anidb.info', 9000)): - self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - self.sock.bind(('0.0.0.0', localport)) - self.sock.settimeout(10) - self.username = username.lower() - self.password = password - self.server = server - self.session = '' - self.lasttime = 0 - self.new_version = False - def __del__(self): - self.logout() - self.sock.close() - def execute(self, data): - t = time.time() - if t < self.lasttime + 2: - time.sleep(self.lasttime + 2 - t) - self.lasttime = time.time() - self.sock.sendto(data + '\n', self.server) - try: - data = self.sock.recv(8192).split('\n') - except socket.timeout: - raise AniDBTimeout() - code, text = data[0].split(' ', 1) - data = data[1:-1] - code = int(code) - return code, text, data - def ping(self): - t = time.time() - try: - return self.execute('PING')[0] == 300 and time.time() - t or None - except AniDBTimeout: - return None - def auth(self): - code, text, data = self.execute('AUTH user=%s&pass=%s&protover=%d&client=%s&clientver=%d' % (self.username, self.password, protover, client, clientver)) - if code in [200, 201]: - self.session = text.split(' ', 1)[0] - if code == 201: - self.new_version = True - elif code == 500: - raise AniDBUserError() - else: - raise AniDBReplyError(code, text) - def logout(self): - if self.session: - try: - self.execute('LOGOUT s=%s' % (self.session)) - self.session = '' - except AniDBError: - pass - def add_hash(self, size, ed2k, state = states['hdd'], viewed = False, source = '', storage = '', other = ''): - while 1: - code, text, data = self.execute('MYLISTADD s=%s&size=%d&ed2k=%s&state=%d&viewed=%d&source=%s&storage=%s&other=%s' % (self.session, size, ed2k, state, viewed and 1 or 0, source, storage, other)) - if code in [210, 310]: - return - elif code in [501, 506]: - self.auth() - elif code == 320: - raise AniDBUnknownFile() - else: - raise AniDBReplyError(code, text) diff --git a/anidb_add b/anidb_add new file mode 100644 index 0000000..04dc1df --- /dev/null +++ b/anidb_add @@ -0,0 +1,96 @@ +#!/usr/bin/env python + +import pyanidb as anidb +import ConfigParser, os, sys, thread, time, getpass, multihash + +num_threads = 0 + +def file_hash(name): + e = multihash.Ed2k() + f = open(name) + data = f.read(32768) + while data: + e.update(data) + data = f.read(32768) + f.close() + return e.digest() + +def hash_file(name): + if not os.access(name, os.R_OK): + print 'Invalid file: %s' % (name) + return + size = os.stat(name).st_size + hash = file_hash(name) + print 'Hashed: ed2k://|file|%s|%d|%s|' % (name, size, hash) + return name, size, hash + +def hash_thread(filelist, hashlist): + global num_threads + num_threads += 1 + try: + while filelist: + h = hash_file(filelist.pop(0)) + if h: + hashlist.append(h) + except IndexError: + pass + num_threads -= 1 + +def auth(): + try: + c = ConfigParser.ConfigParser() + c.read(os.path.expanduser('~/.pyanidb.conf')) + username = c.get('auth', 'username') + password = c.get('auth', 'password') + except: + username = raw_input('Username: ') + password = getpass.getpass() + return username, password + +username, password = auth() + +try: + a = anidb.AniDB(username, password) + #t = a.ping() + #if t: + # print 'AniDB is reachable, %.3fs' % (t) + #else: + # print 'AniDB is unreachable.' + # sys.exit(1) + a.auth() + print 'Logged in as user %s.' % (username) + if a.new_version: + print 'New version available.' + + filelist = sys.argv[1:] + hashlist = [] + + thread.start_new_thread(hash_thread, (filelist, hashlist)) + + while hashlist or num_threads or filelist: + if not hashlist: + time.sleep(0.1) + continue + name, size, hash = hashlist.pop(0) + try: + while 1: + try: + a.add_hash(size, hash) + except anidb.AniDBTimeout: + print 'Connection timed out, retrying.' + continue + break + except anidb.AniDBUnknownFile: + print 'Unknown file: %s' % (name) + continue + print 'Added file: %s' % (name) + print 'All operations finished.' +except anidb.AniDBUserError: + print 'Invalid username/password.' + sys.exit(1) +except anidb.AniDBTimeout: + print 'Connection timed out.' + sys.exit(1) +except anidb.AniDBError, e: + print 'Fatal error:', e + sys.exit(1) diff --git a/anidb_add.py b/anidb_add.py deleted file mode 100644 index b84f162..0000000 --- a/anidb_add.py +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/env python - -import ConfigParser, os, sys, thread, time, getpass, anidb, ed2k - -num_threads = 0 - -def hash_file(name): - if not os.access(name, os.R_OK): - print 'Invalid file: %s' % (name) - return - size = os.stat(name).st_size - hash = ed2k.file_hash(name) - print 'Hashed: ed2k://|file|%s|%d|%s|' % (name, size, hash) - return name, size, hash - -def hash_thread(filelist, hashlist): - global num_threads - num_threads += 1 - try: - while filelist: - h = hash_file(filelist.pop(0)) - if h: - hashlist.append(h) - except IndexError: - pass - num_threads -= 1 - -def auth(): - try: - c = ConfigParser.ConfigParser() - c.read(os.path.expanduser('~/.pyanidb.conf')) - username = c.get('auth', 'username') - password = c.get('auth', 'password') - except: - username = raw_input('Username: ') - password = getpass.getpass() - return username, password - -username, password = auth() - -try: - a = anidb.AniDB(username, password) - #t = a.ping() - #if t: - # print 'AniDB is reachable, %.3fs' % (t) - #else: - # print 'AniDB is unreachable.' - # sys.exit(1) - a.auth() - print 'Logged in as user %s.' % (username) - if a.new_version: - print 'New version available.' - - filelist = sys.argv[1:] - hashlist = [] - - thread.start_new_thread(hash_thread, (filelist, hashlist)) - - while hashlist or num_threads or filelist: - if not hashlist: - time.sleep(0.1) - continue - name, size, hash = hashlist.pop(0) - try: - while 1: - try: - a.add_hash(size, hash) - except anidb.AniDBTimeout: - print 'Connection timed out, retrying.' - continue - break - except anidb.AniDBUnknownFile: - print 'Unknown file: %s' % (name) - continue - print 'Added file: %s' % (name) - print 'All operations finished.' -except anidb.AniDBUserError: - print 'Invalid username/password.' - sys.exit(1) -except anidb.AniDBTimeout: - print 'Connection timed out.' - sys.exit(1) -except anidb.AniDBError, e: - print 'Fatal error:', e - sys.exit(1) diff --git a/ed2k/Makefile b/ed2k/Makefile deleted file mode 100644 index 07d23df..0000000 --- a/ed2k/Makefile +++ /dev/null @@ -1,27 +0,0 @@ -CC=gcc -CFLAGS= -CPP=g++ -CPPFLAGS= -LD=gcc -LDFLAGS=-shared - -OBJECTS=ed2k_wrapper.o ed2k.o -TARGET=_ed2k.so -INCLUDE=-I /usr/include/python2.4/ -LIB=-l boost_python -l ssl - -all: $(TARGET) - -clean: - -rm $(TARGET) $(OBJECTS) - -# M�-regel -$(TARGET): $(OBJECTS) Makefile - $(LD) $(LDFLAGS) -o $(TARGET) $(OBJECTS) $(LIB) - -# Pseudoregler -%.o: %.c Makefile - $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE) - -%.o: %.cpp Makefile - $(CPP) $(CPPFLAGS) -o $@ -c $< $(INCLUDE) \ No newline at end of file diff --git a/ed2k/__init__.py b/ed2k/__init__.py deleted file mode 100644 index 754b5c9..0000000 --- a/ed2k/__init__.py +++ /dev/null @@ -1,11 +0,0 @@ -from _ed2k import * - -def file_hash(name): - e = Ed2k() - f = open(name) - data = f.read(32768) - while data: - e.update(data) - data = f.read(32768) - f.close() - return e.digest() diff --git a/ed2k/ed2k.cpp b/ed2k/ed2k.cpp deleted file mode 100644 index 369f57e..0000000 --- a/ed2k/ed2k.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include "ed2k.h" - -namespace Hex { - static char* digits = "0123456789abcdef"; - std::string hex(char* bin, int length) { - std::string s(length * 2, ' '); - for(int i = 0; i < length; i++) { - s[i*2] = digits[(bin[i] >> 4) & 0xf]; - s[i*2+1] = digits[bin[i] & 0xf]; - } - return s; - } - std::string hex(int bin) { - std::string s(sizeof(int) * 2, ' '); - for(int i = 0; i < sizeof(int) * 2; i++) { - s[sizeof(int) * 2 - 1 - i] = digits[bin & 0xf]; - bin = bin >> 4; - } - return s; - } -} - -template -inline T min(T a, T b) { - return (a > b) ? b : a; -} - -Ed2k::Ed2k() { - MD4_Init(&md4_partial); - MD4_Init(&md4_final); - size_total = 0; - digest_str = ""; -} - -void Ed2k::update(std::string data_str) { - unsigned int length = data_str.length(); - const char* data = data_str.c_str(); - while(length) { - if(!(size_total % (9500 * 1024)) && size_total) { - unsigned char digest[16]; - MD4_Final(digest, &md4_partial); - MD4_Update(&md4_final, digest, 16); - MD4_Init(&md4_partial); - } - int size = min(length, (9500 * 1024) - (size_total % (9500 * 1024))); - MD4_Update(&md4_partial, data, size); - length -= size; - data += size; - size_total += size; - }; -} - -std::string Ed2k::digest() { - if(!digest_str.length()) { - char* digest = new char[16]; - if(size_total > (9500 * 1024)) { - unsigned char digest_partial[16]; - MD4_Final(digest_partial, &md4_partial); - MD4_Update(&md4_final, digest_partial, 16); - MD4_Final((unsigned char*)digest, &md4_final); - } else { - MD4_Final((unsigned char*)digest, &md4_partial); - } - digest_str = Hex::hex(digest, 16); - delete digest; - } - return digest_str; -} diff --git a/ed2k/ed2k.h b/ed2k/ed2k.h deleted file mode 100644 index 97feb61..0000000 --- a/ed2k/ed2k.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef _ED2K_H_ -#define _ED2K_H_ - -#include -#include - -class Ed2k { - private: - MD4_CTX md4_partial; - MD4_CTX md4_final; - unsigned int size_total; - std::string digest_str; - public: - Ed2k(); - void update(std::string data); - std::string digest(); -}; - -#endif // _ED2K_H_ diff --git a/ed2k/ed2k_wrapper.cpp b/ed2k/ed2k_wrapper.cpp deleted file mode 100644 index 0a9f8c8..0000000 --- a/ed2k/ed2k_wrapper.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "ed2k.h" - -#include -using namespace boost::python; - -BOOST_PYTHON_MODULE(_ed2k) { - class_("Ed2k") - .def("update", &Ed2k::update) - .def("digest", &Ed2k::digest); -} diff --git a/pyanidb.conf b/pyanidb.conf index 093e7a4..f400006 100644 --- a/pyanidb.conf +++ b/pyanidb.conf @@ -2,4 +2,4 @@ [auth] username = foo -password = foossecretpassword \ No newline at end of file +password = password diff --git a/pyanidb/__init__.py b/pyanidb/__init__.py new file mode 100644 index 0000000..1a36ec3 --- /dev/null +++ b/pyanidb/__init__.py @@ -0,0 +1,95 @@ +import socket, time + +protover = 3 +client = 'pyanidb' +clientver = 2 + +states = { + 'unknown': 0, + 'hdd': 1, + 'cd': 2, + 'deleted': 3, + 'shared': 4, + 'release': 5 +} + +class AniDBError(Exception): + pass + +class AniDBTimeout(AniDBError): + pass + +class AniDBLoginError(AniDBError): + pass + +class AniDBUserError(AniDBLoginError): + pass + +class AniDBReplyError(AniDBError): + pass + +class AniDBUnknownFile(AniDBError): + pass + +class AniDB: + def __init__(self, username, password, localport = 1234, server = ('api.anidb.info', 9000)): + self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.sock.bind(('0.0.0.0', localport)) + self.sock.settimeout(10) + self.username = username.lower() + self.password = password + self.server = server + self.session = '' + self.lasttime = 0 + self.new_version = False + def __del__(self): + self.logout() + self.sock.close() + def execute(self, data): + t = time.time() + if t < self.lasttime + 2: + time.sleep(self.lasttime + 2 - t) + self.lasttime = time.time() + self.sock.sendto(data + '\n', self.server) + try: + data = self.sock.recv(8192).split('\n') + except socket.timeout: + raise AniDBTimeout() + code, text = data[0].split(' ', 1) + data = data[1:-1] + code = int(code) + return code, text, data + def ping(self): + t = time.time() + try: + return self.execute('PING')[0] == 300 and time.time() - t or None + except AniDBTimeout: + return None + def auth(self): + code, text, data = self.execute('AUTH user=%s&pass=%s&protover=%d&client=%s&clientver=%d' % (self.username, self.password, protover, client, clientver)) + if code in [200, 201]: + self.session = text.split(' ', 1)[0] + if code == 201: + self.new_version = True + elif code == 500: + raise AniDBUserError() + else: + raise AniDBReplyError(code, text) + def logout(self): + if self.session: + try: + self.execute('LOGOUT s=%s' % (self.session)) + self.session = '' + except AniDBError: + pass + def add_hash(self, size, ed2k, state = states['hdd'], viewed = False, source = '', storage = '', other = ''): + while 1: + code, text, data = self.execute('MYLISTADD s=%s&size=%d&ed2k=%s&state=%d&viewed=%d&source=%s&storage=%s&other=%s' % (self.session, size, ed2k, state, viewed and 1 or 0, source, storage, other)) + if code in [210, 310]: + return + elif code in [501, 506]: + self.auth() + elif code == 320: + raise AniDBUnknownFile() + else: + raise AniDBReplyError(code, text) diff --git a/setup.py b/setup.py new file mode 100755 index 0000000..ec7e191 --- /dev/null +++ b/setup.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python + +import os +from distutils.core import setup, Extension + +setup( + name='pyanidb', + packages = ['pyanidb'], + scripts = ['anidb_add']) -- cgit v1.2.3