Sunday, 27 May 2012

Capture website screenshots in PHP using grabzit library


Near about 1 year ago, one of our project’s requirement was to take screenshot of some website pages at run time and save them, but at that moment our team didn’t find any proper solution. Today while I was going through Google Reader found a article “Sreenshots in PHP with GrabzIt”, went into detail of that and found useful. Although it’s properly documented on the website even then I will try to summaries in few easy steps.

1) Register yourself : http://grabz.it/register.aspx

2) After successful completion of registration you will get Application Key and 
     Application Secret. Store it somewhere.

3) Download Simple PHP Library from : http://grabz.it/api/php/download.aspx

4) Assign write and read permissions to images folder.


5) Go to GrabzItConfig.php file and set the values of configuration variables : 
    $grabzItApplicationKey, $grabzItApplicationSecret and  
    $grabzItHandlerUrl.

6) Run index.php file.


It will show you all the screenshots. As these people have used core PHP for coding so you can easily modify the code as per your requirements. You can store image ids in database and then use them later etc.


Thanks!!!!!!!! Enjoy Programming :)
 

Saturday, 12 May 2012

Draw 3D Line Chart Using JFreeChart in Java

Hi all, following is the example of how to draw 3D Line Chart in Java using JFreeChart library.

LineChart3DDemo.java


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package drawgraph;

/**
 *
 * @author sshankar
 */

import java.awt.Color;
import java.awt.Dimension;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;

/**
 * A simple demonstration application showing how to create a line chart using data from a
 * {@link CategoryDataset}.
 */

public class LineChart3DDemo extends ApplicationFrame {

    /**
     * Creates a new demo.
     *
     * @param title  the frame title.
     */

    static final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
   
    public LineChart3DDemo(String title,String categoryAxisLabel, String valueAxisLabel) {
   
        super(title);
        final CategoryDataset dataset = createDataset();
        final JFreeChart chart = createChart(title,categoryAxisLabel, valueAxisLabel,dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
    }

    /**
     * Creates a sample dataset.
     *
     * @return The dataset.
     */
    static private CategoryDataset createDataset() {
       
        dataset.addValue(1.0, "Series1", "Category1");
        dataset.addValue(4.0, "Series1", "Category2");
        dataset.addValue(3.0, "Series1", "Category3");
        dataset.addValue(5.0, "Series1", "Category4");
        dataset.addValue(5.0, "Series1", "Category5");

        dataset.addValue(5.0, "Series2", "Category1");
        dataset.addValue(7.0, "Series2", "Category2");
        dataset.addValue(6.0, "Series2", "Category3");
        dataset.addValue(8.0, "Series2", "Category4");
        dataset.addValue(4.0, "Series2", "Category5");

        dataset.addValue(4.0, "Series3", "Category1");
        dataset.addValue(3.0, "Series3", "Category2");
        dataset.addValue(2.0, "Series3", "Category3");
        dataset.addValue(3.0, "Series3", "Category4");
        dataset.addValue(6.0, "Series3", "Category5");
       
        return dataset;
               
    }
   
    /**
     * Creates a sample chart.
     *
     * @param dataset  a dataset.
     *
     * @return The chart.
     */
    private static JFreeChart createChart(String title,String categoryAxisLabel, String valueAxisLabel, CategoryDataset dataset) {
       
        JFreeChart jfreechart = ChartFactory.createLineChart3D(title, categoryAxisLabel, valueAxisLabel, dataset, PlotOrientation.VERTICAL, true, true, true);
        jfreechart.setBackgroundPaint(Color.WHITE);
       
        CategoryPlot categoryplot = jfreechart.getCategoryPlot();
        categoryplot.setBackgroundPaint(new Color(238, 238, 255));
        categoryplot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
               
        //Manages the Domain(x-axis) label position
        CategoryAxis categoryaxis = categoryplot.getDomainAxis();
        categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
   
        return jfreechart;
    }
    
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {

        final LineChart3DDemo demo = new LineChart3DDemo("3D Line Chart Demo","Category","Values");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}

After successfully running the file, you will get following output :



Thanks!!!!!!!!!!!  Enjoy Programming :)



Saturday, 5 May 2012

Shell Script to export MySQL table data into a text file

Hi, our today's topic is "Write Shell Script to export MySql table data into a text file". I will try my best to explain each and everything in steps. Let's start :

Following is full shell script

#tableexport.sh
 
#(1) define variables
DBNAME=test
FNAME=/usr/tmp/$(date +%Y.%m.%d)-$1.txt

#(2)creates an file with column names seperated by comma in a single line
mysql -u$2 -p$3 $DBNAME -B -e "SELECT COLUMN_NAME FROM information_schema.COLUMNS C WHERE table_name = '$1';" | awk '{print $1}' | grep -iv ^COLUMN_NAME$ | sed 's/^/"/g;s/$/"/g' | tr '\n' ',' > $FNAME

#(3)appends newline to mark beginning of data vs. column titles
echo "" >> $FNAME

#(4)dumps data from DB into /usr/tmp/tempfile.txt
mysql -u$2 -p$3 $DBNAME -B -e "SELECT * INTO OUTFILE '/usr/tmp/tempfile.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' FROM $1;"

#(5)merges data file and $FNAME file column names
cat /usr/tmp/tempfile.txt >> $FNAME

#(6)deletes tempfile
rm -rf /usr/tmp/tempfile.txt

You will call the script using following nomenclature.
sh   tableexport.sh  table_name  db_username  db_password

I will be using a table named bookmarks of test database for explanation.
Let’s discuss each and every step now.

I will be using a table named bookmarks of test database for explanation.

Let’s discuss each and every step now.

1)    In first step we have defined two variables.
       
       DBNAME  : It represents the database name
       FNAME : It specifies the file name and it’s location that we have to populate
   
2)    Second step is the most important one. It would extract Column names from the table
       and would write them into a single line separated by commas. One more thing to pen
       down is that to run this script file we have to pass three arguments
   
        a) Table Name b) Database Username and c) Database Password

       So in this second step $1 represents the 1st argument means table name, $2 represents
       the 2nd argument means database name and $3 represents the 3rd argument means
       database password. 
   
       Let’s go into more detail. I will divide the whole command into parts and explain
       it further.
   
       [dirtyhandsphp@blog shiv]$ mysql –u$2 –p$3 test -B -e "SELECT COLUMN_NAME
       FROM  information_schema.COLUMNS C WHERE table_name = 'bookmarks';"
   
        This command will fetch the column information of bookmarks table of test database
        and the output  would be
   
        //output starts here
       COLUMN_NAME
       bookmark_id
       bookmark_name
       bookmark_url
       created_date
       modified_date
       is_active
       //output ends here
   
      [dirtyhandsphp@blog shiv]$ mysql –u$2 –p$3 test -B -e "SELECT COLUMN_NAME
      FROM information_schema.COLUMNS C WHERE table_name = 'bookmarks';"
      | awk '{print $1}'
   
      Ask is an important unix utility. Here in out example it would read first word of the line
      and print it. As in out case whole column name is a single word so output would be same.
   
      //output starts here
      COLUMN_NAME
      bookmark_id
      bookmark_name
      bookmark_url
      created_date
      modified_date
      is_active
      //output ends here
   
      [dirtyhandsphp@blog shiv]$ mysql –u$2 –p$3 test -B -e "SELECT COLUMN_NAME
      FROM information_schema.COLUMNS C WHERE table_name = 'bookmarks';" |
      awk '{print $1}' | grep -iv   ^COLUMN_NAME$
   
      It would display all the columns except the COLUMN_NAME. So output would be
   
      //output starts here
      bookmark_id
      bookmark_name
      bookmark_url
      created_date
      modified_date
      is_active
      //output ends here
   
      [dirtyhandsphp@blog shiv]$ mysql –u$2 –p$3 test -B -e "SELECT COLUMN_NAME
      FROM information_schema.COLUMNS C WHERE table_name = 'bookmarks';" | 
      awk '{print $1}' | grep -iv ^COLUMN_NAME$ | sed 's/^/"/g;s/$/"/g'
   
      sed is again an important utility of unix.  In our example it would replace starting of the
      line (represented by ^) and end of the line (represented by $) by double quotes. Here
      g specifies the global substitute otherwise it would work only for first line.
     
      Ouput would be :
   
      //output starts here
      “bookmark_id”
      “bookmark_name”
      “bookmark_url”
      “created_date”
      “modified_date”
      “is_active”
      //output ends here
   
      [dirtyhandsphp@blog shiv]$ mysql –u$2 –p$3 test -B -e "SELECT COLUMN_NAME
      FROM information_schema.COLUMNS C WHERE table_name = 'bookmarks';" |
      awk '{print $1}' | grep -iv  ^COLUMN_NAME$ | sed 's/^/"/g;s/$/"/g' | tr '\n' ',' |
      sed 's/\(.*\),/\1./' > $FNAME
   
      Tr : Translate characters.  In this case it would replace ‘\n’ means new line character
      with comma.
   
      We have again used sed here. It would replace the last comma with space. So the final
      output of this step would be
   
      //output starts here
      “bookmark_id”,“bookmark_name”,“bookmark_url”,“created_date”,“modified_date”,
      “is_active”
      //output ends here
   
      It would be written to the $FNAME file.

3)   As the documentation says it would just put us in next line so that we can copy other
      data in $FNAME file’s next line. 

4)   Next step would just get the content from table, would separate them by comma and
      enclosed by double quotes and put them in a file /usr/tmp/tempfile.txt.

5)   5th step would append the tempfile.txt file data into $FNAME.

6)   Will delete temporary file : tempfile.txt.


Thanks!!!!!!!!!! Enjoy Programming :)          



Gmail: Download blocked file

Hi, today we are going to learn, how to download blocked file in gmail. Sometimes you see following message in gmail: and you can...