<s> import re <EOL> import os <EOL> import requests <EOL> from urllib . parse import urlparse <EOL> from django . core . management . base import BaseCommand <EOL> from django . conf import settings <EOL> from django . core . files import File <EOL> from ... models import Page , Image , page_image_path <EOL> class Command ( BaseCommand ) : <EOL> """<STR_LIT>""" <EOL> def get_success_pages ( self ) : <EOL> return Page . objects . filter ( path__startswith = '<STR_LIT>' ) <EOL> def image_url ( self , path ) : <EOL> """<STR_LIT>""" <EOL> new_url = path . replace ( settings . MEDIA_ROOT , settings . MEDIA_URL ) <EOL> return new_url . replace ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> def fix_image ( self , path , page ) : <EOL> url = '<STR_LIT>' . format ( path ) <EOL> r = requests . get ( url ) <EOL> if r . status_code != <NUM_LIT:200> : <EOL> print ( "<STR_LIT>" . format ( url ) ) <EOL> return <EOL> img = Image ( ) <EOL> img . page = page <EOL> filename = os . path . basename ( urlparse ( url ) . path ) <EOL> output_path = page_image_path ( img , filename ) <EOL> directory = os . path . dirname ( output_path ) <EOL> if not os . path . exists ( directory ) : <EOL> os . makedirs ( directory ) <EOL> with open ( output_path , '<STR_LIT:wb>' ) as f : <EOL> f . write ( r . content ) <EOL> reopen = open ( output_path , '<STR_LIT:rb>' ) <EOL> new_file = File ( reopen ) <EOL> img . image . save ( filename , new_file , save = True ) <EOL> return self . image_url ( output_path ) <EOL> def find_image_paths ( self , page ) : <EOL> content = page . content . raw <EOL> paths = set ( re . findall ( r'<STR_LIT>' , content ) ) <EOL> if paths : <EOL> print ( "<STR_LIT>" . format ( len ( paths ) , page . path ) ) <EOL> return paths <EOL> def process_success_story ( self , page ) : <EOL> """<STR_LIT>""" <EOL> image_paths = self . find_image_paths ( page ) <EOL> for path in image_paths : <EOL> new_url = self . fix_image ( path , page ) <EOL> print ( "<STR_LIT>" . format ( path , new_url ) ) <EOL> content = page . content . raw <EOL> new_content = content . replace ( path , new_url ) <EOL> page . content = new_content <EOL> page . save ( ) <EOL> def handle ( self , * args , ** kwargs ) : <EOL> self . pages = self . get_success_pages ( ) <EOL> print ( "<STR_LIT>" . format ( len ( self . pages ) ) ) <EOL> for p in self . pages : <EOL> self . process_success_story ( p ) </s>
