Last active
October 24, 2016 21:37
-
-
Save astrostl/8317584db4f892393996e02cfd5b9433 to your computer and use it in GitHub Desktop.
Twitter Tag Plus Photo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Twitter Tag Plus Photo""" | |
from os import environ | |
from sys import argv | |
from urllib.request import urlretrieve | |
import twitter | |
if len(argv) < 2: | |
exit('Usage: ' + argv[0] + ' hashtag [slackchannel]') | |
elif len(argv) == 3: | |
from slacker import Slacker | |
SLACK = Slacker(environ['SLACK_TOKEN']) | |
CHANNEL, BOT, EMOJI = argv[2], 'Twitter TPP', ':bird:' | |
API = twitter.Api(consumer_key=environ['CONSUMER_KEY'], | |
consumer_secret=environ['CONSUMER_SECRET'], | |
access_token_key=environ['ACCESS_TOKEN_KEY'], | |
access_token_secret=environ['ACCESS_TOKEN_SECRET']) | |
MENTIONS = API.GetMentions(count='200') | |
for mention in MENTIONS: | |
has_tag, has_pic = False, False | |
SN, MID = str(mention.user.screen_name), str(mention.id) | |
TWEET = 'https://twitter.com/' + SN + '/status/' + MID | |
if mention.hashtags: | |
for hashtag in mention.hashtags: | |
if hashtag.text.lower() == argv[1]: | |
has_tag = True | |
if mention.media: | |
for index, pic in enumerate(mention.media): | |
if pic.type == 'photo': | |
has_pic = True | |
if has_tag: | |
FILENAME = SN + '_' + MID + '_' + str(index) + '.jpg' | |
urlretrieve(pic.media_url, FILENAME) | |
if has_tag and has_pic: | |
print(TWEET) | |
if len(argv) == 3: | |
SLACK.chat.post_message(CHANNEL, TWEET, username=BOT, icon_emoji=EMOJI) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment