Created
August 13, 2018 16:08
-
-
Save armgit5/0de1547d0bbd17db1f1002b327f2d4ef to your computer and use it in GitHub Desktop.
1. rss_feed.py - core class file for rss_feed (separated into feed and items)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rss_feed import Feed_Channel, Items | |
class Print_Out(): | |
def __init__(self, urls): | |
self.urls = urls | |
self.outputs = "" | |
def print_out(self): | |
for url in self.urls: | |
feed_title = Feed_Channel(URL, "title") | |
feed_description = Feed_Channel(URL, "description") | |
items = Items(URL) | |
# Print out feed title and description | |
self.outputs += feed_title.printout() | |
self.outputs += "\n" | |
self.outputs +=feed_description.printout() | |
self.outputs += "\n" | |
# Print out all items | |
self.outputs += items.printout() | |
self.outputs += "\n" | |
URL = "http://tech.uzabase.com/rss" | |
# Can accept many urls by adding more urls in the array below | |
p = Print_Out([URL]) # This case is only 1 url | |
p.print_out() # Print out the output | |
# Can save to results.txt | |
with open('results.txt','w',encoding='utf8') as f: | |
f.write(p.outputs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import feedparser | |
import re | |
class Uzabase_RSS_Feed(): | |
def __init__(self, url): | |
self.url = url | |
self.feed = feedparser.parse(self.url) # Feed data | |
self.items = self.feed["items"] # Items data | |
# Exclude the word “NewsPicks” from sentence | |
# return the sentence without "NewsPicks" | |
def conversion(self, sentence): | |
return re.sub("NewsPicks", "", sentence) | |
def contains_newspick(self, sentence): | |
return bool(re.search(r'\bNewsPicks\b', sentence)) | |
class Feed_Channel(Uzabase_RSS_Feed): | |
def __init__(self, url, attribute): | |
Uzabase_RSS_Feed.__init__(self, url) | |
self.attribute = attribute | |
# check to make sure the attribute inserted is correct | |
assert self.feed["channel"][attribute], "the attribute inserted is not correct" | |
self.sentence = self.feed["channel"][attribute] # Data used for exclusion | |
def printout(self): | |
# check if contains "NewsPick" if so then output | |
# the sentence | |
output = "" | |
if (self.contains_newspick(self.sentence)): | |
conversed_sentence = self.conversion(self.sentence) | |
output = "Feed channel " + self.attribute + " conversion = " + conversed_sentence | |
else: | |
output = "Feed channel " + self.attribute + " original = " + self.sentence | |
print(output) | |
return output | |
class Item(Uzabase_RSS_Feed): | |
def __init__(self, index, item, url): | |
Uzabase_RSS_Feed.__init__(self, url) | |
self.index = index | |
self.title = item["title"] # Take item title into consideration | |
self.description = item["description"] # Take item description into consideration | |
def printout(self): | |
# check if contains "NewsPick" if so then output | |
# the sentence | |
title_output = "" | |
description_output = "" | |
# check title output | |
if (self.contains_newspick(self.title)): | |
conversed_sentence = self.conversion(self.title) | |
title_output = "Item title " + str(self.index) + " conversion = " + conversed_sentence | |
else: | |
title_output = "Item title " + str(self.index) + " original = " + self.title | |
# check title description | |
if (self.contains_newspick(self.description)): | |
conversed_sentence = self.conversion(self.description) | |
description_output = "Item description " + str(self.index) + " conversion = " + conversed_sentence | |
else: | |
description_output = "Item description " + str(self.index) + " original = " + self.description | |
output = title_output + "\n" + description_output | |
print(output) | |
return output | |
class Items(Uzabase_RSS_Feed): | |
def __init__(self, url): | |
Uzabase_RSS_Feed.__init__(self, url) | |
def printout(self): | |
# check if contains "NewsPick" if so then output | |
# the sentence | |
outputs = "" | |
for i, item in enumerate(self.items): | |
item_instace = Item(i, item, self.url) | |
outputs += item_instace.printout() | |
outputs += "\n" | |
return outputs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment