//
// Mining module for TETDM 
// TweetExtraction.java Version 0.30
// Copyright(C) 2011 Yoko Nishihara All RIGHTS RESERVED.
// This program is provided under the license.
// You Should read the README file.
//

package module.MiningModules.TweetExtraction;		// Replace ALL [MiningStyle] with your [module name]

import source.*;
import source.TextData.*;
//import source.Utility.*;


import java.net.URL;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.*;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


public class TweetExtraction extends MiningModule implements ActionListener
{
	JTextField textfield;
	
	String japaneseText;
	String query = "";
	
	
	public TweetExtraction()
	{
		setModuleID(20);
		pairingVisualizationID = new int[]{2};
	}
	public void initializePanel()
	{
		operationPanel.setBackground(Color.GREEN);
		
		textfield = new JTextField(10);
		japaneseText = fileRead();
		textfield.setText(japaneseText);
		textfield.addActionListener(this);
		operationPanel.add(textfield);
	}

	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 0:				
				break;
		
			case 24:
				query = textfield.getText();
				setData(24,query);
				setData(1,MyMethod());/*violation*/
				displayOperations(24);
				break;
		}
	}
	
	
	String MyMethod()
	{
		
		Pattern patternText = Pattern.compile("<title>(.*?)</title>");
		Pattern patternUnicode = Pattern.compile("x(.+?);");
		
		String html = "";
		
		StringWriter sw = new StringWriter();			//For words
		BufferedWriter bw = new BufferedWriter(sw);				
		
		try{
			String urlText = "http://search.twitter.com/search.atom?q="+URLEncoder.encode(query, "UTF-8")+"&rpp=50&lang=ja";
			URL url = new URL(urlText);
			HttpURLConnection http = (HttpURLConnection)url.openConnection();
			http.setRequestMethod("GET");
			http.setRequestProperty("User-agent","Mozilla/5.0");
			http.setUseCaches(false);
			http.connect();
			BufferedReader br2 = new BufferedReader(new InputStreamReader(http.getInputStream(),"UTF-8"));
			
			String line = "";
			
			byte[] bytes;

			try{
			
			while((line = br2.readLine()) != null) {
				
				Matcher m2 = patternText.matcher(line);
				while(m2.find())
				{
					StringBuffer buf = new StringBuffer();
					String[] str2 = m2.group(1).split("&#");
					for(int i=0; i<str2.length; i++)
					{
						Matcher m1 = patternUnicode.matcher(str2[i]);
						while(m1.find())
						{
							String cc = m1.group(1);
							bytes = new byte[cc.length()/2];
							for(int j=0; j<bytes.length; j++)
							{
								bytes[j] = (byte)Integer.parseInt(cc.substring(j*2, (j+1)*2), 16);
							}
							str2[i] = new String(bytes, "UTF-16");
						}
						buf.append(str2[i]);
					}
					
					bw.write(buf.toString());
					bw.newLine();
				}
				
				
			}
			br2.close();
				
				bw.flush();
			}
			catch(Exception e){
				System.out.println("writing ERROR in TweetExtraction");
			}
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}

		return sw.toString();
	}
		
	
	public void actionPerformed(ActionEvent e)
	{
		insideOfActionPerformed(e);
		
		if(e.getSource () == textfield)
		{
			miningOperations(24);
		}
	}
}





