Skip to content

Instantly share code, notes, and snippets.

@fkztw
Last active October 24, 2022 22:34
Show Gist options
  • Save fkztw/a90f5d2948442dc482e35d671b074c6f to your computer and use it in GitHub Desktop.
Save fkztw/a90f5d2948442dc482e35d671b074c6f to your computer and use it in GitHub Desktop.
Get tweets I saved (by sending DM to my bot account)
#!/usr/bin/env python3
import urllib
import twitter
# Fill these contants by yourself.
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN_KEY = ""
ACCESS_TOKEN_SECRET = ""
MY_TWITTER_ID = 0
MY_BOT_TWITTER_ID = 0
api = twitter.Api(
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
access_token_key=ACCESS_TOKEN_KEY,
access_token_secret=ACCESS_TOKEN_SECRET,
)
# Twitter API limitation:
# "Last 30 days DMs" or "Up to 200 DMs which created before 30 days"
sent_direct_messages = api.GetSentDirectMessages(count=200)
for dm in sent_direct_messages:
if dm.recipient_id == MY_BOT_TWITTER_ID:
try:
# Got t.co url in DM, use urllib to get its real Twitter Status URL.
with urllib.request.urlopen(dm.text) as response:
real_url = response.geturl()
except Exception:
raise
else:
print("+ <{}>".format(real_url))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment