Advertisement
imcrazytwkr

Tumblr blog trimmer

Apr 30th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.34 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. exec 2>/dev/null
  4.  
  5. root_dir=$(realpath $1)
  6. meta_dir="$root_dir/!meta"
  7.  
  8. for blog in $root_dir/*; do
  9.   if [ ! -d $blog ]; then
  10.     continue
  11.   fi
  12.  
  13.   if [ $blog = $meta_dir ]; then
  14.     echo 'Meta dir found, skipping...'
  15.     continue
  16.   fi
  17.  
  18.   for dir in $(find $blog -d 1 -type d -not -name 'json' -not -name 'media'); do
  19.     rm -rf "$dir"
  20.   done
  21.  
  22.   json_dir="$blog/json"
  23.  
  24.   if [ -d $json_dir ]; then
  25.     find $json_dir -d 1 -type f -not -name '*.json' -exec rm -f "{}" \;
  26.   else
  27.     if [ -e $json_dir ]; then
  28.       rm -rf "$json_dir"
  29.     fi
  30.  
  31.     mkdir "$json_dir"
  32.   fi
  33.  
  34.   for file in $(find $blog -d 1 -type f -name '*.json' | sort -r); do
  35.     file_name=$(basename $file)
  36.  
  37.     hash=$(md5 -q $file)
  38.     if [ $(find $json_dir -d 1 -type f -name '*json' | xargs md5 -q | grep -c "$hash") -lt 1 ]; then
  39.       mv "$file" "$json_dir/$file_name"
  40.       continue
  41.     fi
  42.  
  43.     rm -f "$file"
  44.   done
  45.  
  46.   media_dir="$blog/media"
  47.  
  48.   if [ -d $media_dir ]; then
  49.     for image in $(find $media_dir -d 1 -type f); do
  50.       image_type=$(identify -format '%m\n' "$image" | awk 'NR==1 { print tolower($1) } NR > 1 { exit }')
  51.  
  52.       # Not an image and not JSON since we've already removed them
  53.       if [ -z "$image_type" ]; then
  54.         rm -f "$image"
  55.         continue
  56.       fi
  57.  
  58.       # Image, need to move it to media_dir
  59.       image_name="$(basename $image | awk -F '.' 'NR==1 { print $1 } NR > 1 { exit }').$image_type"
  60.       if [ "$image_name" = "$(basename $image)" ]; then
  61.         continue
  62.       fi
  63.  
  64.       mv "$image" "$media_dir/$image_name"
  65.     done
  66.   else
  67.     if [ -e $media_dir ]; then
  68.       rm -rf "$media_dir"
  69.     fi
  70.  
  71.     mkdir "$media_dir"
  72.   fi
  73.  
  74.   for image in $(find $blog -d 1 -type f); do
  75.     image_type=$(identify -format '%m\n' "$image" | awk 'NR==1 { print tolower($1) } NR > 1 { exit }')
  76.  
  77.     # Not an image and not JSON since we've already removed them
  78.     if [ -z "$image_type" ]; then
  79.       rm -f "$image"
  80.       continue
  81.     fi
  82.  
  83.     # Image, need to move it to media_dir
  84.     image_name="$(basename $image | awk -F '.' 'NR==1 { print $1 } NR > 1 { exit }').$image_type"
  85.     mv "$image" "$media_dir/$image_name"
  86.   done
  87.  
  88.   if [ $(ls $media_dir | wc -l) -lt 1 ]; then
  89.     rm -rf $blog
  90.     printf 'Removed empty blog %s\n' $(basename $blog)
  91.     continue
  92.   fi
  93.  
  94.   printf 'Trimmed %s\n' $(basename $blog)
  95. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement