Skip to content

Instantly share code, notes, and snippets.

@hapo31
Created October 23, 2016 18:09
Show Gist options
  • Save hapo31/4495f8ab973ca42e8395104f2a294927 to your computer and use it in GitHub Desktop.
Save hapo31/4495f8ab973ca42e8395104f2a294927 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
consumer_key = "YOUR APP CONSUMER KEY"
consumer_secret = "YOUR APP COUSUMER SECRET"
import tweepy
import sys
import urllib3
def main():
print("tweet url>")
url = input()
id = ""
try:
id = url.split("/")[5]
except:
print("url error.")
return
auth = tweepy.auth.AppAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth)
tweet = api.get_status(id)
ent = tweet.extended_entities["media"][0]["video_info"]["variants"]
media_url = ""
for i in ent:
if "url" in i and i["url"].find("1280x720") > 0 and i["url"].find("mp4") > 0:
media_url = i["url"]
print("find media: %s" % media_url)
break
if media_url:
http = urllib3.PoolManager()
r = http.request("GET", media_url)
if r.status == 200:
filename = media_url.split("/")[-1]
with open(filename, "wb") as f:
f.write(r.data)
print("*** file saved by %s ***" % filename)
else:
print("raised error. status:(%s)" % r.status)
print("done.")
else:
print("not found media...")
if __name__ == '__main__':
sys.exit(int(main() or 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment