from bs4 import BeautifulSoup
import urllib
import urllib.request
import re
import csv
import os
import sys


def get_song_info(sid):
    url = "http://17sing.tw/song/index.php?sid=" + str(sid)
    try:
        req = urllib.request.urlopen(url)
        text = req.read()
        soup = BeautifulSoup(text, 'html5lib')
        song_name = str(soup.find("title").contents[0]) + ".mp3"
        like_num = str(soup.find("span", "icon-like").contents[0])
        review = str(soup.find("span", "icon-music").contents[0])

        pattern = re.compile('http[0-9,a-z,A-Z,.,:,/,_,\-,!,@,#,$,%,^,&,*,(,),+,=,]*mp3')
        uid = str(soup.find("p", "sd-uid").contents[0])
    except AttributeError:
        print("sid: " + str(sid) + " no data")
        return None
    except IndexError as e:
        print(e)
        return None
    except Exception as e:
        print(e)
        return None

    buff = str(soup.text)
    try:
        song_src = pattern.findall(buff)[0]
    except IndexError:
        return None
    return sid, uid, song_name, like_num, song_src, review


def save_to_csv(sid_min, sid_max, check_point=10000):
    file = None

    if os.path.exists("./data") is not True:
        os.mkdir("./data")

    for i in range(sid_min, sid_max):

        # if detect check point, open a new file

        if i % check_point is 0:
            if file is not None:
                file.close()
            file = open('./data/%07d-%07d.csv' % (i, i + check_point), 'w', encoding='utf-8')
            cursor = csv.writer(file)
            print("Now crawling %d " % i)
        elif file is None:
            file = open('./data/%07d-%07d.csv' % (i, i + check_point), 'w', encoding='utf-8')
            cursor = csv.writer(file)

        # request info
        info = get_song_info(i)

        # if request success, swrite to csv cursor
        if info is not None:
            print("sid:%d" % i)
            cursor.writerow(info)

    file.close()


if __name__ == "__main__":

    # TODO: if min % checkpoint != 0 will cause a error

    if len(sys.argv) < 3:
        print("Usage:", sys.argv[0], "min_sid max_sid")
        sys.exit(1)

    min = int(sys.argv[1])
    max = int(sys.argv[2])

    print("from sid = %d to %d:" % (min, max))
    save_to_csv(min, max)
