def update_tweets(tweet_list, tweet_entities=tweet_html_entities, download=False): # Need to escape HTML entities htmlparser = html_parser.HTMLParser() unescape = htmlparser.unescape obj_list = [] for tweet in tweet_list: tweet_id = tweet['id'] tweet_username = tweet['user']['screen_name'] tweet_name = tweet['user']['name'] tweet_created = datetime.strptime( tweet['created_at'], '%a %b %d %H:%M:%S +0000 %Y' ).replace(tzinfo=utc) tweet_is_reply = tweet['in_reply_to_screen_name'] is not None if 'retweeted_status' in tweet: retweeted_username = tweet['retweeted_status']['user']['screen_name'] retweeted_name = tweet['retweeted_status']['user']['name'] retweeted_tweet_id = tweet['retweeted_status']['id'] tweet_text = tweet['retweeted_status']['text'] tweet_html = tweet_entities(tweet_text, **tweet['retweeted_status']['entities']) favorite_count = tweet['retweeted_status']['favorite_count'] retweet_count = tweet['retweeted_status']['retweet_count'] else: retweeted_username = '' retweeted_name = '' retweeted_tweet_id = None tweet_text = tweet['text'] tweet_html = tweet_entities(tweet_text, **tweet['entities']) favorite_count = tweet['favorite_count'] retweet_count = tweet['retweet_count'] tweet_text = unescape(tweet_text) obj, created = Tweet.objects.get_or_create(tweet_id=tweet_id, defaults={ 'user': tweet_username, 'name': tweet_name, 'text': tweet_text, 'html': tweet_html, 'favorite_count': favorite_count, 'retweet_count': retweet_count, 'retweeted_username': retweeted_username, 'retweeted_name': retweeted_name, 'retweeted_tweet_id': retweeted_tweet_id, 'is_reply': tweet_is_reply, 'created': tweet_created, }) if created: # Add hashtags tweet_hashtags(tweet=obj, hashtags=tweet['entities'].get('hashtags', [])) # Add any photos tweet_photos(tweet=obj, media=tweet['entities'].get('media', []), download=download) else: # Update counts, but try to avoid excessive updates update_fields = [] if obj.favorite_count != favorite_count: obj.favorite_count = favorite_count update_fields.append('favorite_count') if obj.retweet_count != retweet_count: obj.retweet_count = retweet_count update_fields.append('retweet_count') if update_fields: obj.save(update_fields=update_fields) obj_list.append(obj) return obj_list
from django.template import defaultfilters from django.template.loader import get_template from django.utils.encoding import force_text from django.utils.html import strip_tags from django.utils.six.moves import html_parser from django.utils.translation import ugettext_lazy as _ from django_jinja import library from pytz import timezone, utc from soapbox.models import Message from statici18n.templatetags.statici18n import statici18n from urlobject import URLObject from ..urlresolvers import reverse, split_path from ..utils import format_date_time, order_params, urlparams htmlparser = html_parser.HTMLParser() # Yanking filters from Django. library.filter(defaultfilters.escapejs) library.filter(defaultfilters.linebreaksbr) library.filter(strip_tags) library.filter(defaultfilters.truncatewords) library.filter(urlparams) library.global_function(statici18n) @library.filter def paginator(pager): """Render list of pages.""" return Paginator(pager).render()