Commit 2e90d9cf authored by Mats Sjöberg's avatar Mats Sjöberg

Moved Item, Feed classes to

parent 6e720af9
import collections
import re
import feedparser
class Item(object):
"""Class encapsulating the information for a single podcast item,
i.e. a single episode. Also contains a static cache keeping track
of already downloaded episodes.
cache = collections.OrderedDict()
cache_read = False
cache_file = "cache"
def __init__(self, item_data, feed):
if not Item.cache_read:
Item.read_cache() = item_data
self.feed = feed
summ =["summary"]
summ = re.sub('<[^<]+?>', ' ', summ)
summ = summ.replace('&#38;', '&').replace('&#8230;', '...')
summ = re.sub('&#?[0-9a-z]+;', '', summ)
summ = re.sub('[ \t]+', ' ', summ)
summ = re.sub('\n+', '\n', summ)
self.summary = summ
def __str__(self):
return "[{0}] {1}".format(self.feed.title(),
def is_new(self):
return self.guid() not in Item.cache
def mark_as_seen(self):
Item.cache[self.guid()] = self.title()
def guid(self):
"""Method that determines and return a unique identifier of
the item."""
def author(self):
if "author" in
return ""
def title(self):
def auto_download(self):
return self.feed.do_auto
def download_url(self):
url = ""
if "media_content" in
for media in["media_content"]:
if "url" in media:
url = media["url"]
elif "enclosure" in
url =["enclosure"]["url"]
elif "links" in
for link in["links"]:
if link["rel"] == "enclosure":
url = link["href"]
return url
def download_localname(self):
dl_url = self.download_url()
parts = dl_url.rpartition('.')
return re.sub('[^a-zA-Z0-9]', '_', self.title()) + '.' + parts[2]
def print_summary(self, max_summary_lines):
# clear_screen()
print("\n\n*", self)
sumlines = self.summary.splitlines()
summary = "\n".join(sumlines[0:max_summary_lines])
if len(sumlines) > max_summary_lines:
summary += "\n..."
author =
if author != "":
summary = "Author: " + author + "\n" + summary
def setup_cache(dir):
Item.cache_file = dir+"/cache"
def save_cache():
"""Saves cache from memory to file."""
with open(Item.cache_file, "w") as fp:
for guid, title in Item.cache.items():
fp.write(guid + '\t' + title + '\n')
def read_cache():
"""Reads old cache from file to memory."""
with open(Item.cache_file) as fp:
for line in fp:
(guid, title) = line.rstrip().split('\t')
Item.cache[guid] = title
Item.cache_read = True
class Feed(object):
Class encapsulating the information for a podcast feed.
def __init__(self, url, dirname, do_auto):
self.url = url
self.dirname = dirname
self.do_auto = do_auto = {}
def update(self, newest=0): = feedparser.parse(self.url)
title = self.title()
if not title:
print("Error updating", self.url)
return []
print("Updating", self.title(), "...")
new_items = []
count = 0
for item_data in["items"]:
item = Item(item_data, self)
if item.is_new():
if newest == 0 or count < newest:
count += 1
# Item.save_cache()
return new_items
def title(self):
if "title" in["channel"]:
return ""
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment