Пример #1
0
def handleItemForm(request, form, item):
    error = None

    language = Language.getOrCreate(form.cleaned_data['language'])
    source = Source.getOrCreate(form.cleaned_data['source'])
    filesize = None
    if form.cleaned_data['size']:
        filesize = form.cleaned_data['size']

    itemInstance = ItemInstance.getOrCreate(size=filesize,
                                            language=language,
                                            source=source,
                                            creator=request.user,
                                            item=item)
    itemInstance.save()

    if form.cleaned_data['file']:
        itemInstance.path = generateDestinationPath(form)
        if form.cleaned_data['tar']:
            tf = tarfile.open(fileobj=form.cleaned_data['file'])
            tf.extractall(path=path)
        else:
            destination = open(path+os.path.basename(form.cleaned_data['file'].name), 'wb+')
            for chunk in form.cleaned_data['file'].chunks():
                destination.write(chunk)
            destination.close()
        itemInstance.size = form.cleaned_data['file'].size
        itemInstance.mtime = int(os.stat(path).st_mtime)
        itemInstance.present = True
        itemInstance.save()
        form = None
        error = 'Thank you for contributing.'
    elif (form.cleaned_data['dlLinks'] or form.cleaned_data['dlLinksFile']) and form.cleaned_data['keyfile']:
        dlUrls = []
        if form.cleaned_data['dlLinks']:
            dlUrls = [ url.strip() for url in form.cleaned_data['dlLinks'].split(',')]
        else:
            dlUrls = [ url.strip() for url in form.cleaned_data['dlLinksFile'] ]
        if len(dlUrls) > 0:
            key = EncryptionKey.fromFileHandle(form.cleaned_data['keyfile'])
            downloadFileGroup = DownloadFileGroup(itemInstance=itemInstance, key=key)
            downloadFileGroup.save()
            for url in dlUrls:
                df = DownloadFile(downloadFileGroup=downloadFileGroup, downloadLink=url)
                df.save()
        form = None
        error = 'Thank you for contributing.'
    else:
        form = None
        error = "Thank you for enlisting your item. You will be contacted when someone requests this item."
    return form, error
Пример #2
0
def addSeries(directory):
    serieregex = re.compile("^(?P<title>.*?)$")
    seasonregex = re.compile(
        '^s(?:eason)?\s*0*(?P<number>\d+)(?:\s+-\s+(?P<subtitle>.*?))?(?:\s+\((?P<year>\d{4})\))?(?:\s+\[(?P<source>[a-zA-Z0-9!"$%&/()=\\\\?_-]*)\])?(?:\s+\|(?P<language>.*)\|)?$',
        re.I,
    )
    if os.path.isdir(directory):
        for serieDir in os.listdir(directory):
            seriePath = directory + "/" + serieDir
            if os.path.isdir(seriePath):
                serieDirName = os.path.basename(serieDir)
                serieName = serieregex.match(serieDirName).group(1)
                for seasonDir in os.listdir(seriePath):
                    if os.path.isdir(seriePath + "/" + seasonDir):
                        seasonDirName = os.path.basename(seasonDir)
                        match = seasonregex.match(seasonDirName)
                        if match:
                            seasonDict = match.groupdict()
                            seasonNumber = seasonDict["number"]
                            ssubname = seasonDict.get("subtitle", None)
                            syear = seasonDict.get("year", None)
                            seasonPath = directory + "/" + serieDirName + "/" + seasonDirName
                            serie = Serie.getOrCreate(name=serieName)
                            smtime = int(os.stat(seasonPath).st_mtime)
                            ssource = Source.getOrCreate(name=seasonDict.get("source", None))
                            slanguage = Language.getOrCreate(name=seasonDict.get("language", None))
                            directoryListing = ""
                            dirlist = os.listdir(seasonPath)
                            dirlist.sort()
                            for d in dirlist:
                                directoryListing += "%s\n" % d
                            directoryListing = directoryListing[:-1]
                            season = Season.getOrCreate(serie=serie, number=seasonNumber, subname=ssubname, year=syear)
                            try:
                                itemInstance = ItemInstance.objects.get(path=seasonPath)
                                if itemInstance.mtime < smtime:
                                    itemInstance.path = seasonPath
                                    itemInstance.mtime = smtime
                                    itemInstance.size = getDirSize(seasonPath)
                                    itemInstance.save()
                            except ItemInstance.DoesNotExist:
                                ssize = getDirSize(seasonPath)
                                print (serieName, seasonNumber, ssubname, syear, ssource, ssize)
                                itemInstance = ItemInstance(
                                    item=season,
                                    path=seasonPath,
                                    source=ssource,
                                    language=slanguage,
                                    mtime=smtime,
                                    size=ssize,
                                    #                                                            directoryListing=directoryListing,
                                    present=True,
                                )
                                itemInstance.save()
                        else:
                            print "WARNING: invalid season directory format '%s'" % seasonDirName
                    else:
                        print "WARNING: season directory contains invalid file '%s'" % seasonDir
            else:
                print 'WARNING: invalid file in series directory "%s"' % serieDir
    else:
        print 'ERROR: no such directory "%s"' % (directory)