Skip to content

Commit

Permalink
Create sync_s3_images.py
Browse files Browse the repository at this point in the history
  • Loading branch information
bobby-didcoding authored Feb 24, 2025
1 parent 85648e4 commit 9b29cf7
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions core/management/commands/sync_s3_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.core.management import BaseCommand

import json
from django.conf import settings
import requests
from bs4 import BeautifulSoup


class Command(BaseCommand):
help = 'Used to scrape a give url for all images and dump to json'

def add_arguments(self, parser):
parser.add_argument('url_to_scrape', type=str, help='Please add a valid url')

def handle(self, *args, **options):

def make_json(json_file_path):
data = {}

with open(json_file_path, 'w', encoding='utf-8') as jsonf:
url = options['url_to_scrape']
html_page = requests.get(url)
soup = BeautifulSoup(html_page.content, 'html.parser')
images = soup.find_all('img')
data['images'] = [img.get('src') for img in images]
jsonf.write(json.dumps(data, indent=4))

json_file_path = settings.ROOT_DIR / 's3_images.json'

make_json(json_file_path)

self.stdout.write(self.style.SUCCESS('All done, bye!'))

0 comments on commit 9b29cf7

Please sign in to comment.