000
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

package br.com.hkp.whatsappwebfix;

import br.com.hkp.whatsappwebfix.gui.ProgressFrame;
import br.com.hkp.whatsappwebfix.util.FileTools;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*****************************************************************************
 * Baixa os arquivos PNG com figuras de Emojis estilo WhatsApp no site da 
 * Emojipedia.
 * 
 * @since 27 de novembro de 2020 
 * @version 1.0
 * @author "Pedro Reis"
 ****************************************************************************/
public final class DownloadPngs
{
    private final ProgressFrame frame;
    
    private static final String TARGET_DIR = "png";
   
    /*[00]---------------------------------------------------------------------
    
    -------------------------------------------------------------------------*/
    /**
     * Construtor da classe
     */
    public DownloadPngs()
    {
        frame = new ProgressFrame("Baixando...", 800, 450);
    }//construtor
   
    /*[01]---------------------------------------------------------------------
    
    -------------------------------------------------------------------------*/
    private void downloadAll(final Matcher m) throws IOException
    {
        int count = 0;
        
        while (m.find())
        {
            String s = m.group();
            
            String url = s.substring(s.indexOf("https:"), s.length());
            
            FileTools.downloadUrl(url, TARGET_DIR);
            
            frame.println(String.format("%04d - %s\n", ++count, url));
        }
 
    }//downloadAll()
    
    /*[02]---------------------------------------------------------------------
    
    -------------------------------------------------------------------------*/
    /**
     * Baixa os arquivos listados na pagina de WhatsApp emojis da Emojipedia.
     * 
     * @param emojipediaFile Arquivo com o fonte da pagina HTML da Emojipedia 
     * que lista os emojis do whatsapp
     * 
     * @throws IOException Em caso de erro de IO
     */
    public void downloadPngs(final File emojipediaFile) throws IOException
    {
      
        File downloaDir = new File(TARGET_DIR);
        
        if (!downloaDir.exists())
        {
            if (!downloaDir.mkdirs())
            {
                System.err.println("Erro ao criar pasta /" + TARGET_DIR);
                System.exit(1);
            }
        } 
                       
        String contentFile = FileTools.readTextFile(emojipediaFile);
             
      /*---------------------------------------------------------------------*/
            
        frame.setVisible(true);
        
        /*
        regex para localizar nomes de arquivos PNG no atributo srcset
        */
        Pattern srcSet = 
            Pattern.compile(" srcset=\"https.+?\\." + TARGET_DIR + "\\b");
        
        Matcher m = srcSet.matcher(contentFile);
        
        downloadAll(m);//baixa os arquivos que regex localizar
        
        /*
        regex para localizar nomes de arquivos PNG no atributo data-src
        */
        Pattern dataSrc = 
            Pattern.compile("data-src=\"https.+?\\." + TARGET_DIR + "\\b");
        
        m = dataSrc.matcher(contentFile);
        
        downloadAll(m);//baixa os arqs. que regex localizar
        
        frame.setTitle("");
        
        frame.println("Arquivos baixados para a pasta " + TARGET_DIR);
               
        java.awt.Toolkit.getDefaultToolkit().beep();
   
    }//downloadPngs()
    
}//classe DownloadPngs