Skip to content

Instantly share code, notes, and snippets.

@wirewc
Created September 3, 2014 19:05
Show Gist options
  • Save wirewc/a147f67e24d36a25cf7e to your computer and use it in GitHub Desktop.
Save wirewc/a147f67e24d36a25cf7e to your computer and use it in GitHub Desktop.
News for MakePi
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
"""This reads the news for google news."""
import feedparser,requests,json,urllib,urllib2
tech = """https://news.google.com/news/section?pz=1&cf=all&ned=us&hl=en&topic=tc&output=rss"""
archNewsUrl = """https://www.archlinux.org/feeds/news/"""
hadUrl = """http://feeds2.feedburner.com/hackaday/LgoM"""
mlvUrl= """http://makelehighvalley.com/blog/feed/"""
h76Url = """http://feeds.feedburner.com/Hive76"""
crackedUrl = """http://feeds.feedburner.com/CrackedRSS"""
keenerdUrl = """http://kmkeen.com/rss.xml"""
ctechUrl = """http://rss.cnn.com/rss/cnn_tech.rss"""
cnnUrl = """http://rss.cnn.com/rss/cnn_topstories.rss"""
hscUrl = """http://hackerspaceclt.com/feed/"""
tatersUrl = """www.taskkillim.me/?feed=rss2"""
archAssaultUrl = """https://archassault.org/feeds/news/"""
archAssaultRepoUrl = """https://archassault.org/feeds/packages/"""
archAssaultBugUrl = """https://bugs.archassault.org/feed.php?feed_type=rss2&project=3"""
guppieUrl = """https://www.eyrasecurity.com/blog/feed/"""
keeniggUrl = """http://igg.kmkeen.com/rss.xml"""
months = {'Jan':'00','Feb':'01','Mar':'02','Apr':'03','May':'04','Jun':'05', \
'Jul':'06','Aug':'07','Sep':'08','Oct':'09','Nov':'10','Dec':'11'}
def newsOP(phenny,input):
"""Displays the options for all the news options."""
commands = ['guppy','gtech','HaD','archnews','mlv','h76news','cnn','ctech','keenerd','keenigg','hsc','aa','aarepo','aabugs']
output = ""
for i in range(len(commands)):
if i != 0:
output+=", "
output += commands[i]
phenny.reply(output)
newsOP.commands = ["news"]
newsOP.thread = 'true'
def gtech(phenny,input):
"""QueenBee function to get the most recent news from
Google News Technology."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = recentTech(numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
gtech.commands = ['gtech']
gtech.thread = 'true'
def archNews(phenny,input):
"""QueenBee function to get the most recent news from
Arch Linux News."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = aNewsStory(numStories) # Gets the most recent stories
phenny.reply(stories) # Send them to channel in a reply
archNews.commands = ['archnews']
archNews.thread = 'true'
def hadNews(phenny,input):
"""QueenBee function to get the most recent news from
Arch Linux News."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = hadNewsStory(numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
hadNews.commands = ['HaD']
hadNews.thread = 'true'
def mlvNews(phenny,input):
"""QueenBee function to get the most recent news from
Make Lehigh Valley News."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = arrayEntries(mlvUrl,numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
mlvNews.commands = ['mlv']
mlvNews.thread = 'true'
def h76News(phenny,input):
"""QueenBee function to get the most recent news from
Hive76 News."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = arrayEntries(h76Url,numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
h76News.commands = ['h76news']
h76News.thread = 'true'
def crackedNews(phenny,input):
"""QueenBee function to get the most posts from
cracked.com."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = arrayEntries(crackedUrl,numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
crackedNews.commands = ['cracked']
crackedNews.thread = 'true'
def keenerdNews(phenny,input):
"""QueenBee function to get the most posts from
keenerds webpage."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = arrayEntries(keenerdUrl,numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
keenerdNews.commands = ['keenerd']
keenerdNews.thread = 'true'
def ctechNews(phenny,input):
"""QueenBee function to get the most posts from
CNN Technology webpage."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = arrayEntries(ctechUrl,numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
ctechNews.commands = ['ctech']
ctechNews.thread = 'true'
def cnnNews(phenny,input):
"""QueenBee function to get the most posts from
CNN webpage."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = arrayEntries(cnnUrl,numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
cnnNews.commands = ['cnn']
cnnNews.thread = 'true'
def hscNews(phenny,input):
"""QueenBee function to get the most posts from
Hack Space Charlotte webpage."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = arrayEntries(hscUrl,numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
hscNews.commands = ['hsc']
hscNews.thread = 'true'
def tatersNews(phenny,input):
"""QueenBee function to get the most posts from
taters webpage."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = arrayEntries(tatersUrl,numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
tatersNews.commands = ['taters']
tatersNews.thread = 'true'
def archAssaultNews(phenny,input):
"""QueenBee function to get the most posts from
Arch Assault webpage."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = arrayEntries(archAssaultUrl,numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
archAssaultNews.commands = ['aa']
archAssaultNews.thread = 'true'
def archAssaultRepo(phenny,input):
"""QueenBee function to get the most posts from
Arch Assault webpage."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = arrayEntries(archAssaultRepoUrl,numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
archAssaultRepo.commands = ['aarepo']
archAssaultRepo.thread = 'true'
def archAssaultBugs(phenny,input):
"""QueenBee function to get the most posts from
Arch Assault webpage."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = arrayEntries(archAssaultBugUrl,numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
archAssaultBugs.commands = ['aabugs']
archAssaultBugs.thread = 'true'
def guppie(phenny,input):
"""Pulls from Guppy's company blog."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = arrayEntries(guppieUrl,numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
guppie.commands = ['guppy']
guppie.thread = 'true'
def keenigg(phenny,input):
"""Pulls from Guppy's company blog."""
if input.group(2):
try:
numStories = int(input.group(2))
except:
numStories = 1
else:
numStories = 1
stories = arrayEntries(keeniggUrl,numStories) # Gets the most recent stories
lenStories = len(stories)
for i in range(lenStories):
phenny.reply(str(i)+".) "+stories[i])
keenigg.commands = ['keenigg']
keenigg.thread = 'true'
def enumStories(num=10):
"""Gets an enumerated list of the latest tech stories."""
feed = feedparser.parse(tech)
for i in range(10):
print((str(i)+".) "+unicode(feed['entries'][i]['title'])+\
" - "+shorten(feed['entries'][i]['link'])))+"\n"
def arrayEntries(url,num=1):
"""Takes any RSS feed and returns the entries to the num
specified."""
feed = feedparser.parse(url)
data = []
max = len(feed['entries'])
retVal = []
if(num>max):
num = max
for i in range(max):
data.append(convertDate(feed['entries'][i]['published'],i))
data.sort()
data.reverse()
for i in range(num):
retVal.append(unicode(feed['entries'][data[i][1]]['title'])+\
" - "+shorten(feed['entries'][data[i][1]]['link']))
return retVal
pass
def recentTech(num):
"""Returns the most recent stories specified."""
feed = feedparser.parse(tech)
data = []
max = len(feed['entries'])
retVal = []
if(num>max):
num = max
for i in range(max):
data.append(convertDate(feed['entries'][i]['published'],i))
data.sort()
data.reverse()
for i in range(num):
retVal.append(unicode(feed['entries'][data[i][1]]['title'])+\
" - "+shorten(feed['entries'][data[i][1]]['link']))
return retVal
pass
def hadNewsStory(num):
"""Returns the most recent stories specified."""
feed = feedparser.parse(hadUrl)
data = []
max = len(feed['entries'])
retVal = []
if(num>max):
num = max
for i in range(max):
data.append(convertDate(feed['entries'][i]['published'],i))
data.sort()
data.reverse()
for i in range(num):
retVal.append(unicode(feed['entries'][data[i][1]]['title'])+\
" - "+shorten(feed['entries'][data[i][1]]['link']))
return retVal
pass
def getFeed():
return feedparser.parse(tech)
def enumMonth(month):
"""Insert a month with a standard 3 letter code. The funtion
returns the month number. If there is an invalid entry,
returns -1."""
try:
return months[month]
except:
return -1
def trecent(feed):
"""Test function for sorting the date of entries."""
data = []
for i in range(10):
data.append(convertDate(feed['entries'][i]['published'],i))
data.sort()
data.reverse()
return data
def aNewsStory(storyNum=1):
"""Retrieves the latest news story information from the Arch
Linux main page."""
num = storyNum-1
feed = feedparser.parse(archNewsUrl)
max = len(feed['entries'])
if(num>max):
num = max
retVal = feed['entries'][num]['title']+" Posted: "+\
feed['entries'][num]['published']+" - "+\
shorten(feed['entries'][num]['link']) + ""
return retVal
def convertDate(feedDate,entry):
"""Converts the data to a string of numbers delimited by
semicolons for sorting purposes."""
data = feedDate.split(" ")
retval = data[3]+":"+str(months[data[2]])+":"+data[1]+":"+data[4]+""
return [retval,entry]
def shorten(url):
gurl = 'https://www.googleapis.com/urlshortener/v1/url'
req = urllib2.Request(gurl, data= "{\"longUrl\": \"" + url + "\"}" )
req.add_header('Content-Type', 'application/json')
results = json.load(urllib2.urlopen(req))
return results['id']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment