Пример #1
0
class Reader(object) :
    def __init__(self,login,passwd) :
        self._googlereader = GoogleReader()
        self._googlereader.identify(login=login,passwd=passwd)
        if not(self._googlereader.login()) :
            raise "Can't login"
        self._database = ReaderDatabase('reader.sqlite3')
    def synchro(self) :
        xmlfeed = self._googlereader.get_feed(n=200)
        self._database.start_add_session()
        for entry in xmlfeed.get_entries() :
            self._database.add_item(
                google_id=entry['google_id'].encode('utf-8'),
                original_id=entry['original_id'].encode('utf-8'),
                link=entry['link'].encode('utf-8'),
                content=entry['content'].encode('utf-8'),
                title=entry['title'].encode('utf-8'),
                author=entry['author'].encode('utf-8'),
                published=entry['published'],
                updated=entry['updated'],
                crawled=entry['crawled'],
                )
            for term in entry['categories'] :
                label = entry['categories'][term]
                self._database.add_item_categorie(
                    google_id=entry['google_id'].encode('utf-8'),
                    categorie_name=term.encode('utf-8'),
                    categorie_shortname=label.encode('utf-8'),
                    )
        self._database.stop_add_session()
Пример #2
0
class Reader(object):
    def __init__(self, login, passwd):
        self._googlereader = GoogleReader()
        self._googlereader.identify(login=login, passwd=passwd)
        if not (self._googlereader.login()):
            raise "Can't login"
        self._database = ReaderDatabase('reader.sqlite3')

    def synchro(self):
        xmlfeed = self._googlereader.get_feed(n=200)
        self._database.start_add_session()
        for entry in xmlfeed.get_entries():
            self._database.add_item(
                google_id=entry['google_id'].encode('utf-8'),
                original_id=entry['original_id'].encode('utf-8'),
                link=entry['link'].encode('utf-8'),
                content=entry['content'].encode('utf-8'),
                title=entry['title'].encode('utf-8'),
                author=entry['author'].encode('utf-8'),
                published=entry['published'],
                updated=entry['updated'],
                crawled=entry['crawled'],
            )
            for term in entry['categories']:
                label = entry['categories'][term]
                self._database.add_item_categorie(
                    google_id=entry['google_id'].encode('utf-8'),
                    categorie_name=term.encode('utf-8'),
                    categorie_shortname=label.encode('utf-8'),
                )
        self._database.stop_add_session()
Пример #3
0
    def triggerReload(self):
        self.reloading = True

        newfeeds = []
        oldfeeds = self.feeds
        found = False
        for x in config.plugins.simpleRSS.feed:
            for feed in oldfeeds:
                if x.uri.value == feed.uri:
                    # Update possibly different autoupdate value
                    feed.autoupdate = x.autoupdate.value
                    newfeeds.append(feed)  # Append to new Feeds
                    oldfeeds.remove(feed)  # Remove from old Feeds
                    found = True
                    break
            if not found:
                newfeeds.append(UniversalFeed(x.uri.value, x.autoupdate.value))
            found = False

        self.feeds = newfeeds

        if config.plugins.simpleRSS.enable_google_reader.value:
            self.googleReader = GoogleReader(
                config.plugins.simpleRSS.google_username.value,
                config.plugins.simpleRSS.google_password.value)
            self.googleReader.login().addCallback(
                self.googleLoggedIn).addErrback(self.googleLoginFailed)
        else:
            self.reloading = False
Пример #4
0
    def __init__(self, poll=True):
        # Timer
        self.poll_timer = eTimer()
        self.poll_timer_conn = self.poll_timer.timeout.connect(self.poll)
        self.do_poll = poll

        # this indicates we're reloading the list of feeds
        self.reloading = False

        self.newItemFeed = BaseFeed(
            "",
            _("New Items"),
            _("New Items since last Auto-Update"),
        )

        # Generate Feeds
        self.feeds = [
            UniversalFeed(x.uri.value, x.autoupdate.value)
            for x in config.plugins.simpleRSS.feed
        ]

        if not config.plugins.simpleRSS.enable_google_reader.value:
            if poll:
                self.poll_timer.start(0, 1)
        else:
            self.googleReader = GoogleReader(
                config.plugins.simpleRSS.google_username.value,
                config.plugins.simpleRSS.google_password.value)
            self.googleReader.login().addCallback(
                self.googleLoggedIn).addErrback(self.googleLoginFailed)

        # Initialize Vars
        self.current_feed = 0
Пример #5
0
def recommend_with_google(request):
    """
    Make a recommendation by parsing the user's google reader feeds
    """
    form = GoogleLoginForm(request.POST)

    if form.is_valid():
        cache_key = "feed-hashes-%s" % form.cleaned_data.get("username")
        feed_hashes = cache.get(cache_key)
        
        if not feed_hashes:
            reader = GoogleReader()
            reader.identify(form.cleaned_data.get("username"), form.cleaned_data.get("password"))

            if reader.login():
                feeds = reader.get_subscription_list()
                client = PostRank()
                feed_hashes = []

                for feed in feeds.get("subscriptions"):
                    if feed.get("id"):
                        feed_hash = client.get_feed_hash(feed.get("id")[5:])

                        if feed_hash:
                            feed_hashes.append(feed_hash)

                cache.set(cache_key, feed_hashes, 24 * 60 * 60)
            else:
                if request.is_ajax():
                    template = "google_error.html"
                else:
                    template = "google_error_page.html"

                return render_to_response("recommender/%s" % template, {

                }, context_instance=RequestContext(request))

        if feed_hashes:
            recommendations = client.get_recommendations(feed_hashes, limit=5)

            if recommendations:
                if request.is_ajax():
                    template = "results.html"
                else:
                    template = "results_page.html"

                return render_to_response("recommender/%s" % template, {
                    "results": recommendations,
                }, context_instance=RequestContext(request))

    return render_to_response("recommender/google.html", {
        "recommend_form": form,
    }, context_instance=RequestContext(request))
Пример #6
0
	def __init__(self, poll = True):
		# Timer
		self.poll_timer = eTimer()
		self.poll_timer_conn = self.poll_timer.timeout.connect(self.poll)
		self.do_poll = poll

		# this indicates we're reloading the list of feeds
		self.reloading = False

		self.newItemFeed = BaseFeed(
			"",
			_("New Items"),
			_("New Items since last Auto-Update"),
		)

		# Generate Feeds
		self.feeds = [
			UniversalFeed(
				x.uri.value,
				x.autoupdate.value
			)
				for x in config.plugins.simpleRSS.feed
		]

		if not config.plugins.simpleRSS.enable_google_reader.value:
			if poll:
				self.poll_timer.start(0, 1)
		else:
			self.googleReader = GoogleReader(config.plugins.simpleRSS.google_username.value, config.plugins.simpleRSS.google_password.value)
			self.googleReader.login().addCallback(self.googleLoggedIn).addErrback(self.googleLoginFailed)

		# Initialize Vars
		self.current_feed = 0
Пример #7
0
	def triggerReload(self):
		self.reloading = True

		newfeeds = []
		oldfeeds = self.feeds
		found = False
		for x in config.plugins.simpleRSS.feed:
			for feed in oldfeeds:
				if x.uri.value == feed.uri:
					# Update possibly different autoupdate value
					feed.autoupdate = x.autoupdate.value
					newfeeds.append(feed) # Append to new Feeds
					oldfeeds.remove(feed) # Remove from old Feeds
					found = True
					break
			if not found:
				newfeeds.append(
					UniversalFeed(
						x.uri.value,
						x.autoupdate.value
				))
			found = False

		self.feeds = newfeeds

		if config.plugins.simpleRSS.enable_google_reader.value:
			self.googleReader = GoogleReader(config.plugins.simpleRSS.google_username.value, config.plugins.simpleRSS.google_password.value)
			self.googleReader.login().addCallback(self.googleLoggedIn).addErrback(self.googleLoginFailed)
		else:
			self.reloading = False
Пример #8
0
    def _get_googlereader(self):
        if self.__googlereader is None:
            login_info = {
                'login': self._config['login'],
                'passwd': self._config['passwd']
            }
            http_proxy = None

            proxy_host = self._config['proxy_host']
            if (proxy_host is not None) and (proxy_host != ''):
                http_proxy = (proxy_host, "%s" % self._config['proxy_port'])

            self.__googlereader = GoogleReader(
                agent='pyrfeed-framework-contact:pyrfeed_at_gmail/%s' %
                pyrfeed_version,
                http_proxy=http_proxy)
            self.__googlereader.identify(**login_info)
            if not (self.__googlereader.login()):
                raise Exception("Can't login")
        return self.__googlereader
Пример #9
0
    def _get_googlereader(self) :
        if self.__googlereader is None :
            login_info = { 'login' : self._config['login'], 'passwd' : self._config['passwd'] }
            http_proxy = None

            proxy_host = self._config['proxy_host']
            if (proxy_host is not None) and (proxy_host!='') :
                http_proxy = (proxy_host,"%s" % self._config['proxy_port'])

            self.__googlereader = GoogleReader(agent='pyrfeed-framework-contact:pyrfeed_at_gmail/%s' % pyrfeed_version,http_proxy=http_proxy)
            self.__googlereader.identify(**login_info)
            if not(self.__googlereader.login()) :
                raise Exception("Can't login")
        return self.__googlereader
Пример #10
0
class GoogleBase(object) :
    _SORT_PREFIXE = 'sort:'

    def __init__(self,config) :
        self._config = config
        self._filter = self._config['filter']
        self.__googlereader = None

    def _get_googlereader(self) :
        if self.__googlereader is None :
            login_info = { 'login' : self._config['login'], 'passwd' : self._config['passwd'] }
            http_proxy = None

            proxy_host = self._config['proxy_host']
            if (proxy_host is not None) and (proxy_host!='') :
                http_proxy = (proxy_host,"%s" % self._config['proxy_port'])

            self.__googlereader = GoogleReader(agent='pyrfeed-framework-contact:pyrfeed_at_gmail/%s' % pyrfeed_version,http_proxy=http_proxy)
            self.__googlereader.identify(**login_info)
            if not(self.__googlereader.login()) :
                raise Exception("Can't login")
        return self.__googlereader

    def get_shortname(self,longname) :
        if longname.startswith(CONST.ATOM_PREFIXE_LABEL) :
            shortname = 'label:' + longname[len(CONST.ATOM_PREFIXE_LABEL):]
        elif longname.startswith(CONST.ATOM_PREFIXE_STATE_GOOGLE) :
            shortname = 'state:' + longname[len(CONST.ATOM_PREFIXE_STATE_GOOGLE):]
        elif longname.startswith(CONST.ATOM_GET_FEED) :
            shortname = 'feed:' + longname[len(CONST.ATOM_GET_FEED):]
        else :
            shortname = longname
        return shortname

    def get_longname(self,shortname) :
        if shortname.startswith('label:') :
            longname = CONST.ATOM_PREFIXE_LABEL + shortname[len('label:'):]
        elif shortname.startswith('state:') :
            longname = CONST.ATOM_PREFIXE_STATE_GOOGLE + shortname[len('state:'):]
        elif longname.startswith('feed:') :
            shortname = CONST.ATOM_GET_FEED + longname[len('feed:'):]
        else :
            longname = shortname
        return longname

    def get_filter(self) :
        return self._filter

    def set_filter(self,filter_command) :
        self._filter = filter_command

    def _format_content(self,content,title,date) :
        fdate = time.strftime(self._config['datefmt'],time.gmtime( date ))
        return "<h1>%s</h1><p align='right'><font size='-1'>%s</font></p><br align='left'>%s" % (title,fdate,content)

    def get_feed_args_default(self) :
        """Get the default arguments for get_feed, based on current configuration"""

        get_feed_args = {}
        max_count = self._config['google/max_count']

        if not(self._config['google/include_read']) :
            get_feed_args['exclude_target'] = CONST.ATOM_STATE_READ
        if self._config['url'] :
            get_feed_args['url'] = self._config['url']
        if self._config['label'] :
            get_feed_args['feed'] = CONST.ATOM_PREFIXE_LABEL + self._config['label']
        elif self._config['feed'] :
            get_feed_args['feed'] = self._config['feed']

        get_feed_args['count'] = max_count

        return get_feed_args
Пример #11
0
 def __init__(self,login,passwd) :
     self._googlereader = GoogleReader()
     self._googlereader.identify(login=login,passwd=passwd)
     if not(self._googlereader.login()) :
         raise "Can't login"
     self._database = ReaderDatabase('reader.sqlite3')
Пример #12
0
class RSSPoller:
    """Keeps all Feed and takes care of (automatic) updates"""
    def __init__(self, poll=True):
        # Timer
        self.poll_timer = eTimer()
        self.poll_timer_conn = self.poll_timer.timeout.connect(self.poll)
        self.do_poll = poll

        # this indicates we're reloading the list of feeds
        self.reloading = False

        self.newItemFeed = BaseFeed(
            "",
            _("New Items"),
            _("New Items since last Auto-Update"),
        )

        # Generate Feeds
        self.feeds = [
            UniversalFeed(x.uri.value, x.autoupdate.value)
            for x in config.plugins.simpleRSS.feed
        ]

        if not config.plugins.simpleRSS.enable_google_reader.value:
            if poll:
                self.poll_timer.start(0, 1)
        else:
            self.googleReader = GoogleReader(
                config.plugins.simpleRSS.google_username.value,
                config.plugins.simpleRSS.google_password.value)
            self.googleReader.login().addCallback(
                self.googleLoggedIn).addErrback(self.googleLoginFailed)

        # Initialize Vars
        self.current_feed = 0

    def googleLoggedIn(self, sid=None):
        self.googleReader.getSubscriptionList().addCallback(
            self.googleSubscriptionList).addErrback(
                self.googleSubscriptionFailed)

    def googleLoginFailed(self, res=None):
        AddPopup(
            _("Failed to login to Google Reader."),
            MessageBox.TYPE_ERROR,
            5,
        )

        self.reloading = False
        if self.do_poll:
            self.poll_timer.start(0, 1)

    def googleSubscriptionList(self, subscriptions=None):
        self.feeds.extend(subscriptions)

        self.reloading = False
        if self.do_poll:
            self.doCallback()
            self.poll_timer.start(0, 1)

    def googleSubscriptionFailed(self, res=None):
        AddPopup(
            _("Failed to get subscriptions from Google Reader."),
            MessageBox.TYPE_ERROR,
            5,
        )

        self.reloading = False
        if self.do_poll:
            self.poll_timer.start(0, 1)

    def addCallback(self, callback):
        if callback not in update_callbacks:
            update_callbacks.append(callback)

    def removeCallback(self, callback):
        if callback in update_callbacks:
            update_callbacks.remove(callback)

    def doCallback(self, id=None):
        for callback in update_callbacks:
            try:
                callback(id)
            except Exception:
                pass

    def error(self, error=""):
        print("[SimpleRSS] failed to fetch feed:", error)

        # Assume its just a temporary failure and jump over to next feed
        self.next_feed()

    def _gotPage(self, data, id=None, callback=False, errorback=None):
        # workaround: exceptions in gotPage-callback were ignored
        try:
            self.gotPage(data, id)
            if callback:
                self.doCallback(id)
        except NotImplementedError as errmsg:
            # Don't show this error when updating in background
            if id is not None:
                AddPopup(
                    _("Sorry, this type of feed is unsupported:\n%s") %
                    (str(errmsg)),
                    MessageBox.TYPE_INFO,
                    5,
                )
            else:
                # We don't want to stop updating just because one feed is broken
                self.next_feed()
        except Exception:
            import traceback, sys
            traceback.print_exc(file=sys.stdout)
            # Errorback given, call it (asumme we don't need do restart timer!)
            if errorback is not None:
                errorback()
                return
            # Assume its just a temporary failure and jump over to next feed
            self.next_feed()

    def gotPage(self, data, id=None):
        feed = cElementTree_fromstring(data)

        # For Single-Polling
        if id is not None:
            self.feeds[id].gotFeed(feed)
            print("[SimpleRSS] single feed parsed...")
            return

        new_items = self.feeds[self.current_feed].gotFeed(feed)

        print("[SimpleRSS] feed parsed...")

        # Append new items to locally bound ones
        if new_items is not None:
            self.newItemFeed.history.extend(new_items)

        # Start Timer so we can either fetch next feed or show new_items
        self.next_feed()

    def singlePoll(self, id, callback=False, errorback=None):
        getPage(self.feeds[id].uri).addCallback(
            self._gotPage, id, callback, errorback).addErrback(errorback)

    def poll(self):
        # Reloading, reschedule
        if self.reloading:
            print("[SimpleRSS] timer triggered while reloading, rescheduling")
            self.poll_timer.start(10000, 1)
        # End of List
        elif len(self.feeds) <= self.current_feed:
            # New Items
            if self.newItemFeed.history:
                print("[SimpleRSS] got new items, calling back")
                self.doCallback()

                # Inform User
                update_notification_value = config.plugins.simpleRSS.update_notification.value
                if update_notification_value == "preview":
                    from RSSScreens import RSSFeedView

                    from Tools.Notifications import AddNotificationWithID, RemovePopup

                    RemovePopup(NOTIFICATIONID)

                    AddNotificationWithID(NOTIFICATIONID,
                                          RSSFeedView,
                                          self.newItemFeed,
                                          newItems=True)
                elif update_notification_value == "notification":
                    AddPopup(
                        _("Received %d new news item(s).") %
                        (len(self.newItemFeed.history)), MessageBox.TYPE_INFO,
                        5, NOTIFICATIONID)
                elif update_notification_value == "ticker":
                    from RSSTickerView import tickerView
                    if not tickerView:
                        print(
                            "[SimpleRSS] missing ticker instance, something with my code is wrong :-/"
                        )
                    else:
                        tickerView.display(self.newItemFeed)
            # No new Items
            else:
                print("[SimpleRSS] no new items")

            self.current_feed = 0
            self.poll_timer.startLongTimer(
                config.plugins.simpleRSS.interval.value * 60)
        # It's updating-time
        else:
            # Assume we're cleaning history if current feed is 0
            clearHistory = self.current_feed == 0
            if config.plugins.simpleRSS.update_notification.value != "none":
                from Tools import Notifications
                if hasattr(Notifications, 'notificationQueue'):
                    notifications = Notifications.notificationQueue.queue
                    current_notifications = Notifications.notificationQueue.current
                    handler = lambda note: (note.fnc, note.screen, note.args,
                                            note.kwargs, note.id)
                    handler_current = lambda note: (note[0].id, )
                else:
                    notifications = Notifications.notifications
                    current_notifications = Notifications.current_notifications
                    handler_current = handler = lambda note: note

                for x in current_notifications:
                    if handler_current(x)[0] == NOTIFICATIONID:
                        print(
                            "[SimpleRSS] timer triggered while preview on screen, rescheduling"
                        )
                        self.poll_timer.start(10000, 1)
                        return

                if clearHistory:
                    for x in notifications:
                        if handler(x)[4] == NOTIFICATIONID:
                            print(
                                "[SimpleRSS] wont wipe history because it was never read"
                            )
                            clearHistory = False
                            break

            if clearHistory:
                del self.newItemFeed.history[:]

            # Feed supposed to autoupdate
            feed = self.feeds[self.current_feed]

            if feed.autoupdate:
                getPage(feed.uri).addCallback(self._gotPage).addErrback(
                    self.error)
            # Go to next feed
            else:
                print("[SimpleRSS] passing feed")
                self.next_feed()

    def next_feed(self):
        self.current_feed += 1
        self.poll_timer.start(1000, 1)

    def shutdown(self):
        self.poll_timer_conn = None
        self.poll_timer = None
        self.do_poll = False

    def triggerReload(self):
        self.reloading = True

        newfeeds = []
        oldfeeds = self.feeds
        found = False
        for x in config.plugins.simpleRSS.feed:
            for feed in oldfeeds:
                if x.uri.value == feed.uri:
                    # Update possibly different autoupdate value
                    feed.autoupdate = x.autoupdate.value
                    newfeeds.append(feed)  # Append to new Feeds
                    oldfeeds.remove(feed)  # Remove from old Feeds
                    found = True
                    break
            if not found:
                newfeeds.append(UniversalFeed(x.uri.value, x.autoupdate.value))
            found = False

        self.feeds = newfeeds

        if config.plugins.simpleRSS.enable_google_reader.value:
            self.googleReader = GoogleReader(
                config.plugins.simpleRSS.google_username.value,
                config.plugins.simpleRSS.google_password.value)
            self.googleReader.login().addCallback(
                self.googleLoggedIn).addErrback(self.googleLoginFailed)
        else:
            self.reloading = False
Пример #13
0
 def __init__(self, login, passwd):
     self._googlereader = GoogleReader()
     self._googlereader.identify(login=login, passwd=passwd)
     if not (self._googlereader.login()):
         raise "Can't login"
     self._database = ReaderDatabase('reader.sqlite3')
Пример #14
0
class GoogleBase(object):
    _SORT_PREFIXE = 'sort:'

    def __init__(self, config):
        self._config = config
        self._filter = self._config['filter']
        self.__googlereader = None

    def _get_googlereader(self):
        if self.__googlereader is None:
            login_info = {
                'login': self._config['login'],
                'passwd': self._config['passwd']
            }
            http_proxy = None

            proxy_host = self._config['proxy_host']
            if (proxy_host is not None) and (proxy_host != ''):
                http_proxy = (proxy_host, "%s" % self._config['proxy_port'])

            self.__googlereader = GoogleReader(
                agent='pyrfeed-framework-contact:pyrfeed_at_gmail/%s' %
                pyrfeed_version,
                http_proxy=http_proxy)
            self.__googlereader.identify(**login_info)
            if not (self.__googlereader.login()):
                raise Exception("Can't login")
        return self.__googlereader

    def get_shortname(self, longname):
        if longname.startswith(CONST.ATOM_PREFIXE_LABEL):
            shortname = 'label:' + longname[len(CONST.ATOM_PREFIXE_LABEL):]
        elif longname.startswith(CONST.ATOM_PREFIXE_STATE_GOOGLE):
            shortname = 'state:' + longname[len(CONST.ATOM_PREFIXE_STATE_GOOGLE
                                                ):]
        elif longname.startswith(CONST.ATOM_GET_FEED):
            shortname = 'feed:' + longname[len(CONST.ATOM_GET_FEED):]
        else:
            shortname = longname
        return shortname

    def get_longname(self, shortname):
        if shortname.startswith('label:'):
            longname = CONST.ATOM_PREFIXE_LABEL + shortname[len('label:'):]
        elif shortname.startswith('state:'):
            longname = CONST.ATOM_PREFIXE_STATE_GOOGLE + shortname[len('state:'
                                                                       ):]
        elif longname.startswith('feed:'):
            shortname = CONST.ATOM_GET_FEED + longname[len('feed:'):]
        else:
            longname = shortname
        return longname

    def get_filter(self):
        return self._filter

    def set_filter(self, filter_command):
        self._filter = filter_command

    def _format_content(self, content, title, date):
        fdate = time.strftime(self._config['datefmt'], time.gmtime(date))
        return "<h1>%s</h1><p align='right'><font size='-1'>%s</font></p><br align='left'>%s" % (
            title, fdate, content)

    def get_feed_args_default(self):
        """Get the default arguments for get_feed, based on current configuration"""

        get_feed_args = {}
        max_count = self._config['google/max_count']

        if not (self._config['google/include_read']):
            get_feed_args['exclude_target'] = CONST.ATOM_STATE_READ
        if self._config['url']:
            get_feed_args['url'] = self._config['url']
        if self._config['label']:
            get_feed_args[
                'feed'] = CONST.ATOM_PREFIXE_LABEL + self._config['label']
        elif self._config['feed']:
            get_feed_args['feed'] = self._config['feed']

        get_feed_args['count'] = max_count

        return get_feed_args
Пример #15
0
class Reader(object):

  def __init__(self, email, password):
    self.reader = GoogleReader()
    self.reader.identify(email, password)
    if not self.reader.login():
      raise Exception(u'Failed to login to Google Reader')
    self.clear_cache()

  def clear_cache(self):
    self.cache = {}

  @cache('subscriptions')
  def get_subscriptions(self):
    subscriptions = {}
    for x in self.reader.get_subscription_list()['subscriptions']:
      subscriptions[x['id']] = x
    return subscriptions

  @cache('feed_title')
  def get_feed_title(self):
    return self.get_unread_feed().get_title()

  @cache('unread_feed')
  def get_unread_feed(self):
    return self.reader.get_feed(count=CONF.unread.max_count,
                                exclude_target=CONST.ATOM_STATE_READ)

  @cache('unread_entries')
  def get_unread_entries(self):
    subscriptions = self.get_subscriptions()
    entries = []
    for entry in self.get_unread_feed().get_entries():
      if self.ad_filter(entry): continue
      id = entry['sources'].keys()[0]
      entry['subscription_id'] = id
      entry['subscription_title'] = subscriptions[id]['title']
      entry['pinned'] = False
      # XXX
      cat = entry['categories']
      entry['starred'] = cat.has_key(CONST.ATOM_STATE_STARRED)
      # FIXME fresh? or unread?
      entry['unread'] = cat.has_key(CONST.ATOM_STATE_FRESH)
      entries.append(entry)
    ChildThread(self.decode, entries).start()
    return entries

  @cache('unread_counts')
  def get_unread_counts(self):
    counts = {}
    for x in self.reader.get_unread_count_list()['unreadcounts']:
      counts[x['id']] = x['count']
    return counts

  @cache('unread_count')
  def get_unread_count(self):
    for k,v in self.get_unread_counts().iteritems():
      if k.endswith('/state/com.google/reading-list'): return v
    return 0

  def get_pinned_count(self):
    return len(self.get_pinned_entries())

  @cache('starred_count')
  def get_starred_count(self):
    return len(filter(lambda x: x['starred'], self.get_unread_entries()))

  @cache('pinned_entries')
  def get_pinned_entries(self):
    return []

  def toggle_pin(self, entry):
    entry['pinned'] = not entry['pinned']
    if entry['pinned']: self.get_pinned_entries().append(entry)
    else:               self.get_pinned_entries().remove(entry)

  def toggle_star(self, entry):
    # XXX
    if entry['starred']:
      ChildThread(self.reader.del_star, entry['google_id']).start()
      self.cache['starred_count'] -= 1
    else:
      ChildThread(self.reader.add_star, entry['google_id']).start()
      self.cache['starred_count'] += 1
    entry['starred'] = not entry['starred']

  def set_read(self, entry):
    if not entry['unread']: return
    ChildThread(self.reader.set_read, entry['google_id']).start()
    entry['unread'] = False
    self.cache['unread_count'] -= 1

  def set_unread(self, entry):
    if entry['unread']: return
    ChildThread(self.reader.set_unread, entry['google_id']).start()
    entry['unread'] = True
    self.cache['unread_count'] += 1

  def toggle_read(self, entry):
     if entry['unread']: self.set_read(entry)
     else:               self.set_unread(entry)

  def ad_filter(self, entry):
    if not CONF.ad_filter.enable: return False
    if CONF.ad_filter.pattern.match(entry['title']):
      Thread(target=self.reader.set_read, args=(entry['google_id'],)).run()
      return True
    return False

  def decode(self, entries):
    for entry in entries:
      cmd = 'w3m -dump -T text/html'
      proc = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE)
      proc.stdin.write(entry['content'].encode('utf-8'))
      proc.stdin.close()
      entry['content'] = proc.stdout.readlines()
Пример #16
0
def get_reader():
    reader = GoogleReader()
    reader.identify('*****@*****.**', '02515633tzf')
    reader.login()
    return reader
Пример #17
0
 def __init__(self, email, password):
   self.reader = GoogleReader()
   self.reader.identify(email, password)
   if not self.reader.login():
     raise Exception(u'Failed to login to Google Reader')
   self.clear_cache()
Пример #18
0
def main():
    """
    fetch_rss.py <url>
    --no-image
    --no-mark
    """
    arg_list = ["--no-image", "--no-mark", "--debug-on", "-h"]
    no_image = False
    no_mark = False
    debug_on = False
    if len(sys.argv) >= 2:
        for arg in sys.argv[1:]:
            if arg not in arg_list:
                print "Error: Unsupported argument", arg
                return
            if arg == "--no-image":
                no_image = True
            if arg == "--no-mark":
                no_mark = True
            if arg == "--debug-on":
                debug_on = True
                no_image = True
                no_mark = True
            if arg == "-h":
                print "Usage: fetch_rss.py [--no-image] [--no-mark] [-h] url"
                print "Options:"
                print "    -h: show this help message"
                print "    --no-image: fetch rss without images"
                print "    --no-mark: not mark all as read after fetching"
                print "    --debug-on: fetch rss from a test label in google reader, e.g. Linux"
                return


    pretty_print("Fetch RSS from Google Reader")

    # fetch article
    username = base64.b64decode("your encrypted username")
    password = base64.b64decode("your encrypted password")
    if debug_on:
        feed_list_url = ("https://www.google.com/reader"
                         "/public/subscriptions/user/-/label/Linux")
        gr = GoogleReader(username=username, password=password,feed_list_url=feed_list_url)
    else:
        gr = GoogleReader(username=username, password=password)

    print "> Counting Articles..."

    article_amount = gr.get_amount()
    if not article_amount:
        print "> No new items."
        return

    input_char = ""
    while input_char.lower() not in ["y", "n"]:
        input_char = raw_input("    Fetch all %s articles? (y or n)" % article_amount)
        if input_char == "y":
            break
        elif input_char == "n":
            pretty_print("Abort to fetching articles")
            # os.rmdir(project_folder)
            return

    base_folder = "/home/ryan/local/scripts/kindle/pub"
    timestamp = time.strftime("rss_%Y-%m-%d_%H-%M")
    folder_name = "%s_%s" % (timestamp, gr.feed_list_url.rpartition("/")[2])
    global project_folder
    project_folder = os.path.join(base_folder, folder_name)
    os.mkdir(project_folder)
    project_title = "news.rss"

    gr.fetch_feeds()

    kf = kindle_format.Periodical(file_folder=project_folder, title=project_title)
    article_count = 0
    for feed_count, feed in enumerate(gr.unread_feeds):
        kf.append_section(feed.title)
        for feed_article_count, article in enumerate(feed.articles):
            left = article_amount - article_count - feed_article_count
            title = "%s&lt; %s" % (left, article.title)
            if article.desc:
                content = "".join(article.desc)
            else:
                content = ""
            kf.append_item(title, content)
        article_count += len(feed.articles)

    if no_image:
        kf.output(fetch_image=False)
    else:
        kf.output()

    if not no_mark:
        gr.mark_all_as_read()

    pretty_print("Success")
Пример #19
0
class RSSPoller:
	"""Keeps all Feed and takes care of (automatic) updates"""

	def __init__(self, poll = True):
		# Timer
		self.poll_timer = eTimer()
		self.poll_timer_conn = self.poll_timer.timeout.connect(self.poll)
		self.do_poll = poll

		# this indicates we're reloading the list of feeds
		self.reloading = False

		self.newItemFeed = BaseFeed(
			"",
			_("New Items"),
			_("New Items since last Auto-Update"),
		)

		# Generate Feeds
		self.feeds = [
			UniversalFeed(
				x.uri.value,
				x.autoupdate.value
			)
				for x in config.plugins.simpleRSS.feed
		]

		if not config.plugins.simpleRSS.enable_google_reader.value:
			if poll:
				self.poll_timer.start(0, 1)
		else:
			self.googleReader = GoogleReader(config.plugins.simpleRSS.google_username.value, config.plugins.simpleRSS.google_password.value)
			self.googleReader.login().addCallback(self.googleLoggedIn).addErrback(self.googleLoginFailed)

		# Initialize Vars
		self.current_feed = 0

	def googleLoggedIn(self, sid = None):
		self.googleReader.getSubscriptionList().addCallback(self.googleSubscriptionList).addErrback(self.googleSubscriptionFailed)

	def googleLoginFailed(self, res = None):
		AddPopup(
			_("Failed to login to Google Reader."),
			MessageBox.TYPE_ERROR,
			5,
		)

		self.reloading = False
		if self.do_poll:
			self.poll_timer.start(0, 1)

	def googleSubscriptionList(self, subscriptions = None):
		self.feeds.extend(subscriptions)

		self.reloading = False
		if self.do_poll:
			self.doCallback()
			self.poll_timer.start(0, 1)

	def googleSubscriptionFailed(self, res = None):
		AddPopup(
			_("Failed to get subscriptions from Google Reader."),
			MessageBox.TYPE_ERROR,
			5,
		)

		self.reloading = False
		if self.do_poll:
			self.poll_timer.start(0, 1)

	def addCallback(self, callback):
		if callback not in update_callbacks:
			update_callbacks.append(callback)

	def removeCallback(self, callback):
		if callback in update_callbacks:
			update_callbacks.remove(callback)

	def doCallback(self, id = None):
		for callback in update_callbacks:
			try:
				callback(id)
			except Exception:
				pass

	def error(self, error = ""):
		print("[SimpleRSS] failed to fetch feed:", error)

		# Assume its just a temporary failure and jump over to next feed
		self.next_feed()

	def _gotPage(self, data, id = None, callback = False, errorback = None):
		# workaround: exceptions in gotPage-callback were ignored
		try:
			self.gotPage(data, id)
			if callback:
				self.doCallback(id)
		except NotImplementedError as errmsg:
			# Don't show this error when updating in background
			if id is not None:
				AddPopup(
					_("Sorry, this type of feed is unsupported:\n%s") % (str(errmsg)),
					MessageBox.TYPE_INFO,
					5,
				)
			else:
				# We don't want to stop updating just because one feed is broken
				self.next_feed()
		except Exception:
			import traceback, sys
			traceback.print_exc(file=sys.stdout)
			# Errorback given, call it (asumme we don't need do restart timer!)
			if errorback is not None:
				errorback()
				return
			# Assume its just a temporary failure and jump over to next feed
			self.next_feed()

	def gotPage(self, data, id = None):
		feed = cElementTree_fromstring(data)

		# For Single-Polling
		if id is not None:
			self.feeds[id].gotFeed(feed)
			print("[SimpleRSS] single feed parsed...")
			return

		new_items = self.feeds[self.current_feed].gotFeed(feed)

		print("[SimpleRSS] feed parsed...")

		# Append new items to locally bound ones
		if new_items is not None:
			self.newItemFeed.history.extend(new_items)

		# Start Timer so we can either fetch next feed or show new_items
		self.next_feed()

	def singlePoll(self, id, callback = False, errorback = None):
		getPage(self.feeds[id].uri).addCallback(self._gotPage, id, callback, errorback).addErrback(errorback)

	def poll(self):
		# Reloading, reschedule
		if self.reloading:
			print("[SimpleRSS] timer triggered while reloading, rescheduling")
			self.poll_timer.start(10000, 1)
		# End of List
		elif len(self.feeds) <= self.current_feed:
			# New Items
			if self.newItemFeed.history:
				print("[SimpleRSS] got new items, calling back")
				self.doCallback()

				# Inform User
				update_notification_value = config.plugins.simpleRSS.update_notification.value
				if update_notification_value == "preview":
					from RSSScreens import RSSFeedView

					from Tools.Notifications import AddNotificationWithID, RemovePopup

					RemovePopup(NOTIFICATIONID)

					AddNotificationWithID(
						NOTIFICATIONID,
						RSSFeedView,
						self.newItemFeed,
						newItems = True
					)
				elif update_notification_value == "notification":
					AddPopup(
						_("Received %d new news item(s).") % (len(self.newItemFeed.history)),
						MessageBox.TYPE_INFO,
						5,
						NOTIFICATIONID
					)
				elif update_notification_value == "ticker":
					from RSSTickerView import tickerView
					if not tickerView:
						print("[SimpleRSS] missing ticker instance, something with my code is wrong :-/")
					else:
						tickerView.display(self.newItemFeed)
			# No new Items
			else:
				print("[SimpleRSS] no new items")

			self.current_feed = 0
			self.poll_timer.startLongTimer(config.plugins.simpleRSS.interval.value*60)
		# It's updating-time
		else:
			# Assume we're cleaning history if current feed is 0
			clearHistory = self.current_feed == 0
			if config.plugins.simpleRSS.update_notification.value != "none":
				from Tools import Notifications
				if hasattr(Notifications, 'notificationQueue'):
					notifications = Notifications.notificationQueue.queue
					current_notifications = Notifications.notificationQueue.current
					handler = lambda note: (note.fnc, note.screen, note.args, note.kwargs, note.id)
					handler_current = lambda note: (note[0].id,)
				else:
					notifications = Notifications.notifications
					current_notifications = Notifications.current_notifications
					handler_current = handler = lambda note: note

				for x in current_notifications:
					if handler_current(x)[0] == NOTIFICATIONID:
						print("[SimpleRSS] timer triggered while preview on screen, rescheduling")
						self.poll_timer.start(10000, 1)
						return

				if clearHistory:
					for x in notifications:
						if handler(x)[4] == NOTIFICATIONID:
							print("[SimpleRSS] wont wipe history because it was never read")
							clearHistory = False
							break

			if clearHistory:
				del self.newItemFeed.history[:]

			# Feed supposed to autoupdate
			feed = self.feeds[self.current_feed]

			if feed.autoupdate:
				getPage(feed.uri).addCallback(self._gotPage).addErrback(self.error)
			# Go to next feed
			else:
				print("[SimpleRSS] passing feed")
				self.next_feed()

	def next_feed(self):
		self.current_feed += 1
		self.poll_timer.start(1000, 1)

	def shutdown(self):
		self.poll_timer_conn = None
		self.poll_timer = None
		self.do_poll = False

	def triggerReload(self):
		self.reloading = True

		newfeeds = []
		oldfeeds = self.feeds
		found = False
		for x in config.plugins.simpleRSS.feed:
			for feed in oldfeeds:
				if x.uri.value == feed.uri:
					# Update possibly different autoupdate value
					feed.autoupdate = x.autoupdate.value
					newfeeds.append(feed) # Append to new Feeds
					oldfeeds.remove(feed) # Remove from old Feeds
					found = True
					break
			if not found:
				newfeeds.append(
					UniversalFeed(
						x.uri.value,
						x.autoupdate.value
				))
			found = False

		self.feeds = newfeeds

		if config.plugins.simpleRSS.enable_google_reader.value:
			self.googleReader = GoogleReader(config.plugins.simpleRSS.google_username.value, config.plugins.simpleRSS.google_password.value)
			self.googleReader.login().addCallback(self.googleLoggedIn).addErrback(self.googleLoginFailed)
		else:
			self.reloading = False