def __init__(self, username, query_string): super(FacebookFeedQuery, self).__init__(username, query_string) settings = get_socialfeed_setting('CONFIG').get('facebook', None) if not settings: raise ImproperlyConfigured( "No facebook configuration defined in the settings. " "Make sure you define WAGTAIL_SOCIALFEED_CONFIG in your " "settings with at least a 'facebook' entry.") graph = GraphAPI("{}|{}".format(settings['CLIENT_ID'], settings['CLIENT_SECRET'])) required_fields = get_socialfeed_setting('FACEBOOK_FIELDS') self._paginator = graph.get('{}/posts?fields={}'.format( self.username, ','.join(required_fields)), page=True)
def get_items(self, config, limit=0, query_string=None, use_cache=True): """ Return a list of `FeedItem`s and handle caching. :param config: `SocialFeedConfiguration` to use :param limit: limit the output. Use 0 or None for no limit (default=0) :param query_string: the search term to filter on (default=None) :param use_cache: utilize the cache store/retrieve the results (default=True) """ cls_name = self.__class__.__name__ cache_key = 'socialfeed:{}:data:{}'.format(cls_name, config.id) if query_string: cache_key += ":q-{}".format(query_string) data = cache.get(cache_key, []) if use_cache else None if data: logger.debug("Getting data from cache ({})".format(cache_key)) else: logger.debug("Fetching data online") data_raw = self._fetch_online(config=config, query_string=query_string) data = list(map(self._convert_raw_item, data_raw)) if use_cache: logger.debug("Storing data in cache ({})".format(cache_key)) cache.set(cache_key, data, get_socialfeed_setting('CACHE_DURATION')) if limit: return data[:limit] return data
def _more_history_allowed(self, oldest_date): """ Determine if we should load more history. This is used when searching for specific posts using the `query_string` argument. :param oldest_date: date of the oldest post in the last set returned """ now = datetime.datetime.now(tzutc()) last_allowed = now - get_socialfeed_setting('SEARCH_MAX_HISTORY') return oldest_date > last_allowed
import logging from dateutil import parser as dateparser from django.core.exceptions import ImproperlyConfigured from twython import Twython from wagtailsocialfeed.utils.conf import get_socialfeed_setting from . import AbstractFeed, AbstractFeedQuery, FeedItem logger = logging.getLogger('wagtailsocialfeed') settings = get_socialfeed_setting('CONFIG').get('twitter', None) if not settings: raise ImproperlyConfigured( "No twitter configuration defined in the settings. " "Make sure you define WAGTAIL_SOCIALFEED_CONFIG in your " "settings with at least a 'twitter' entry.") class TwitterFeedItem(FeedItem): @classmethod def get_post_date(cls, raw): # Use the dateutil parser because on some platforms # python's own strptime doesn't support the %z directive. # Format: '%a %b %d %H:%M:%S %z %Y' return dateparser.parse(raw.get('created_at')) @classmethod def from_raw(cls, raw): image = None extended = raw.get('extended_entities', None)