python-feedgen/feedgen/ext/torrent.py

128 lines
3.6 KiB
Python
Raw Normal View History

2016-05-24 00:46:11 +02:00
# -*- coding: utf-8 -*-
'''
2016-05-24 15:48:18 +02:00
feedgen.ext.torrent
2016-05-24 00:46:11 +02:00
~~~~~~~~~~~~~~~~~~~
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):
2016-05-24 23:56:32 +02:00
self.__torrent_filename = None
self.__torrent_infohash = None
self.__torrent_contentlength = None
self.__torrent_seeds = None
self.__torrent_peers = None
self.__torrent=verified = None
2016-05-24 00:46:11 +02:00
2016-05-24 23:56:32 +02:00
2016-05-24 00:46:11 +02:00
def extend_rss(self, entry):
'''Add additional fields to an RSS item.
:param feed: The RSS item XML element to use.
'''
2016-05-24 23:56:32 +02:00
if self.__torrent_filename:
filename = etree.SubElement(entry, '{%s}filename' % TORRENT_NS)
filename.text = self.__torrent_filename
2016-05-24 15:48:18 +02:00
2016-05-24 23:56:32 +02:00
if self.__torrent_contentlength:
contentlength = etree.SubElement(entry, '{%s}contentlength' % TORRENT_NS)
contentlength.text = self.__torrent_contentlength
2016-05-24 15:48:18 +02:00
2016-05-24 23:56:32 +02:00
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
2016-05-24 15:48:18 +02:00
2016-05-24 23:56:32 +02:00
if self.__torrent_seeds:
seeds = etree.SubElement(entry, '{%s}seed' % TORRENT_NS)
seeds.text = self.__torrent_seeds
2016-05-24 15:48:18 +02:00
2016-05-24 23:56:32 +02:00
if self.__torrent_peers:
peers = etree.SubElement(entry, '{%s}peers' % TORRENT_NS)
peers.text = self.__torrent_peers
2016-05-24 15:48:18 +02:00
2016-05-24 23:56:32 +02:00
if self.__torrent_seeds:
verified = etree.SubElement(entry, '{%s}verified' % TORRENT_NS)
verified.text = self.__torrent_verified
2016-05-24 15:48:18 +02:00
2016-05-24 23:56:32 +02:00
def filename(self, torrent_filename=None):
2016-05-24 15:48:18 +02:00
'''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
2016-05-24 23:56:32 +02:00
def infohash (self, torrent_infohash=None):
2016-05-24 15:48:18 +02:00
'''Get or set the hash of the target file.
2016-05-24 23:56:32 +02:00
:param torrent_infohash: The target file hash.
2016-05-24 15:48:18 +02:00
:returns: The target hash file.
'''
2016-05-24 23:56:32 +02:00
if not torrent_infohash is None:
self.__torrent_infohash = torrent_infohash
return self.__torrent_infohash
2016-05-24 15:48:18 +02:00
2016-05-24 23:56:32 +02:00
def contentlength (self, torrent_contentlength=None):
2016-05-24 15:48:18 +02:00
'''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
2016-05-24 23:56:32 +02:00
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