Skip to content

Commit

Permalink
rewrite: tw: improve twitter rewrite to force mp4 for videos in embed…
Browse files Browse the repository at this point in the history
…ded tweets (#761)
  • Loading branch information
ikreymer authored Sep 1, 2022
1 parent 16135d9 commit 8ef4ff1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pywb/rewrite/rewrite_dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,25 @@ def rewrite_tw_dash(string, *args):
try:
best_variant = None
best_bitrate = 0
best_src = ""
max_bitrate = 5000000

data = json.loads(string)
for variant in data["variants"]:
if variant["content_type"] != "video/mp4":
if (("content_type" in variant and variant["content_type"] != "video/mp4") or
("type" in variant and variant["type"] != "video/mp4")):
continue

bitrate = variant.get("bitrate")
src = variant.get("src")

if bitrate and bitrate > best_bitrate and bitrate <= max_bitrate:
best_variant = variant
best_bitrate = bitrate
# just compare src strings with dimensions
elif src and src > best_src:
best_variant = variant
best_src = src

if best_variant:
data["variants"] = [best_variant]
Expand Down
9 changes: 9 additions & 0 deletions pywb/rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ rules:

# twitter rules
#=================================================================

- url_prefix: 'com,twitter)/i/profiles/show/'

fuzzy_lookup: '/profiles/show/.*with_replies\?.*(max_id=[^&]+)'
Expand All @@ -84,6 +85,14 @@ rules:
function: 'pywb.rewrite.rewrite_dash:rewrite_tw_dash'


- url_prefix: ['com,twimg,syndication,cdn)/tweet-result']

rewrite:
js_regexs:
- match: 'video":(.*?viewCount":\d+})'
group: 1
function: 'pywb.rewrite.rewrite_dash:rewrite_tw_dash'



# facebook rules
Expand Down

0 comments on commit 8ef4ff1

Please sign in to comment.