Skip to content

Instantly share code, notes, and snippets.

@SophieShears
Created September 2, 2025 21:31
Show Gist options
  • Save SophieShears/35d20292b34e138d070435fe02fe4793 to your computer and use it in GitHub Desktop.
Save SophieShears/35d20292b34e138d070435fe02fe4793 to your computer and use it in GitHub Desktop.
Add MetaAdVid videos to MetaAds
import json
from submissions.models import MetaAd, MetaAdVid
from tools.meta_ads import MetaAds
meta_ads_no_vid = MetaAd.objects.filter(metaad_vids__isnull=True, direct_ad_full_md__isnull=False)
total_added = 0
for meta_ad in meta_ads_no_vid:
ad_json = json.loads(meta_ad.direct_ad_full_md)
try:
content_url = ad_json['snapshot']['cards'][0]['video_sd_url']
except (IndexError, KeyError):
try:
content_url = ad_json['snapshot']['videos'][0]['video_sd_url']
except (IndexError, KeyError):
continue # Can't get ad id
if content_url is None:
continue
video_id = MetaAds().get_video_id_from_content_url(content_url)
vids = MetaAdVid.objects.filter(video_id=video_id)
if not vids.exists():
continue
vid = vids[0]
meta_ad.metaad_vids.add(vid)
total_added += 1
print(f'Added {total_added} videos to ads.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment