From 6b7c44eeb3af0dad7717c0cb4618f2cd1746597b Mon Sep 17 00:00:00 2001 From: raspbeguy Date: Tue, 24 May 2016 00:46:11 +0200 Subject: [PATCH 1/4] Creation of torrent extention --- feedgen/ext/torrent.py | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 feedgen/ext/torrent.py diff --git a/feedgen/ext/torrent.py b/feedgen/ext/torrent.py new file mode 100644 index 0000000..eab61fa --- /dev/null +++ b/feedgen/ext/torrent.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +''' + feedgen.ext.podcast + ~~~~~~~~~~~~~~~~~~~ + + Extends the FeedGenerator to produce torrent feeds. + + :copyright: 2016, Raspbeguy + + :license: FreeBSD and LGPL, see license.* for more details. +''' + +from lxml import etree +from feedgen.ext.base import BaseExtension,BaseEntryExtension + +TORRENT_NS = 'http://xmlns.ezrss.it/0.1/dtd/' + +class TorrentExtension(BaseExtension): + '''FeedGenerator extension for torrent feeds. + ''' + + + def extend_ns(self): + return {'torrent' : TORRENT_NS} + + +class TorrentEntryExtension(BaseEntryExtension): + '''FeedEntry extention for torrent feeds + ''' + + + def __init__(self): + self.__torrent_enclosure = None + self.__torrent_media = None + self.__torrent_guid = None + + + def extend_rss(self, entry): + '''Add additional fields to an RSS item. + + :param feed: The RSS item XML element to use. + ''' + if self.__torrent_enclosure: + enclosure = etree.SubElement(entry, '{%s}enclosure' % TORRENT_NS) From 2b597a6dee10192faa6faabce4b7b14590b8da6c Mon Sep 17 00:00:00 2001 From: raspbeguy Date: Tue, 24 May 2016 15:48:18 +0200 Subject: [PATCH 2/4] finished torrent ext --- feedgen/ext/torrent.py | 89 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 6 deletions(-) diff --git a/feedgen/ext/torrent.py b/feedgen/ext/torrent.py index eab61fa..8d8d9ca 100644 --- a/feedgen/ext/torrent.py +++ b/feedgen/ext/torrent.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- ''' - feedgen.ext.podcast + feedgen.ext.torrent ~~~~~~~~~~~~~~~~~~~ Extends the FeedGenerator to produce torrent feeds. @@ -30,9 +30,11 @@ class TorrentEntryExtension(BaseEntryExtension): def __init__(self): - self.__torrent_enclosure = None - self.__torrent_media = None - self.__torrent_guid = None + self.__torrent_filename = None + self.__torrent_url = None + self.__torrent_hash = None + self.__torrent_length = None + self.__torrent_contentlength = None def extend_rss(self, entry): @@ -40,5 +42,80 @@ class TorrentEntryExtension(BaseEntryExtension): :param feed: The RSS item XML element to use. ''' - if self.__torrent_enclosure: - enclosure = etree.SubElement(entry, '{%s}enclosure' % TORRENT_NS) + enclosure = etree.SubElement(entry, '{%s}enclosure' % TORRENT_NS) + guid = etree.SubElement(entry, '{%s}guid' % TORRENT_NS) + torrent = etree.SubElement(entry, '{%s]torrent' % TORRENT_NS) + + enclosure.attrib['type'] = 'application/x-bittorrent' + + if self.__torrent_url: + enclosure.attrib['url'] = self.__torrent_url + guid.text = self.__torrent_url + + if self.__torrent_filename: + torrent_filename = etree.SubElement(torrent, '{%s}filename' % TORRENT_NS) + torrent_filename.text = self.__torrent_filename + + if self.__torrent_length: + enclosure.attrib['length'] = self.__torrent_length + + if self.__torrent_contentlength: + torrent_length = etree.SubElement(torrent, '{%s}contentlength' % TORRENT_NS) + torrent_length.text = self.__torrent_contentlength + + if self.__torrent_hash: + torrent_hash = etree.SubElement(torrent, '{%s}infohash' % TORRENT_NS) + torrent_hash.text = self.__torrent_hash + torrent.magnet = etree.SubElement(torrent, '{%s}magneturi' % TORRENT_NS) + torrent_magnet.text = 'magnet:?xt=urn:btih:' + self.__torrent_hash + + + def torrent_filename(self, torrent_filename=None): + '''Get or set the name of the torrent file. + + :param torrent_filename: The name of the torrent file. + :returns: The name of the torrent file. + ''' + if not torrent_filename is None: + self.__torrent_filename = torrent_filename + return self.__torrent_filename + + def torrent_url (self, torrent_url=None): + '''Get or set the URL of the torrent. + + :param torrent_url: The torrent URL. + :returns: The torrent URL. + ''' + if not torrent_url is None: + self.__torrent_url = torrent_url + return self.__torrent_url + + def torrent_hash (self, torrent_hash=None): + '''Get or set the hash of the target file. + + :param torrent_url: The target file hash. + :returns: The target hash file. + ''' + if not torrent_hash is None: + self.__torrent_hash = torrent_hash + return self.__torrent_hash + + def torrent_length (self, torrent_length=None): + '''Get or set the size of the torrent file. + + :param torrent_length: The torrent size. + :returns: The torrent size. + ''' + if not torrent_length is None: + self.__torrent_length = torrent_length + return self.__torrent_length + + def torrent_contentlength (self, torrent_contentlength=None): + '''Get or set the size of the target file. + + :param torrent_contentlength: The target file size. + :returns: The target file size. + ''' + if not torrent_contentlength is None: + self.__torrent_contentlength = torrent_contentlength + return self.__torrent_contentlength From 60765741504c2373524d95fa186c3cf7b91fbe80 Mon Sep 17 00:00:00 2001 From: raspbeguy Date: Tue, 24 May 2016 17:45:38 +0200 Subject: [PATCH 3/4] fixed torrent ext, but still not doing what we want --- .gitignore | 2 ++ feedgen/ext/torrent.py | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index b6f6775..02bef52 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ feedgen/tests/tmp_Rssfeed.xml tmp_Atomfeed.xml tmp_Rssfeed.xml + +build/** diff --git a/feedgen/ext/torrent.py b/feedgen/ext/torrent.py index 8d8d9ca..09e54cb 100644 --- a/feedgen/ext/torrent.py +++ b/feedgen/ext/torrent.py @@ -44,7 +44,7 @@ class TorrentEntryExtension(BaseEntryExtension): ''' enclosure = etree.SubElement(entry, '{%s}enclosure' % TORRENT_NS) guid = etree.SubElement(entry, '{%s}guid' % TORRENT_NS) - torrent = etree.SubElement(entry, '{%s]torrent' % TORRENT_NS) + torrent = etree.SubElement(entry, '{%s}torrent' % TORRENT_NS) enclosure.attrib['type'] = 'application/x-bittorrent' @@ -66,7 +66,7 @@ class TorrentEntryExtension(BaseEntryExtension): if self.__torrent_hash: torrent_hash = etree.SubElement(torrent, '{%s}infohash' % TORRENT_NS) torrent_hash.text = self.__torrent_hash - torrent.magnet = etree.SubElement(torrent, '{%s}magneturi' % TORRENT_NS) + torrent_magnet = etree.SubElement(torrent, '{%s}magneturi' % TORRENT_NS) torrent_magnet.text = 'magnet:?xt=urn:btih:' + self.__torrent_hash From 94eabab02d3c030e9885e3a9a22da8e1a8a5e516 Mon Sep 17 00:00:00 2001 From: raspbeguy Date: Tue, 24 May 2016 23:56:32 +0200 Subject: [PATCH 4/4] Fixed torrent, added test and doc --- doc/api.rst | 1 + doc/ext/api.ext.torrent.rst | 7 ++ feedgen/__main__.py | 15 ++++- feedgen/ext/torrent.py | 124 +++++++++++++++++++----------------- setup.py | 4 +- 5 files changed, 89 insertions(+), 62 deletions(-) create mode 100644 doc/ext/api.ext.torrent.rst diff --git a/doc/api.rst b/doc/api.rst index 6aca66e..9991a6d 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -17,3 +17,4 @@ Contents: ext/api.ext.dc ext/api.ext.podcast ext/api.ext.podcast_entry + ext/api.ext.torrent diff --git a/doc/ext/api.ext.torrent.rst b/doc/ext/api.ext.torrent.rst new file mode 100644 index 0000000..cf2ee8b --- /dev/null +++ b/doc/ext/api.ext.torrent.rst @@ -0,0 +1,7 @@ +.. raw:: html + +
Contents
+
+ +.. automodule:: feedgen.ext.torrent + :members: diff --git a/feedgen/__main__.py b/feedgen/__main__.py index 4463ea6..dc527ac 100644 --- a/feedgen/__main__.py +++ b/feedgen/__main__.py @@ -26,8 +26,9 @@ if __name__ == '__main__': if len(sys.argv) != 2 or not ( sys.argv[1].endswith('rss') \ or sys.argv[1].endswith('atom') \ + or sys.argv[1].endswith('torrent') \ or sys.argv[1].endswith('podcast') ): - print_enc ('Usage: %s ( .atom | atom | .rss | rss | podcast )' % \ + print_enc ('Usage: %s ( .atom | atom | .rss | rss | podcast | torrent )' % \ 'python -m feedgen') print_enc ('') print_enc (' atom -- Generate ATOM test output and print it to stdout.') @@ -39,6 +40,7 @@ if __name__ == '__main__': print_enc (' dc.rss -- Generate DC extension test output (rss format) and print it to stdout.') print_enc (' syndication.atom -- Generate DC extension test output (atom format) and print it to stdout.') print_enc (' syndication.rss -- Generate DC extension test output (rss format) and print it to stdout.') + print_enc (' torrent -- Generate Torrent test output and print it to stdout.') print_enc ('') exit() @@ -90,6 +92,17 @@ if __name__ == '__main__': 'Verba tu fingas et ea dicas, quae non sentias?') fe.podcast.itunes_author('Lars Kiesow') print_enc (fg.rss_str(pretty=True)) + + elif arg == 'torrent': + fg.load_extension('torrent') + fe.link( href='http://somewhere.behind.the.sea/torrent/debian-8.4.0-i386-netint.iso.torrent', rel='alternate', type='application/x-bittorrent, length=1000' ) + fe.torrent.filename('debian-8.4.0-i386-netint.iso.torrent') + fe.torrent.infohash('7661229811ef32014879ceedcdf4a48f256c88ba') + fe.torrent.contentlength('331350016') + fe.torrent.seeds('789') + fe.torrent.peers('456') + fe.torrent.verified('123') + print_enc (fg.rss_str(pretty=True)) elif arg.startswith('dc.'): fg.load_extension('dc') diff --git a/feedgen/ext/torrent.py b/feedgen/ext/torrent.py index 09e54cb..b7f5f49 100644 --- a/feedgen/ext/torrent.py +++ b/feedgen/ext/torrent.py @@ -18,8 +18,6 @@ TORRENT_NS = 'http://xmlns.ezrss.it/0.1/dtd/' class TorrentExtension(BaseExtension): '''FeedGenerator extension for torrent feeds. ''' - - def extend_ns(self): return {'torrent' : TORRENT_NS} @@ -27,50 +25,48 @@ class TorrentExtension(BaseExtension): class TorrentEntryExtension(BaseEntryExtension): '''FeedEntry extention for torrent feeds ''' - - def __init__(self): - self.__torrent_filename = None - self.__torrent_url = None - self.__torrent_hash = None - self.__torrent_length = None - self.__torrent_contentlength = None - + self.__torrent_filename = None + self.__torrent_infohash = None + self.__torrent_contentlength = None + self.__torrent_seeds = None + self.__torrent_peers = None + self.__torrent=verified = None + def extend_rss(self, entry): '''Add additional fields to an RSS item. :param feed: The RSS item XML element to use. ''' - enclosure = etree.SubElement(entry, '{%s}enclosure' % TORRENT_NS) - guid = etree.SubElement(entry, '{%s}guid' % TORRENT_NS) - torrent = etree.SubElement(entry, '{%s}torrent' % TORRENT_NS) - - enclosure.attrib['type'] = 'application/x-bittorrent' - - if self.__torrent_url: - enclosure.attrib['url'] = self.__torrent_url - guid.text = self.__torrent_url - if self.__torrent_filename: - torrent_filename = etree.SubElement(torrent, '{%s}filename' % TORRENT_NS) - torrent_filename.text = self.__torrent_filename - - if self.__torrent_length: - enclosure.attrib['length'] = self.__torrent_length + filename = etree.SubElement(entry, '{%s}filename' % TORRENT_NS) + filename.text = self.__torrent_filename if self.__torrent_contentlength: - torrent_length = etree.SubElement(torrent, '{%s}contentlength' % TORRENT_NS) - torrent_length.text = self.__torrent_contentlength + contentlength = etree.SubElement(entry, '{%s}contentlength' % TORRENT_NS) + contentlength.text = self.__torrent_contentlength - if self.__torrent_hash: - torrent_hash = etree.SubElement(torrent, '{%s}infohash' % TORRENT_NS) - torrent_hash.text = self.__torrent_hash - torrent_magnet = etree.SubElement(torrent, '{%s}magneturi' % TORRENT_NS) - torrent_magnet.text = 'magnet:?xt=urn:btih:' + self.__torrent_hash + if self.__torrent_infohash: + infohash = etree.SubElement(entry, '{%s}infohash' % TORRENT_NS) + infohash.text = self.__torrent_infohash + magnet = etree.SubElement(entry, '{%s}magneturi' % TORRENT_NS) + magnet.text = 'magnet:?xt=urn:btih:' + self.__torrent_infohash + + if self.__torrent_seeds: + seeds = etree.SubElement(entry, '{%s}seed' % TORRENT_NS) + seeds.text = self.__torrent_seeds + + if self.__torrent_peers: + peers = etree.SubElement(entry, '{%s}peers' % TORRENT_NS) + peers.text = self.__torrent_peers + + if self.__torrent_seeds: + verified = etree.SubElement(entry, '{%s}verified' % TORRENT_NS) + verified.text = self.__torrent_verified - def torrent_filename(self, torrent_filename=None): + def filename(self, torrent_filename=None): '''Get or set the name of the torrent file. :param torrent_filename: The name of the torrent file. @@ -80,37 +76,17 @@ class TorrentEntryExtension(BaseEntryExtension): self.__torrent_filename = torrent_filename return self.__torrent_filename - def torrent_url (self, torrent_url=None): - '''Get or set the URL of the torrent. - - :param torrent_url: The torrent URL. - :returns: The torrent URL. - ''' - if not torrent_url is None: - self.__torrent_url = torrent_url - return self.__torrent_url - - def torrent_hash (self, torrent_hash=None): + def infohash (self, torrent_infohash=None): '''Get or set the hash of the target file. - :param torrent_url: The target file hash. + :param torrent_infohash: The target file hash. :returns: The target hash file. ''' - if not torrent_hash is None: - self.__torrent_hash = torrent_hash - return self.__torrent_hash + if not torrent_infohash is None: + self.__torrent_infohash = torrent_infohash + return self.__torrent_infohash - def torrent_length (self, torrent_length=None): - '''Get or set the size of the torrent file. - - :param torrent_length: The torrent size. - :returns: The torrent size. - ''' - if not torrent_length is None: - self.__torrent_length = torrent_length - return self.__torrent_length - - def torrent_contentlength (self, torrent_contentlength=None): + def contentlength (self, torrent_contentlength=None): '''Get or set the size of the target file. :param torrent_contentlength: The target file size. @@ -119,3 +95,33 @@ class TorrentEntryExtension(BaseEntryExtension): if not torrent_contentlength is None: self.__torrent_contentlength = torrent_contentlength return self.__torrent_contentlength + + def seeds (self, torrent_seeds=None): + '''Get or set the number of seeds. + + :param torrent_seeds: The seeds number. + :returns: The seeds number. + ''' + if not torrent_seeds is None: + self.__torrent_seeds = torrent_seeds + return self.__torrent_seeds + + def peers (self, torrent_peers=None): + '''Get or set the number od peers + + :param torrent_infohash: The peers number. + :returns: The peers number. + ''' + if not torrent_peers is None: + self.__torrent_peers = torrent_peers + return self.__torrent_peers + + def verified (self, torrent_verified=None): + '''Get or set the number of verified peers. + + :param torrent_infohash: The verified peers number. + :returns: The verified peers number. + ''' + if not torrent_verified is None: + self.__torrent_verified = torrent_verified + return self.__torrent_verified diff --git a/setup.py b/setup.py index 059dcb7..1dbdca4 100755 --- a/setup.py +++ b/setup.py @@ -8,11 +8,11 @@ setup( name = 'feedgen', packages = ['feedgen', 'feedgen/ext'], version = feedgen.version.version_full_str, - description = 'Feed Generator (ATOM, RSS, Podcasts)', + description = 'Feed Generator (ATOM, RSS, Podcasts, torrent)', author = 'Lars Kiesow', author_email = 'lkiesow@uos.de', url = 'http://lkiesow.github.io/python-feedgen', - keywords = ['feed','ATOM','RSS','podcast'], + keywords = ['feed','ATOM','RSS','podcast','torrent'], license = 'FreeBSD and LGPLv3+', install_requires = ['lxml', 'dateutils'], classifiers = [