itrade_news_google.py :  » Business-Application » iTrade » itrade » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Business Application » iTrade 
iTrade » itrade » itrade_news_google.py
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
# ============================================================================
# Project Name : iTrade
# Module Name  : itrade_news_google.py
#
# Description: News from Google.com
#
# The Original Code is iTrade code (http://itrade.sourceforge.net).
#
# The Initial Developer of the Original Code is  Gilles Dumortier.
#
# Portions created by the Initial Developer are Copyright (C) 2004-2008 the
# Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see http://www.gnu.org/licenses/gpl.html
#
# History       Rev   Description
# 2005-10-23    dgil  Wrote it from scratch
# ============================================================================

# ============================================================================
# Imports
# ============================================================================

# python system
import logging
import re
import time
import webbrowser
import string

# iTrade system
from itrade_logging import *
from itrade_local import setLocale,getLocale

# feedparser
import feedparser

# ============================================================================
# Used to build a Feed entry
# ============================================================================

class _FeedEntry(object):
    pass

# ============================================================================
# News_Google()
#
# Google RSS News : http://news.google.fr/news?hl=%s&ie=UTF-8&output=rss&q=%s
#   hl  language
#   q   company name
# ============================================================================

class News_Google(object):
    def __init__(self):
        debug('News_Google:__init__')
        self.m_feed = None
        self.m_url = None
        self.m_quote = None

        self.m_baseurl = "http://news.google.fr/news?hl=%s&ie=UTF-8&output=rss&q=%s&scoring=d"

    # ---[ protected interface ] ---
    def getURL(self):
        return self.m_url

    def getFeed(self):
        return self.m_feed

    def getQuote(self):
        return self.m_quote

    def feed(self,url):
        self.m_url = url
        info('RSS News refresh %s',self.m_url)
        self.m_feed = self.convert(feedparser.parse(self.m_url))
        return self.m_feed

    def convert(self,feed):
        # convert Google RSS to internal RSS
        if not feed:
            return None

        flux = _FeedEntry()
        flux.entries = []
        flux.feed = _FeedEntry()
        flux.feed.title = feed.feed.title

        # force local to 'en' : strptime() will work correctly
        setLocale('en')

        for eachEntry in feed.entries:
            entry = _FeedEntry()
            entry.link = "google::%s" % eachEntry.link
            entry.title = "%s" % eachEntry.title

            # convert datetime in the right locale
            entry.date = time.strptime(eachEntry.date, "%a, %d %b %Y %H:%M:%S %Z")
            print '%s: %s -> %s' % (entry.link,eachEntry.date,entry.date)

            entry.summary = eachEntry.summary
            entry.source = "google"
            flux.entries.append(entry)

        # restore locale
        setLocale()

        #info('Feed %s',flux.feed.title)
        return flux

    def goto(self,html,url):        # __x to be removed
        webbrowser.open(url)

    # ---[ public interface ] ---
    def feedQuote(self,quote,lang=None,page=0):
        self.m_quote = quote
        if lang==None:
            lang = self.m_quote.country()

        param = self.m_quote.name()+' '+self.m_quote.ticker()
        param = string.replace(param,' ','+')

        return self.feed(self.m_baseurl % (lang,param))

# ============================================================================
# Export me
# ============================================================================

try:
    ignore(gNewsGoogle)
except NameError:
    gNewsGoogle = News_Google()

# ============================================================================
# Test me
# ============================================================================

if __name__=='__main__':
    setLevel(logging.INFO)

    from itrade_local import setLang
    setLang('fr')

    from itrade_quotes import quotes
    quote = quotes.lookupTicker('AAPL')
    print gNewsGoogle.feedQuote(quote)

    quote = quotes.lookupTicker('AXL')
    print gNewsGoogle.feedQuote(quote)
    print gNewsGoogle.feedQuote(quote,'fr')

    quote = quotes.lookupTicker('SAF')
    print gNewsGoogle.feedQuote(quote)

# ============================================================================
# That's all folks !
# ============================================================================
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.