Merge branch 'master' of https://github.com/raspbeguy/python-feedgen into torrent

This commit is contained in:
Lars Kiesow 2016-09-04 15:53:24 +02:00
commit 26b40e6c91
6 changed files with 153 additions and 3 deletions

2
.gitignore vendored
View file

@ -10,3 +10,5 @@ feedgen/tests/tmp_Rssfeed.xml
tmp_Atomfeed.xml
tmp_Rssfeed.xml
build/**

View file

@ -17,3 +17,4 @@ Contents:
ext/api.ext.dc
ext/api.ext.podcast
ext/api.ext.podcast_entry
ext/api.ext.torrent

View file

@ -0,0 +1,7 @@
.. raw:: html
<div class="apititle"><b>Contents</b></div>
<div class="apitoc"></div>
.. automodule:: feedgen.ext.torrent
:members:

View file

@ -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 ( <file>.atom | atom | <file>.rss | rss | podcast )' % \
print_enc ('Usage: %s ( <file>.atom | atom | <file>.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()
@ -91,6 +93,17 @@ if __name__ == '__main__':
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')
fg.dc.dc_contributor('Lars Kiesow')

127
feedgen/ext/torrent.py Normal file
View file

@ -0,0 +1,127 @@
# -*- coding: utf-8 -*-
'''
feedgen.ext.torrent
~~~~~~~~~~~~~~~~~~~
Extends the FeedGenerator to produce torrent feeds.
:copyright: 2016, Raspbeguy <raspbeguy@hashtagueule.fr>
: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_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.
'''
if self.__torrent_filename:
filename = etree.SubElement(entry, '{%s}filename' % TORRENT_NS)
filename.text = self.__torrent_filename
if self.__torrent_contentlength:
contentlength = etree.SubElement(entry, '{%s}contentlength' % TORRENT_NS)
contentlength.text = self.__torrent_contentlength
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 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 infohash (self, torrent_infohash=None):
'''Get or set the hash of the target file.
:param torrent_infohash: The target file hash.
:returns: The target hash file.
'''
if not torrent_infohash is None:
self.__torrent_infohash = torrent_infohash
return self.__torrent_infohash
def 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
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

View file

@ -10,11 +10,11 @@ setup(
name = 'feedgen',
packages = packages,
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', 'python-dateutil'],
classifiers = [