本文整理汇总了Python中models.Photo.tweet方法的典型用法代码示例。如果您正苦于以下问题:Python Photo.tweet方法的具体用法?Python Photo.tweet怎么用?Python Photo.tweet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Photo
的用法示例。
在下文中一共展示了Photo.tweet方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fetch_hashtag
# 需要导入模块: from models import Photo [as 别名]
# 或者: from models.Photo import tweet [as 别名]
def fetch_hashtag(self, **kwargs):
try:
# Connect to Twitter Search API
twitter = Twython(settings.TWITTER_CONSUMER_KEY, access_token=settings.TWITTER_ACCESS_TOKEN)
# Last fetching
album = Album.objects.get(pk=kwargs['pk'])
hashtag = '%23' + album.title
print hashtag
if album.last_fetching:
last_fetching = album.last_fetching.strftime('%a %b %d %H:%M:%S %z %Y')
since = album.last_fetching.strftime('%Y-%m-%d')
else:
last_fetching = datetime.datetime.today().strftime('%a %b %d %H:%M:%S %z %Y')
since = datetime.datetime.today().strftime('%Y-%m-%d')
'''
Retrieve most recent 100 photos from Twitter
'''
result = twitter.search(q=hashtag, filter='images', since=since, count='100', result_type='recent')
photos = []
check_exists = []
new_items = 0
for i in range(len(result['statuses'])):
created_at = result['statuses'][i]['created_at']
# Create a 'photo' object
photo = Photo()
photo.album_id = album.id
if result['statuses'][i]['entities'].has_key('media'): # and last_fetching < created_at:
tweet = result['statuses'][i]['entities']['media'][0]['expanded_url']
media = result['statuses'][i]['entities']['media'][0]['media_url_https']
'''
We are trying to avoid duplicates for each fetch.
1. Store the media link in check_exists list.
2. If the media link is not in check_exists, assign it to the photo object.
3. Increment the 'new_items' counter.
4. Insert the link into check_exists list.
'''
if media not in check_exists:
photo.media = str(media)
photo.tweet = str(tweet)
photos.append(photo)
new_items += 1
check_exists.append(str(media))
'''
Save all new object using one query (bulk_create).
'''
Photo.objects.bulk_create(photos)
'''
Update the last_fetching field to now.
'''
album.last_fetching = datetime.datetime.now()
album.save()
'''
Send an e-mail to MANAGERS (settings.py)
'''
total_photos = Photo.objects.filter(album_id__exact=album.id).count()
new_photos_links = '\n'.join(map(str, check_exists))
subject = 'album: %s, new pic: %s' % (album.title, new_items)
message = 'Total photos: %s \n\n%s' % (total_photos, new_photos_links)
mail_managers(subject, message)
# Redirect to /list/album/<album-id>/photos
url = '%s%s/photos/' % (reverse('list_album_view'), album.id)
return redirect(url)
except TwythonError as error:
print error