Skip to content

Commit

Permalink
net-p2p/py-ed2k-tools: new port had been added (+)
Browse files Browse the repository at this point in the history
This is a Python framework for easy manipulation of .part.met files
(Overnet/eDonkey2000 download meta-information).  This set of classes
allows for rapid development of ed2k tools, using a very simple API.
  • Loading branch information
Alexey Dokuchaev authored and Alexey Dokuchaev committed May 18, 2023
1 parent 3f97a92 commit 85e836a
Show file tree
Hide file tree
Showing 10 changed files with 406 additions and 0 deletions.
1 change: 1 addition & 0 deletions net-p2p/Makefile
Expand Up @@ -78,6 +78,7 @@
SUBDIR += p5-WWW-BitTorrent
SUBDIR += phex
SUBDIR += prowlarr
SUBDIR += py-ed2k-tools
SUBDIR += py-libtorrent-rasterbar
SUBDIR += py-nicotine-plus
SUBDIR += py-transmission-rpc
Expand Down
39 changes: 39 additions & 0 deletions net-p2p/py-ed2k-tools/Makefile
@@ -0,0 +1,39 @@
PORTNAME= ed2k-tools
PORTVERSION= 0.1
CATEGORIES= net-p2p python
MASTER_SITES= SF/${PORTNAME}/ed2k-python/${PORTVERSION}
PKGNAMEPREFIX= ${PYTHON_PKGNAMEPREFIX}
DISTNAME= ed2k_python-${PORTVERSION}

MAINTAINER= danfe@FreeBSD.org
COMMENT= Python framework for manipulating eD2K metafiles
WWW= https://ed2k-tools.sourceforge.net/python.shtml

LICENSE= MIT

USES= python:run shebangfix
SHEBANG_FILES= fix_sofar.py make_met.py non-met/shutdown_core.py \
retrieve_link.py temp_summary.py
WRKSRC= ${WRKDIR}/ed2k_python

NO_ARCH= yes
NO_BUILD= yes

PLIST_FILES= ${PYTHON_SITELIBDIR}/ed2k_metutils.py \
${SHEBANG_FILES:T:S,^,bin/,}
PORTDOCS= README

OPTIONS_DEFINE= DOCS

do-install:
@${MKDIR} ${STAGEDIR}${PYTHON_SITELIBDIR}
${INSTALL_DATA} ${WRKSRC}/ed2k_metutils.py \
${STAGEDIR}${PYTHON_SITELIBDIR}
${INSTALL_SCRIPT} ${SHEBANG_FILES:S,^,${WRKSRC}/,} \
${STAGEDIR}${PREFIX}/bin

do-install-DOCS-on:
@${MKDIR} ${STAGEDIR}${DOCSDIR}
${INSTALL_DATA} ${PORTDOCS:S,^,${WRKSRC}/,} ${STAGEDIR}${DOCSDIR}

.include <bsd.port.mk>
3 changes: 3 additions & 0 deletions net-p2p/py-ed2k-tools/distinfo
@@ -0,0 +1,3 @@
TIMESTAMP = 1052224864
SHA256 (ed2k_python-0.1.tar.gz) = aa11b85735473319e8d0662fef60af4b777c1759456beb4aaf8545911bff761a
SIZE (ed2k_python-0.1.tar.gz) = 10290
97 changes: 97 additions & 0 deletions net-p2p/py-ed2k-tools/files/patch-ed2k__metutils.py
@@ -0,0 +1,97 @@
--- ed2k_metutils.py.orig 2003-05-06 11:53:14 UTC
+++ ed2k_metutils.py
@@ -6,7 +6,6 @@
# tested on macosx 10.2.4, python 2.2

import struct;
-import types;
import sys;

# Some defines.
@@ -14,16 +13,16 @@ import sys;
TAG_TYPE_STRING = 2;
TAG_TYPE_INTEGER = 3;
-TAG_HANDLE_FILENAME = chr( 1 );
-TAG_HANDLE_FILESIZE = chr( 2 );
-TAG_HANDLE_FILETYPE = chr( 3 );
-TAG_HANDLE_FILEFORMAT = chr( 4 );
-TAG_HANDLE_SOFAR = chr( 8 );
-TAG_HANDLE_GAP_START = chr( 9 );
-TAG_HANDLE_GAP_END = chr( 10 );
-TAG_HANDLE_TEMP_NAME = chr( 18 );
-TAG_HANDLE_PAUSED = chr( 20 );
-TAG_HANDLE_PRIORITY = chr( 24 );
+TAG_HANDLE_FILENAME = b'\x01'
+TAG_HANDLE_FILESIZE = b'\x02'
+TAG_HANDLE_FILETYPE = b'\x03'
+TAG_HANDLE_FILEFORMAT = b'\x04'
+TAG_HANDLE_SOFAR = b'\x08'
+TAG_HANDLE_GAP_START = b'\x09'
+TAG_HANDLE_GAP_END = b'\x0a'
+TAG_HANDLE_TEMP_NAME = b'\x12'
+TAG_HANDLE_PAUSED = b'\x14'
+TAG_HANDLE_PRIORITY = b'\x18'

class MetFile:
"""Class designed to hold the data of a .part.met file."""
@@ -39,7 +38,7 @@ class MetFile:
# a .part file must exist, even if it's empty. The same doesn't apply for new overnet.
self.version = 225;
self.modDate = 0;
- self.fileID = '\0' * 16;
+ self.fileID = b'\0' * 16
return;

header_struct = "<BI16sH";
@@ -58,7 +57,7 @@ class MetFile:
dstore = dstore[ 4 : ];
for meta in range( n_meta ):
- t_type, = struct.unpack( "<B", dstore[ 0 ] );
+ t_type, = struct.unpack("<B", dstore[0:1])
dstore = dstore[ 1 : ];

name_len, = struct.unpack( "<H", dstore[ : 2 ] );
@@ -81,14 +80,14 @@ class MetFile:
"""Return a string representation of the file MD4."""
data = "";
for i in range( len( self.fileID ) ):
- data += "%02x" % ord( self.fileID[ i ] );
+ data += "%02x" % self.fileID[i]
return data.upper();
def getEd2K( self ):
"""Return the ed2k:// link associated with this met file."""
size = self.FindTags( TAG_HANDLE_FILESIZE )[ 0 ].value;
name = self.FindTags( TAG_HANDLE_FILENAME )[ 0 ].value;
- return "ed2k://|file|%s|%s|%s|" % ( name, size, self.getMD4() );
+ return "ed2k://|file|%s|%s|%s|" % (name.decode(), size, self.getMD4())
def ReduceToData( self ):
"""Reduce a class instance back into a stream suitable for writing to disk."""
@@ -109,13 +108,13 @@ class MetFile:
"""Return an array of tags matching the supplied handle.
Tags relating to gaps do no obey the usual 'special tag'
semantics, so set the flag to 1 if you are dealing with them."""
- if gaptags: return [ x for x in self.m_tags if x.name[ 0 ] == tagHandle ];
+ if gaptags: return [ x for x in self.m_tags if x.name[0:1] == tagHandle ]
else: return [ x for x in self.m_tags if x.name == tagHandle ];
def PurgeTags( self, tagHandle, gaptags = 0 ):
"""This is the same as FindTags, except it removes the
matching tags from the meta-tag store."""
- if gaptags: self.m_tags = [ x for x in self.m_tags if x.name[ 0 ] != tagHandle ];
+ if gaptags: self.m_tags = [ x for x in self.m_tags if x.name[0:1] != tagHandle ]
else: self.m_tags = [ x for x in self.m_tags if x.name != tagHandle ];

class MetaTag:
@@ -127,7 +126,7 @@ class MetaTag:
self.value = value;
if t_type == None:
# Rudiments of Autodetection...
- if type( value ) == types.IntType:
+ if isinstance(value, int):
self.tag_type = TAG_TYPE_INTEGER;
else:
self.tag_type = TAG_TYPE_STRING;
63 changes: 63 additions & 0 deletions net-p2p/py-ed2k-tools/files/patch-fix__sofar.py
@@ -0,0 +1,63 @@
--- fix_sofar.py.orig 2003-05-06 11:53:14 UTC
+++ fix_sofar.py
@@ -11,26 +11,25 @@ if __name__ == "__main__":
# This will undo the damage.
if len( sys.argv ) < 2:
- print "invocation: %s <x.part.met> [x.part.met ...]" % sys.argv[ 0 ];
- print
- print "Some versions of Overnet on MacOSX seem not to write the 0x08 'sofar' tag"
- print "on exiting, this gives the appearance that the next time you boot overnet,"
- print "nothing has been downloaded. It's only cosmetic, however."
- print
- print "If you want to create new .met files with this 'bug' corrected, run this"
- print "program with the affected .met files as the command line arguments. You"
- print "will get new .met files titled X.new, where X was the original .part.met"
- print "file. Copy these over the top of your originals if you're sure thats what"
- print "you want to do."
- print
- print "Of course, Overnet will re-break these files on its next exit. You'll"
- print "need to run this program a lot to keep everything setup."
- print
+ print("invocation: %s <x.part.met> [x.part.met ...]" % sys.argv[0])
+ print()
+ print("Some versions of Overnet on MacOSX seem not to write the 0x08 'sofar' tag")
+ print("on exiting, this gives the appearance that the next time you boot overnet,")
+ print("nothing has been downloaded. It's only cosmetic, however.")
+ print()
+ print("If you want to create new .met files with this 'bug' corrected, run this")
+ print("program with the affected .met files as the command line arguments. You")
+ print("will get new .met files titled X.new, where X was the original .part.met")
+ print("file. Copy these over the top of your originals if you're sure thats what")
+ print("you want to do.")
+ print()
+ print("Of course, Overnet will re-break these files on its next exit. You'll")
+ print("need to run this program a lot to keep everything setup.")
sys.exit( -1 );

for met_file in sys.argv[ 1 : ]:

- fh = open( met_file, "r" );
+ fh = open(met_file, "rb")
data = fh.read();
fh.close();

@@ -55,14 +54,14 @@ if __name__ == "__main__":
for gap in gaps.keys():
so_far -= gaps[ gap ];

- print "%s: %s" % ( met_file, met_data.FindTags( TAG_HANDLE_FILENAME )[ 0 ].value );
- print "MD4: %s" % ( met_data.getMD4() );
- print "Obtained size / total: %i / %i" % ( so_far, length );
+ print("%s: %s" % (met_file, met_data.FindTags(TAG_HANDLE_FILENAME)[0].value.decode()))
+ print("MD4: %s" % met_data.getMD4())
+ print("Obtained size / total: %i / %i" % (so_far, length))

met_data.PurgeTags( TAG_HANDLE_SOFAR );
met_data.AddTag( MetaTag( TAG_HANDLE_SOFAR, so_far, TAG_TYPE_INTEGER ) );

- fh = open( "%s.new" % met_file, "w" );
+ fh = open("%s.new" % met_file, "wb")
fh.write( met_data.ReduceToData() );
fh.close();
del( met_data );
79 changes: 79 additions & 0 deletions net-p2p/py-ed2k-tools/files/patch-make__met.py
@@ -0,0 +1,79 @@
--- make_met.py.orig 2003-05-06 11:53:14 UTC
+++ make_met.py
@@ -7,14 +7,13 @@ import re
if __name__ == "__main__":
if len( sys.argv ) < 3:
- print "invocation: %s <temp-directory> <ed2k://...>" % sys.argv[ 0 ];
- print ;
- print "This script creates a new .part.met file in the directory of the first";
- print "argument, which represents the ed2k:// link provided as the second arg.";
- print ;
- print "Useful for adding things to your download list without actually opening ";
- print "Overnet / Donkey.";
- print ;
+ print("invocation: %s <temp-directory> <ed2k://...>" % sys.argv[0])
+ print()
+ print("This script creates a new .part.met file in the directory of the first")
+ print("argument, which represents the ed2k:// link provided as the second arg.")
+ print()
+ print("Useful for adding things to your download list without actually opening")
+ print("Overnet / Donkey.")
sys.exit( -1 );

temp_dir = sys.argv[ 1 ];
@@ -25,9 +24,9 @@ if __name__ == "__main__":
matches = ed2k_reg.findall( ed2k_link );

if not matches:
- print "Oh no! This ( %s ) doesn't feel like an ed2k link!" % ( ed2k_link );
- print "ed2k file links have the form:";
- print " ed2k://|file|<file name>|<file size>|<md4 hash>|";
+ print("Oh no! This (%s) doesn't look like an ed2k link!" % (ed2k_link))
+ print("ed2k file links have the form:")
+ print(" ed2k://|file|<file name>|<file size>|<md4 hash>|")
sys.exit( -1 );

name, size, hash = ed2k_reg.findall( ed2k_link )[ 0 ];
@@ -35,11 +34,11 @@ if __name__ == "__main__":
# Convert the printed hash into a byte representation.
# Surely there's an easier way to do this.
- new_hash = "";
+ new_hash = b''
while hash:
part = hash[ 0 : 2 ];
hash = hash[ 2 : ];
- new_hash += chr( eval( "0x" + part ) );
+ new_hash += bytes([eval("0x" + part)])

# Find the first unused download identifier.
metfiles = [ int( x.split( "." )[ 0 ] ) for x in os.listdir( temp_dir ) if x.endswith( ".part.met" ) ];
@@ -53,22 +52,22 @@ if __name__ == "__main__":
# Build the structure.
new = MetFile();
new.fileID = new_hash;
- new.AddTag( MetaTag( TAG_HANDLE_FILENAME, name ) );
+ new.AddTag(MetaTag(TAG_HANDLE_FILENAME, name.encode()))
new.AddTag( MetaTag( TAG_HANDLE_FILESIZE, size ) );
new.AddTag( MetaTag( TAG_HANDLE_SOFAR, 0 ) );
# Now, I thought this implied an off-by-one error ( if a file is
# five bytes long, the gap should go from byte 0 to byte 4... ),
# but apparently that's not how we do it here.
- new.AddTag( MetaTag( "%s0" % TAG_HANDLE_GAP_START, 0 ) );
- new.AddTag( MetaTag( "%s0" % TAG_HANDLE_GAP_END, size ) );
+ new.AddTag(MetaTag(b"%s0" % TAG_HANDLE_GAP_START, 0))
+ new.AddTag(MetaTag(b"%s0" % TAG_HANDLE_GAP_END, size))
new.AddTag( MetaTag( TAG_HANDLE_PAUSED, 0 ) );
new.AddTag( MetaTag( TAG_HANDLE_PRIORITY, 1 ) );

# Write it out.
- fh = open( filename, "w" );
+ fh = open(filename, "wb")
fh.write( new.ReduceToData() );
fh.close();

del( new );

- print "Wrote met for %s to %s." % ( ed2k_link, filename );
+ print("Wrote met for %s to %s." % (ed2k_link, filename))
29 changes: 29 additions & 0 deletions net-p2p/py-ed2k-tools/files/patch-non-met_shutdown__core.py
@@ -0,0 +1,29 @@
--- non-met/shutdown_core.py.orig 2003-05-06 11:53:14 UTC
+++ non-met/shutdown_core.py
@@ -13,7 +13,7 @@ def make_login_packet( username, password ):
"""Create a authentication packet."""
# This looks like so:
# <length of username: 16bit><username><length of password: 16 bit><password>
- packet = struct.pack( "<BH%isH%is" % ( len( username ), len( password ) ), OP_LOGIN, len( username ), username, len( password ), password );
+ packet = struct.pack("<BH%isH%is" % (len(username), len(password)), OP_LOGIN, len(username), username.encode(), len(password), password.encode())
# Hss hss.
return packet;
@@ -25,7 +25,7 @@ def make_shutdown_packet( ):
def make_connection( hostname, port = 4663 ):
"""Make a socket to the core."""
connection = socket.socket();
- connection.connect( ( hostname, port ) );
+ connection.connect((hostname, int(port)))
return connection;
def send_packet( connection, packet ):
@@ -35,7 +35,7 @@ def send_packet( connection, packet ):
if __name__ == '__main__':
if len( sys.argv ) < 4:
- print "usage: %s <host[:port]> <uname> <pass>" % sys.argv[ 0 ];
+ print("usage: %s <host[:port]> <uname> <pass>" % sys.argv[0])
sys.exit( -1 );

# Split out the hostname if necessary.
32 changes: 32 additions & 0 deletions net-p2p/py-ed2k-tools/files/patch-retrieve__link.py
@@ -0,0 +1,32 @@
--- retrieve_link.py.orig 2003-05-06 11:53:14 UTC
+++ retrieve_link.py
@@ -8,22 +8,21 @@ if __name__ == "__main__":
# you break one so badly all you want back is the ed2k:// hash.
if len( sys.argv ) < 2:
- print "invocation: %s <x.part.met> [x.part.met ...]" % sys.argv[ 0 ];
- print
- print "This program will print out the ed2k:// link responsible for"
- print "the formation of a given .part.met file or files, given on "
- print "the command line."
- print
+ print("invocation: %s <x.part.met> [x.part.met ...]" % sys.argv[0])
+ print()
+ print("This program will print out the ed2k:// link responsible for")
+ print("the formation of a given .part.met file or files, given on")
+ print("the command line.")
sys.exit( -1 );

for met_file in sys.argv[ 1 : ]:

- fh = open( met_file, "r" );
+ fh = open(met_file, "rb")
data = fh.read();
fh.close();

met_data = MetFile( data );
del( data );

- print "%s: %s" % ( met_file, met_data.getEd2K() );
+ print("%s: %s" % (met_file, met_data.getEd2K()))
del( met_data );

0 comments on commit 85e836a

Please sign in to comment.