<commit_before>import tempfile
import urllib2
import zipfile
import os
import simplejson
import datetime


from booki.editor import models

from django.template.defaultfilters import slugify


def createProject(projectName):
    url_name = slugify(projectName)
    
    project = models.Project(url_name = url_name[:20],
                             name = projectName,
                             status = 0)
    project.save()

    status_default = ["published", "not published", "imported"]
    n = len(status_default)

    for statusName in status_default:
        status = models.ProjectStatus(project=project, name=statusName, weight=n)
        status.save()
        n -= 1

    return project


def createBook(project, bookTitle):

    url_title = slugify(bookTitle)

    stat = models.ProjectStatus.objects.filter(project=project, name="imported")[0]
    
    book = models.Book(project = project,
                       url_title = url_title[:20],
                       title = bookTitle,
                       status = stat,
                       published = datetime.datetime.now())
    book.save()
    
    return book
    

def importBookFromURL(bookURL):
    # download it
    f = urllib2.urlopen(bookURL)
    data = f.read()

    (zfile, zname) = tempfile.mkstemp()
    os.write(zfile, data)

    # unzip it
    zdirname = tempfile.mkdtemp()
    zf = zipfile.ZipFile(zname)
    zf.extractall(zdirname)
    zf.close()

    # load info.json

    data = open('%s/info.json' % zdirname, 'r').read()
    info = simplejson.loads(data)


    print info

    bookTitle = info['metadata']['title']

    print bookTitle

    project = createProject(bookTitle)
    book = createBook(project, bookTitle)

    print project
    print book

 #   n = len(info['TOC'])
 #   print n

    for tocEntry in info['TOC']:
        print tocEntry
        chapterName = tocEntry[0]
        chapterFile = tocEntry[1]

        urlName = slugify(chapterName)

        stat = models.ProjectStatus.objects.filter(project=project, name="imported")[0]


        print '-----------------------------'
        print '%s/%s' % (zdirname, chapterFile)

        content = open('%s/%s' % (zdirname, chapterFile), 'r').read()

        chapter = models.Chapter(book = book,
                                 url_title = urlName[:20],
                                 title = chapterName,
                                 status = stat,
                                 content = content,
                                 created = datetime.datetime.now(),
                                 modified = datetime.datetime.now())
        chapter.save()
        
#        c = models.BookToc(book = book,
#                           name = chapterName,
#                           chapter = chapter,
#                           weight = n,
#                           typeof = 1)
#        c.save()
#        n -= 1
                                 

    return
<commit_msg>Implemented ZipFile.extract for the python 2.5<commit_after>import tempfile
import urllib2
import zipfile
import os
import simplejson
import datetime


from booki.editor import models

from django.template.defaultfilters import slugify


from os import mkdir
from os.path import split, exists

def path_name(name):
    return split(name)[0]


def file_name(name):
    return split(name)[1]


def path_names(names):
    return [path_name(name) for name in names if path_name(name) != '']


def file_names(names):
    return [name for name in names if file_name(name)]



def extract(zdirname, zipfile):
    
    names = zipfile.namelist()

    print "names ", names

    for name in path_names(names):
        print "[%s]" % name
        if not exists('%s/%s' % (zdirname, name)): 
            print '%s/%s' % (zdirname, name)
            print " moram ga kreirari"
            mkdir('%s/%s' % (zdirname, name))
    

    for name in file_names(names):
        print "--------------"
        print '%s/%s' % (zdirname, name)
        outfile = file('%s/%s' % (zdirname, name), 'wb')
        outfile.write(zipfile.read(name))
        outfile.close()




def createProject(projectName):
    url_name = slugify(projectName)
    
    project = models.Project(url_name = url_name[:20],
                             name = projectName,
                             status = 0)
    project.save()

    status_default = ["published", "not published", "imported"]
    n = len(status_default)

    for statusName in status_default:
        status = models.ProjectStatus(project=project, name=statusName, weight=n)
        status.save()
        n -= 1

    return project


def createBook(project, bookTitle):

    url_title = slugify(bookTitle)

    stat = models.ProjectStatus.objects.filter(project=project, name="imported")[0]
    
    book = models.Book(project = project,
                       url_title = url_title[:20],
                       title = bookTitle,
                       status = stat,
                       published = datetime.datetime.now())
    book.save()
    
    return book
    

def importBookFromURL(bookURL):
    # download it
    f = urllib2.urlopen(bookURL)
    data = f.read()

    (zfile, zname) = tempfile.mkstemp()
    os.write(zfile, data)

    # unzip it
    zdirname = tempfile.mkdtemp()
    zf = zipfile.ZipFile(zname)
    extract(zdirname, zf)
#    zf.extractall(zdirname)
    zf.close()

    # load info.json

    data = open('%s/info.json' % zdirname, 'r').read()
    info = simplejson.loads(data)


    print info

    bookTitle = info['metadata']['title']

    print bookTitle

    project = createProject(bookTitle)
    book = createBook(project, bookTitle)

    print project
    print book

 #   n = len(info['TOC'])
 #   print n

    for tocEntry in info['TOC']:
        print tocEntry
        chapterName = tocEntry[0]
        chapterFile = tocEntry[1]

        urlName = slugify(chapterName)

        stat = models.ProjectStatus.objects.filter(project=project, name="imported")[0]


        print '-----------------------------'
        print '%s/%s' % (zdirname, chapterFile)

        content = open('%s/%s' % (zdirname, chapterFile), 'r').read()

        chapter = models.Chapter(book = book,
                                 url_title = urlName[:20],
                                 title = chapterName,
                                 status = stat,
                                 content = content,
                                 created = datetime.datetime.now(),
                                 modified = datetime.datetime.now())
        chapter.save()
        
#        c = models.BookToc(book = book,
#                           name = chapterName,
#                           chapter = chapter,
#                           weight = n,
#                           typeof = 1)
#        c.save()
#        n -= 1
                                 

    return

{'fraction_non_alphanumeric': 0.11605190260970986, 'fraction_numerical': 0.0052485785099868785, 'mean_word_length': 1.697601258356272, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4980501', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1899, 'n_words': 447}