Google news in terminal

<

p>After <a href=http://h3manth.com/content/upload-picasa-command-line-using-googlecl">Upload to picasa from cli and google-sunrise-sunset-terminal in past few days, i was gaming on some substantial data gathering code. Python and Google indeed blend very well together, this time i tried a tiny class that gives the top results from Google news from each category.

Google follows a unique URI pattern in all of its services, this time it was Google news service gather my attention from my terminal.
The idea was pretty simple to get the top news in the terminal with shorten URL's,i have made use of python feedparser and is.gd api to achieve this task.
Many might argue that they will just read them in there best feed readers or just the browser, but yes most of the command line junkies would love to have everything in there terminal, like even a music player for that fact!

#!/usr/bin/env python
# Twitt rss feeds
# gnews.py
# Author Hemanth.HM
# Site : www.h3manth.com
# Licensed under GNU GPL Version 3

import feedparser import urllib2

class Googlenews: """ This class helps to fetch top news from specific catagories of Goog News""" def get_input(self): """ Prompts user to select his choice of news """ bold = "\033[1m" reset = "\033[0;0m" try: print """ News feed list : 1.Top \n 2.World \n 3.Australia \n 4.Canada 5.India \n 6.Ireland \n7.New 8.South 9.US 10.UK \n 11.Business \n12Sci/Tech \n13.Health 14.Sports \n15.Entertainment """ news_type = int(raw_input(bold+"Which you you like to read : "+reset)) return news_type except ValueError: print 'Invalid input, please select a number for the list'

<span style='color:#7f0055; font-weight:bold; '>def</span> get_feed(self):
    <span style='color:#3f7f59; '>""" Returns the feed type based on the users input """</span>
    url=<span style='color:#2a00ff; '>"http://news.google.com/news?ned=%s&amp;topic=%s&amp;output=rss"</span>
    links=[{<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"h"</span>},
           {<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"w"</span>},
           {<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"n"</span>},
           {<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"n"</span>},
           {<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"n"</span>},
           {<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"n"</span>},
           {<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"nz"</span>},
           {<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"sa"</span>},
           {<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"n"</span>},
           {<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"n"</span>},
           {<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"b"</span>},
           {<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"t"</span>},
           {<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"m"</span>},
           {<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"s"</span>},
           {<span style='color:#2a00ff; '>"ned"</span>:<span style='color:#2a00ff; '>"us"</span>, <span style='color:#2a00ff; '>"type"</span>:<span style='color:#2a00ff; '>"e"</span>},
          ]
    feed = links[self.get_input()]
    <span style='color:#7f0055; font-weight:bold; '>return</span> url%(feed[<span style='color:#2a00ff; '>"ned"</span>],feed[<span style='color:#2a00ff; '>"type"</span>])

<span style='color:#7f0055; font-weight:bold; '>def</span> shrink(self,url):
    <span style='color:#3f7f59; '>""" Shrink URI using the is.gd API """</span>
    surl = <span style='color:#2a00ff; '>"http://is.gd/api.php?longurl=%s"</span>%(url)
    <span style='color:#7f0055; font-weight:bold; '>return</span> urllib2.urlopen(surl).read()

<span style='color:#7f0055; font-weight:bold; '>def</span> gnews(self):
    <span style='color:#3f7f59; '>""" Returns the top ten news of the selected section """</span>
    feed_url = self.get_feed()
    feed_data = feedparser.parse(feed_url)
    <span style='color:#7f0055; font-weight:bold; '>for</span> data <span style='color:#7f0055; font-weight:bold; '>in</span> feed_data[<span style='color:#2a00ff; '>"items"</span>]:
        <span style='color:#7f0055; font-weight:bold; '>print</span> data[<span style='color:#2a00ff; '>"title"</span>] + <span style='color:#2a00ff; '>":"</span> + self.shrink(data[<span style='color:#2a00ff; '>"link"</span>])

def main(): """ The main block where all the action happens """ news = Googlenews() news.gnews()

if name == "main": main()

Get the code

Share this