@@ -0,0 +1,2208 @@
+#!/usr/bin/python%0A# -*- coding: utf-8 -*-%0A%0Aimport os%0A%0A%0AROOT_FOLDER = '/home/mjulian/coding/figarocorso.github.io/'%0APOST_FOLDER = ROOT_FOLDER + '_posts/day-by-day/'%0AIMAGE_FOLDER = ROOT_FOLDER + 'images/blog/'%0A%0A%0Adef get_file_lines(filename):%0A    with open(POST_FOLDER + filename) as f:%0A        return f.readlines()%0A%0A%0Adef get_post_image_urls(lines):%0A    image_urls = %5B%5D%0A    for line in lines:%0A        image_url = get_image_url_or_none(line)%0A        if image_url:%0A            image_urls.append(image_url)%0A%0A    return image_urls%0A%0A%0Adef get_image_url_or_none(line):%0A    # TODO: Use re%0A    try:%0A        image_url_beggining = line.index('%5D(') + 2%0A        image_url_ending = line.index(')')%0A        line.index('!%5B')%0A    except ValueError:%0A        return None%0A%0A    return line%5Bimage_url_beggining:image_url_ending%5D%0A%0A%0Adef url_is_blog_image(url):%0A    return 'blog.migueljulian.com' in url and 'uploads' in url%0A%0A%0Adef get_post_image(image_urls):%0A    for image_url in image_urls:%0A        if url_is_blog_image(image_url):%0A            return image_url%0A    return None%0A%0A%0Adef get_image_name(url):%0A    return url.split('/')%5B-1%5D%0A%0A%0Adef add_image_header(file_lines, image_name):%0A    header = %22image: /images/blog/%25s%5Cn%22 %25 image_name%0A    file_lines.insert(3, header)%0A%0A%0Adef replace_image_url(file_lines, image_name):%0A    needle = 'http://blog.migueljulian.com/wp-content/uploads'%0A    line_count = 0%0A    for line in file_lines:%0A        new_line = ''%0A        if needle in line:%0A            new_line = line.replace(needle, '/images/blog')%0A%0A        if new_line:%0A            file_lines%5Bline_count%5D = new_line%0A%0A        line_count += 1%0A%0A%0Adef write_file(filename, lines):%0A    print filename%0A    with open(POST_FOLDER + filename, 'w') as f:%0A        for line in lines:%0A            f.write(line)%0A%0A%0Aif __name__ == %22__main__%22:%0A    for filename in os.listdir(POST_FOLDER):%0A        file_lines = get_file_lines(filename)%0A        post_images = get_post_image_urls(file_lines)%0A        post_image = get_post_image(post_images)%0A        if post_image:%0A            image_name = get_image_name(post_image)%0A            add_image_header(file_lines, image_name)%0A            replace_image_url(file_lines, image_name)%0A            write_file(filename, file_lines)%0A

Add image url replacement script