Last active
August 28, 2016 21:14
-
-
Save jirutka/4051571 to your computer and use it in GitHub Desktop.
Script for creating directory A-Z index and recent items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
# encoding: utf-8 | |
############################################################################### | |
# Copyright 2012 Jakub Jirutka. All rights reserved. | |
# | |
# "THE KOFOLA-WARE LICENSE" (Revision 1): | |
# Jakub Jirutka originally wrote this file. As long as you retain this notice you | |
# can do whatever you want with this stuff. If we meet some day, and you think | |
# this stuff is worth it, you can buy me a Kofola in return. <jakub@jirutka.cz> | |
# | |
############################################################################### | |
# | |
# Script for creating directory A-Z index and recent items | |
# | |
# I'm using this script to create A-Z symlinks index of movies for miniDLNA. | |
# | |
# @author Jakub Jirutka <jakub@jirutka.cz> | |
# @version 1.0 | |
# @date 2012-10-10 | |
# | |
import os | |
from os import path | |
import shutil | |
from string import ascii_uppercase, digits | |
import time | |
from datetime import date, timedelta | |
################################ Constants ################################ | |
# where directories to be indexed are located | |
# note: this script does not support nested directories | |
SRC_DIR = "/mnt/shared/media/movies" | |
# where to generate A-Z directories with symlinks | |
DEST_DIR = "/var/lib/minidlna/index/movies" | |
# where to make symlinks to most recent items | |
RECENT_DIR = path.join(DEST_DIR, "0_RECENT_0") | |
# maximum number of items per A-Z group | |
# note: applies only when joining more groups (A,B,...) into one (A-C) | |
GROUP_LIMIT = 10 | |
# how old items will be in RECENT_DIR (days) | |
# note: uses ctime attribute of directory | |
RECENT_DAYS = 30 | |
################################## Main ################################### | |
# prepare | |
dirs_list = sorted(name for name in os.listdir(SRC_DIR) if path.isdir(path.join(SRC_DIR, name))) | |
recent_until = date.today() - timedelta(days=RECENT_DAYS) | |
recent_until = time.mktime(recent_until.timetuple()) | |
print("Removing old links in %s..." % DEST_DIR) | |
os.chdir(DEST_DIR) | |
for dir in os.listdir(DEST_DIR): | |
shutil.rmtree(dir) | |
print("Creating links to recent items:") | |
os.mkdir(RECENT_DIR) | |
for name in dirs_list: | |
src_path = path.join(SRC_DIR, name) | |
ctime = os.stat(src_path).st_ctime | |
if ctime > recent_until: | |
print(" ", name) | |
dest_path = path.join(RECENT_DIR, name) | |
os.symlink(src_path, dest_path) | |
print("Creating A-Z index:") | |
# count frequencies | |
dirs_list_freq = {} | |
for index in digits + ascii_uppercase: | |
items = sum( name.startswith(index) for name in dirs_list ) | |
if items: dirs_list_freq[index] = items | |
# make groups | |
groups = [] | |
current = []; items = 0 | |
for index, freq in sorted(dirs_list_freq.items()): | |
if items + freq < GROUP_LIMIT: | |
current.append(index) | |
else: | |
groups.append(current) | |
current = [index] | |
items = 0 | |
items += freq | |
# create links | |
for group in groups: | |
group_name = str(group[0]) | |
if len(group) > 1: | |
group_name += '-' + str(group[-1]) | |
group_path = path.join(DEST_DIR, group_name) | |
# create group directory | |
os.mkdir(group_path) | |
print(group_name, end=', ') | |
# create symlinks | |
for index in group: | |
for name in filter(lambda x: x.startswith(index), dirs_list): | |
os.symlink(path.join(SRC_DIR, name), path.join(group_path, name)) | |
print("\nCompleted") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment