fix regexp for find images
This commit is contained in:
parent
df601e3234
commit
a31763c550
|
|
@ -1,7 +1,6 @@
|
||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
from project.settings import SCHEMA_URI, SITE_DOMAIN_URI
|
|
||||||
from utils.methods import get_url_images_in_text, get_image_meta_by_url
|
from utils.methods import get_url_images_in_text, get_image_meta_by_url
|
||||||
from news.models import News
|
from news.models import News
|
||||||
from sorl.thumbnail import get_thumbnail
|
from sorl.thumbnail import get_thumbnail
|
||||||
|
|
@ -30,11 +29,6 @@ class Command(BaseCommand):
|
||||||
def optimize(self, text, max_size, max_quality):
|
def optimize(self, text, max_size, max_quality):
|
||||||
"""optimize news images"""
|
"""optimize news images"""
|
||||||
for image in get_url_images_in_text(text):
|
for image in get_url_images_in_text(text):
|
||||||
if not image.startswith('http'):
|
|
||||||
image = f'{SCHEMA_URI}://{SITE_DOMAIN_URI}{image}'
|
|
||||||
|
|
||||||
self.stdout.write(self.style.SUCCESS(f'{image} {text}'))
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
size, width, height = get_image_meta_by_url(image)
|
size, width, height = get_image_meta_by_url(image)
|
||||||
except IOError as ie:
|
except IOError as ie:
|
||||||
|
|
|
||||||
|
|
@ -207,7 +207,7 @@ def section_name_into_index_name(section_name: str):
|
||||||
|
|
||||||
def get_url_images_in_text(text):
|
def get_url_images_in_text(text):
|
||||||
"""Find images urls in text"""
|
"""Find images urls in text"""
|
||||||
return re.findall(r'[^\"\'=\s]+\.jpe?g|png|gif|svg', text)
|
return re.findall(r'\<img.+src=(?!https?:\/\/)([^\/].+?)[\"|\']>', text)
|
||||||
|
|
||||||
|
|
||||||
def get_image_meta_by_url(url) -> (int, int, int):
|
def get_image_meta_by_url(url) -> (int, int, int):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user