From 9077d5a985e6329d8a5d2505787675c821b74b84 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 19 Aug 2009 14:09:19 +0000 Subject: [PATCH] 2009-08-19 Tatsuhiro Tsujikawa Get comment, comment.utf-8, created by and creation date from .torrent file and print them in -S output. * src/bittorrent_helper.cc * src/bittorrent_helper.h * test/BittorrentHelperTest.cc * test/test.torrent * test/utf8.torrent --- ChangeLog | 10 ++++++++ src/bittorrent_helper.cc | 48 ++++++++++++++++++++++++++++++++++++ src/bittorrent_helper.h | 6 +++++ test/BittorrentHelperTest.cc | 16 ++++++++++++ test/test.torrent | 2 +- test/utf8.torrent | 2 +- 6 files changed, 82 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4c8dbd90..6cbdf213 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2009-08-19 Tatsuhiro Tsujikawa + + Get comment, comment.utf-8, created by and creation date from + .torrent file and print them in -S output. + * src/bittorrent_helper.cc + * src/bittorrent_helper.h + * test/BittorrentHelperTest.cc + * test/test.torrent + * test/utf8.torrent + 2009-08-18 Tatsuhiro Tsujikawa Show exact file size along in -S option output. diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index dda7a75f..9f91c04d 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -83,6 +83,14 @@ static const std::string C_ANNOUNCE_LIST("announce-list"); static const std::string C_NODES("nodes"); +static const std::string C_CREATION_DATE("creation date"); + +static const std::string C_COMMENT("comment"); + +static const std::string C_COMMENT_UTF8("comment.utf-8"); + +static const std::string C_CREATED_BY("created by"); + static const std::string DEFAULT_PEER_ID_PREFIX("-aria2-"); const std::string INFO_HASH("infoHash"); @@ -103,6 +111,12 @@ const std::string NAME("name"); const std::string URL_LIST("urlList"); +const std::string CREATION_DATE("creationDate"); + +const std::string COMMENT("comment"); + +const std::string CREATED_BY("createdBy"); + const std::string BITTORRENT("bittorrent"); const std::string MULTI("multi"); @@ -402,6 +416,24 @@ static void processRootDictionary // retrieve nodes extractNodes(torrent, rootDict[C_NODES]); + const BDE& creationDate = rootDict[C_CREATION_DATE]; + if(creationDate.isInteger()) { + torrent[CREATION_DATE] = creationDate; + } + const BDE& commentUtf8 = rootDict[C_COMMENT_UTF8]; + if(commentUtf8.isString()) { + torrent[COMMENT] = commentUtf8; + } else { + const BDE& comment = rootDict[C_COMMENT]; + if(comment.isString()) { + torrent[COMMENT] = comment; + } + } + const BDE& createdBy = rootDict[C_CREATED_BY]; + if(createdBy.isString()) { + torrent[CREATED_BY] = createdBy; + } + ctx->setAttribute(BITTORRENT, torrent); } @@ -498,6 +530,22 @@ void print(std::ostream& o, const SharedHandle& dctx) { const BDE& torrentAttrs = dctx->getAttribute(BITTORRENT); o << "*** BitTorrent File Information ***" << "\n"; + if(torrentAttrs.containsKey(COMMENT)) { + o << "Comment: " << torrentAttrs[COMMENT].s() << "\n"; + } + if(torrentAttrs.containsKey(CREATION_DATE)) { + struct tm* staticNowtmPtr; + char buf[26]; + time_t t = torrentAttrs[CREATION_DATE].i(); + if((staticNowtmPtr = localtime(&t)) != 0 && + asctime_r(staticNowtmPtr, buf) != 0) { + // buf includes "\n" + o << "Creation Date: " << buf; + } + } + if(torrentAttrs.containsKey(CREATED_BY)) { + o << "Created By: " << torrentAttrs[CREATED_BY].s() << "\n"; + } o << "Mode: " << torrentAttrs[MODE].s() << "\n"; o << "Announce:" << "\n"; const BDE& announceList = torrentAttrs[ANNOUNCE_LIST]; diff --git a/src/bittorrent_helper.h b/src/bittorrent_helper.h index 17939c59..43160c04 100644 --- a/src/bittorrent_helper.h +++ b/src/bittorrent_helper.h @@ -71,6 +71,12 @@ extern const std::string NAME; extern const std::string URL_LIST; +extern const std::string CREATION_DATE; + +extern const std::string COMMENT; + +extern const std::string CREATED_BY; + extern const std::string SINGLE; extern const std::string MULTI; diff --git a/test/BittorrentHelperTest.cc b/test/BittorrentHelperTest.cc index 321e371e..0a156967 100644 --- a/test/BittorrentHelperTest.cc +++ b/test/BittorrentHelperTest.cc @@ -51,6 +51,7 @@ class BittorrentHelperTest:public CppUnit::TestFixture { CPPUNIT_TEST(testSetFileFilter_single); CPPUNIT_TEST(testSetFileFilter_multi); CPPUNIT_TEST(testUTF8Torrent); + CPPUNIT_TEST(testMetaData); CPPUNIT_TEST_SUITE_END(); public: void setUp() { @@ -86,6 +87,7 @@ public: void testSetFileFilter_single(); void testSetFileFilter_multi(); void testUTF8Torrent(); + void testMetaData(); }; @@ -625,6 +627,20 @@ void BittorrentHelperTest::testUTF8Torrent() CPPUNIT_ASSERT_EQUAL(std::string("name in utf-8"), getName(dctx)); CPPUNIT_ASSERT_EQUAL(std::string("./name in utf-8/path in utf-8"), dctx->getFirstFileEntry()->getPath()); + CPPUNIT_ASSERT_EQUAL(std::string("This is utf8 comment."), + dctx->getAttribute(BITTORRENT)[COMMENT].s()); +} + +void BittorrentHelperTest::testMetaData() +{ + SharedHandle dctx(new DownloadContext()); + load("test.torrent", dctx); + CPPUNIT_ASSERT_EQUAL(std::string("REDNOAH.COM RULES"), + dctx->getAttribute(BITTORRENT)[COMMENT].s()); + CPPUNIT_ASSERT_EQUAL(std::string("aria2"), + dctx->getAttribute(BITTORRENT)[CREATED_BY].s()); + CPPUNIT_ASSERT_EQUAL((int64_t)1123456789, + dctx->getAttribute(BITTORRENT)[CREATION_DATE].i()); } } // namespace bittorrent diff --git a/test/test.torrent b/test/test.torrent index 09e622fa..43aeb768 100644 --- a/test/test.torrent +++ b/test/test.torrent @@ -1 +1 @@ -d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:privatei1e7:comment17:REDNOAH.COM RULES13:creation datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ceed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test12:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee \ No newline at end of file +d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:privatei1e7:comment17:REDNOAH.COM RULES13:creation datei1123456789e10:created by5:aria24:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ceed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test12:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee \ No newline at end of file diff --git a/test/utf8.torrent b/test/utf8.torrent index 6f26f52f..24b07d48 100644 --- a/test/utf8.torrent +++ b/test/utf8.torrent @@ -1 +1 @@ -d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:privatei1e7:comment17:REDNOAH.COM RULES13:creation datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ce10:path.utf-8l13:path in utf-8eed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test10:name.utf-813:name in utf-812:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee \ No newline at end of file +d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:privatei1e7:comment17:REDNOAH.COM RULES13:comment.utf-821:This is utf8 comment.13:creation datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ce10:path.utf-8l13:path in utf-8eed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test10:name.utf-813:name in utf-812:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee \ No newline at end of file