#!/usr/bin/env python3
# coding=UTF-8
#
# Copyright (c) 2015 ChangZhuo Chen (陳昌倬) <czchen@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import asyncio
import argparse
import logging
import os

import aiohttp


def get_args():
    parser = argparse.ArgumentParser(
        description='KHInsider Downloader'
    )

    parser.add_argument('url',
                        metavar='url',
                        type=str,
                        nargs='+',
                        help='URL to download')

    parser.add_argument('--output', '-o',
                        metavar='output',
                        default=['output'],
                        type=str,
                        nargs=1,
                        help='Output directory')

    return parser.parse_args()


@asyncio.coroutine
def download_single_album(*, loop, output_dir, album_url):
    album_name = album_url[album_url.rfind('/') + 1:]
    album_dir = os.path.join(output_dir, album_url)

    rsp = yield from aiohttp.request('GET', album_url)

    if rsp.status != 200:
        logging.warning('Cannot GET {album_url}, status is {status}'.format(
            album_url=album_url,
            status=rsp.status))
        return

    os.makedirs(album_dir, exist_ok=True)


@asyncio.coroutine
def run(loop):
    args = get_args()

    output_dir = args.output[0]

    os.makedirs(output_dir, exist_ok=True)
    logging.debug('output_dir is {}'.format(output_dir))

    coroutines = []

    for url in args.url:
        coroutines.append(download_single_album(loop=loop,
                                                output_dir=output_dir,
                                                album_url=url))

    asyncio.gather(*coroutines, loop=loop, return_exceptions=True)


def main():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run(loop))
    loop.close()

    return 0

@@ -1874,16 +1874,29 @@
 m_url):%0A
+    try:%0A    
     albu
@@ -1941,16 +1941,20 @@
 ) + 1:%5D%0A
+    
     albu
@@ -2002,16 +2002,20 @@
 l)%0A%0A    
+  
+  
 rsp = yi
@@ -2054,24 +2054,28 @@
 album_url)%0A%0A
+    
     if rsp.s
@@ -2092,24 +2092,28 @@
 00:%0A        
+    
 logging.warn
@@ -2173,32 +2173,36 @@
 at(%0A            
+    
 album_url=album_
@@ -2218,16 +2218,20 @@
         
+    
 status=r
@@ -2242,16 +2242,172 @@
 tatus))%0A
+            return%0A%0A    except Exception:%0A        logging.exception('Cannot retrive information from %7Balbum_url%7D'.format(%0A            album_url=album_url))%0A
         

Add try cache in download_single_album