Hemanth's Scribes

web

Google News in Terminal

Author Photo

Hemanth HM

Thumbnail

Google News in Terminal

After 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'
    
    def get_feed(self):
        """ Returns the feed type based on the users input """
        url="http://news.google.com/news?ned=%s&topic=%s&output=rss"
        links=[{"ned":"us", "type":"h"},
               {"ned":"us", "type":"w"},
               {"ned":"us", "type":"n"},
               {"ned":"us", "type":"n"},
               {"ned":"us", "type":"n"},
               {"ned":"us", "type":"n"},
               {"ned":"us", "type":"nz"},
               {"ned":"us", "type":"sa"},
               {"ned":"us", "type":"n"},
               {"ned":"us", "type":"n"},
               {"ned":"us", "type":"b"},
               {"ned":"us", "type":"t"},
               {"ned":"us", "type":"m"},
               {"ned":"us", "type":"s"},
               {"ned":"us", "type":"e"},
              ]
        feed = links[self.get_input()]
        return url%(feed["ned"],feed["type"])
        
    def shrink(self,url):
        """ Shrink URI using the is.gd API """
        surl = "http://is.gd/api.php?longurl=%s"%(url)
        return urllib2.urlopen(surl).read()
        
    def gnews(self):
        """ Returns the top ten news of the selected section """
        feed_url = self.get_feed()
        feed_data = feedparser.parse(feed_url)
        for data in feed_data["items"]:
            print data["title"] + ":" + self.shrink(data["link"])

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

Get the code

#javascript#linux
Author Photo

About Hemanth HM

Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.