#!/usr/bin/env python

import sys
sys.path.append('/home/leon/projects/notebookWebapp/')

from django.core import management; import notebook; import notebook.settings as settings;management.setup_environ(settings)

from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
from notebook.bookmarks.models import Bookmark

from notebook.notes.views import getT, getW, getNote

from datetime import datetime




def import_only_a(username, bookmark_file, default_vote=0, common_tag=None, common_ws=None):
    """This only get urls, and ignore all the other infos such as folders, desc."""
       
    urls = [(tag['href'], tag.string, tag.get('add_date'), tag.get('last_modified')) for tag in
            BeautifulSoup(bookmark_file).findAll('a')]    
    count_urls_in_file = len(urls)
    print count_urls_in_file, ' urls found in the bookmark file.'
    
    duplicate = []
    count_note_created = 0

    for url in urls:
        n, created = build_one_bookmark(username, url, default_vote)
        if not created:
            duplicate.append((url[0], url[1]))
        else:
            count_note_created+=1    
    
    print  count_note_created, ' bookmarks created'    
    print len(duplicate), ' duplicated bookmarks.'    
    #print  'duplicate is:',duplicate
    duplicate.sort()
    return count_urls_in_file, count_note_created, duplicate


def import_delicious_with_no_desc(username, bookmark_file, default_vote=0, common_tag=None, common_ws=None):
    """This import from bookmark file exported from delicious, which has tags attr inside a tag."""
       
    urls = [(tag['href'], tag.string, tag.get('add_date'), tag.get('last_modified'), tag.get('tags')) for tag in
            BeautifulSoup(bookmark_file).findAll('a')]    
    count_urls_in_file = len(urls)
    print count_urls_in_file, ' urls found in the bookmark file.'
    
    duplicate = []
    count_note_created = 0
    count_tag_created = 0

    for url in urls:
        n, created = build_one_bookmark(username, url, default_vote)
        num_of_tags_created = n.add_tags(url[4]+','+common_tag)        
        count_tag_created = count_tag_created + num_of_tags_created
        W = getW(username)
        w, created = W.objects.get_or_create(name=common_ws)
        w.add_tags(url[4]+','+common_tag)
        w.save()
        if not created:
            duplicate.append((url[0], url[1]))
        else:
            count_note_created+=1    
    
    print  count_note_created, ' bookmarks created'    
    print len(duplicate), ' duplicated bookmarks.'    
    #print  'duplicate is:',duplicate
    duplicate.sort()
    return count_urls_in_file, count_note_created, duplicate, count_tag_created


def import_delicious(username, bookmark_file, default_vote=0, common_tag=None, common_ws=None):
    """This import from bookmark file exported from delicious, which has tags attr inside a tag."""
       
    urls = [(tag['href'], tag.string, tag.get('add_date'), tag.get('last_modified'), tag.get('tags')) for tag in
            BeautifulSoup(bookmark_file).findAll('a')]    
    count_urls_in_file = len(urls)
    print count_urls_in_file, ' urls found in the bookmark file.'
    
    duplicate = []
    count_note_created = 0
    count_tag_created = 0
    bookmark_file.seek(0)
    n = None
    for line in bookmark_file:
        if line.find('<DT><A') != -1:
            url = [(tag['href'], tag.string, tag.get('add_date'), tag.get('last_modified'), tag.get('tags')) for tag in BeautifulSoup(line).findAll('a')][0]
            n, created = build_one_bookmark(username, url, default_vote)  
            if not created:
                duplicate.append((url[0], url[1]))
            else:
                count_note_created+=1   
            num_of_tags_created = n.add_tags(url[4]+','+common_tag)        
            count_tag_created = count_tag_created + num_of_tags_created
            W = getW(username)
            w, created = W.objects.get_or_create(name=common_ws)
            w.add_tags(url[4]+','+common_tag)
            w.save()
             
        if line.find('<DD>') != -1:
            if n:
                desc = line.strip('<DD>').strip('</DD>')  
                n.desc = desc
                n.save() 
                print 'n.desc:', n.desc
    print  count_note_created, ' bookmarks created'    
    print len(duplicate), ' duplicated bookmarks.'    
    #print  'duplicate is:',duplicate
    duplicate.sort()
    return count_urls_in_file, count_note_created, duplicate, count_tag_created


def build_one_bookmark(username, url, default_vote=0):
    N = getNote(username, 'bookmarkbook')
    
    n, created = N.objects.get_or_create(url = url[0])
    if created:
        if url[1]==None:
            n.title = ""