Browse Source

first working version

master
mark 4 months ago
parent
commit
7ba3ea3a7a
3 changed files with 76 additions and 1 deletions
  1. +4
    -1
      .gitignore
  2. +8
    -0
      config.example.json
  3. +64
    -0
      main.py

+ 4
- 1
.gitignore View File

@@ -1 +1,4 @@
tmw_venv
config.json
envs
*.code-workspace
.vscode

+ 8
- 0
config.example.json View File

@@ -0,0 +1,8 @@
{
"consumer_key": "KEY",
"consumer_secret": "SECRET",
"access_token": "TOKEN",
"access_secret": "SECRET",
"burn_after_reading": true,
"output_dir": "./output"
}

+ 64
- 0
main.py View File

@@ -0,0 +1,64 @@
import tweepy
import requests

import json
import shutil
import pathlib
from os import path

config = json.load(open("config.json"))

pathlib.Path(config["output_dir"]).mkdir(parents=True, exist_ok=True)

auth = tweepy.OAuthHandler(config["consumer_key"], config["consumer_secret"])
auth.set_access_token(config["access_token"], config["access_secret"])
api = tweepy.API(auth_handler=auth)


class MediaStreamListener(tweepy.StreamListener):

def on_status(self, status):
print(status.text)
print(status.extended_entities["media"])

media_entities = status.extended_entities["media"]
photo_urls = [
item["media_url"]
for item in media_entities
if "ext_tw_video_thumb" not in item["media_url"] and
item["type"] == "photo"
]

video_entities = [
item for item in media_entities if item["type"] == "video"
]

video_urls = []

for video in video_entities:
variants = video["video_info"]["variants"]
best_bitrate = 0
for variant in variants:
try:
if variant["bitrate"] > best_bitrate:
best_bitrate = variant["bitrate"]
best_url = variant["url"]
except KeyError:
pass
video_urls.append(best_url)

for url in photo_urls + video_urls:
fname = url.split("/")[-1].split("?")[0]
r = requests.get(url, stream=True)
print(r.status_code)
with open(path.join(config["output_dir"], fname), 'wb') as outfile:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, outfile)

if config["burn_after_reading"]:
api.destroy_status(status.id)


listener = MediaStreamListener()
stream = tweepy.Stream(auth=auth, listener=listener)
stream.filter(follow=[str(api.me().id)])

Loading…
Cancel
Save