Advertisement
iliar

Untitled

Oct 15th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. package ru.iliar.RobertoBot.RandomPicture;
  2.  
  3. import java.io.File;
  4. import java.io.FilenameFilter;
  5. import java.util.ArrayList;
  6. import java.util.Random;
  7.  
  8. import ru.iliar.RobertoBot.SystemConfiguration;
  9.  
  10. public class RandomPicture implements FilenameFilter {
  11.  
  12.     /** Список каталогов с изображениями */
  13.     private static final String[] SOURCE_FOLDERS = {
  14.             "DeviantArt",
  15.             "Patreon",
  16.             "Fan-Art (SFW)",
  17.             "EasterEggs",
  18.             "Paper Sketches"
  19.         };
  20.    
  21.     /** Список запрещенных файлов */
  22.     private static final String[] HIDDEN_FILES = {
  23.             "desktop.ini",
  24.             ".DS_Store",
  25.             "Icon\r",
  26.             "2017_11_01 Multiwoofer (game).jpg",
  27.             "2017_07_27 Transistor Sword Render.png",
  28.             "2014.08.07 Beach Party Trio.zip",
  29.             "2004.04.24 ThreeGirls600800.jpg",
  30.             "2004.05.16 Tiger.jpg",
  31.             "2005.01.27 SexyKitty.jpg",
  32.             "2006.02.07 Peeping Tom.jpg",
  33.             "2006.04.14 Topless Vanilla Fanart.jpg",
  34.             "2006.05.01 Stylized painting of a nude.jpg",
  35.             "2006.10.12 Spotted Kitty Sketch.jpg",
  36.             "2007.09.05 What Are You Looking At.jpg",
  37.             "2008.03.24 Yosh Fanart - Melting IceCream.jpg",
  38.             "2008.08.11 The Basitin Ritual of Return.jpg",
  39.             "2008.12.17 Darn that Pesky Basitin Magic.jpg",
  40.             "2017.09.10 Private Muse.png", //Слишком большой файл, добавлена JPG версия
  41.             "2018.03.13 Sleeping Maddie.png", //Слишком большой файл, добавлена JPG версия
  42.             "[AsanoNisshoku] 2017.12.25 Dragon in Pants (original).gif", //Слишком большой файл
  43.             "[Andromeda38] 2016.11.11 Natani and Keith.tiff", //Несовместимый формат
  44.             "[infinitedge2u] 2016.12.06 Boop! (original).png", //Слишком большой файл
  45.             "[DarthKeidran] 2017.12.20 TwoKinds - The Last Journey (original).jpg", //Слишком большой файл
  46.             "[DarthKeidran] 2018.03.08 Shingeki No Kyodran - Titan Nibbly (Spring Snow) (original).jpg", //Слишком большой файл
  47.         }; 
  48.    
  49.     /** Список каталогов с изображениями */
  50.     public ArrayList<File> sourceFolders;
  51.    
  52.     /** Принудительная установка не случайной картинки */
  53.     private File unrandomPicture;
  54.    
  55.     /** Генератор случайных чисел */
  56.     private Random random;
  57.    
  58.     /** Конструктор класса */
  59.     public RandomPicture() {
  60.         super();
  61.        
  62.         random = new Random(System.currentTimeMillis());
  63.        
  64.         sourceFolders = new ArrayList<File>(); 
  65.         for (String folder : SOURCE_FOLDERS) {
  66.             sourceFolders.add(new File(SystemConfiguration.MAIN_FOLDER + File.separator + folder));
  67.         }      
  68.     }
  69.    
  70.     /** Метод сортировщика файлов */
  71.     public boolean accept(File dir, String name) {
  72.         for (String hiddenFile : HIDDEN_FILES) {
  73.             if (name.equalsIgnoreCase(hiddenFile)) {
  74.                 return(false);
  75.             }
  76.         }
  77.         return(true);
  78.     }
  79.    
  80.     /** Возвращает файл случайного изобржаения */
  81.     public File getRandomPicture() {
  82.         if (unrandomPicture != null) {
  83.             File result = unrandomPicture;
  84.             unrandomPicture = null;
  85.             return( result );
  86.         }
  87.        
  88.         ArrayList<File> allFiles = getPool();
  89.  
  90.         return(allFiles.get(random.nextInt(allFiles.size())));
  91.     }
  92.        
  93.     /**
  94.      * @param unrandomPicture the unrandomPicture to set
  95.      */
  96.     public void setUnrandomPicture(File unrandomPicture) {
  97.         this.unrandomPicture = unrandomPicture;
  98.     }
  99.  
  100.     /**
  101.      * Возвращает массив изображений
  102.      * @return
  103.      */
  104.     public ArrayList<File> getPool() {
  105.         ArrayList<File> allFiles;
  106.         allFiles = new ArrayList<File>();  
  107.        
  108.         for (File folder : sourceFolders) {
  109.             File[] filderFiles = folder.listFiles(this);
  110.             for (File file : filderFiles) {
  111.                 if ( file.isFile() ) {
  112.                     allFiles.add(file);
  113.                 }
  114.             }
  115.         }
  116.        
  117.         return(allFiles);
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement