Sunday, August 7, 2011

get/backup a user's tweets by bash

A shell script to download all the tweets of some user to a text file.

Put the contents of this file to a file called get_tweets.sh for example.
Then do "chmod +x get_tweets.sh"
And follow the usage instructions.
#!/bin/bash
#written by rikijpn
#use me like this:
#get_tweets.sh $SOME_USER
#or replace the USERNAME variable for the name of the wanted user
#get_tweets.sh

if [ ! -z $1 ] ;then
    USERNAME=$1
else
    USERNAME=linuxquestions #any default user name you want
fi
URL="http://twitter.com/$USERNAME?page="
FILE_TO_SAVE="tweets.txt"
TEMP_PAGE=`date +%d%m%H%M%S`.txt

printf "\n" > $FILE_TO_SAVE

for PAGE_NUMBER in {1..1000}; do 
    echo "getting page number $PAGE_NUMBER..."
    wget -O $TEMP_PAGE "$URL""$PAGE_NUMBER" 2>/dev/null;
    RELEVANT_LINES_NUMBER=`grep -e 'entry-content' -e 'timestamp' $TEMP_PAGE|wc -l`
    if [ $RELEVANT_LINES_NUMBER -lt 1 ] ; then  break; fi
    grep -e 'entry-content' -e 'timestamp' $TEMP_PAGE |
    sed \
 -e 's,.*<span class="entry-content">,,g' \
 -e 's,</span>,,g' \
 -e 's,.*<span class="published timestamp" data="{time:,,g' \
 -e "s/'//g" \
 -e 's/}">/ --> /g' \
 -e 's/<a.[^>]*>\(.[^<]*\)<\/a>/\1/g' \
 -e 's/^.*<span class="shared-content">/<RETWEET>/g' \
 -e 's/<\/a>/ in local time\n/g' >>$FILE_TO_SAVE
    sleep 5
done

rm $TEMP_PAGE
echo "all pages fetched"
exit 0

It differences "retweets", and outputs the date so you can tell when was it posted.

You can read it at my personal webpage as well.

Welcome/self introduction party of my company

25 years old, male.
I'm dancing to this song:



The purpose of this party, to meet the managers, let some impression, and thus making work friendlier/easier...
Well, it's 5 of us dancing to this so it shouldn't be that embarrassing(?).

Sunday, July 24, 2011

getting variables from GET method in perl (cgi)

Suppose we have a page with the following form:

<form action="index.pl" method="get">
<input type="text" name="search_for" size="30" />
<input type="submit" value="search" />
</form>

So, there's a text input field named "search_for", and a submit button.
The following code gets the variables set in QUERY_STRING (whatever is after pagename.pl?...), into the $arg associative array. So we can get the value of "search_for" by $arg{"search_for"}.

for (split /\&/, $ENV{QUERY_STRING}) {
($key,$val) = split /\=/;
$val =~ s/\+/ /g;
$val =~ s/%([0-9a-fA-F]{2})/chr(hex($1))/ge;
$arg{$key} = $val;
}
print "data searched for is =
$arg{search_for}
";

Friday, July 22, 2011

apache cgi enabled site on specific dir

As an example, using /home/username/some_test_dir/, although it's obviously better not to use home dirs for this kind of things.

These settings allow you to access the files in /home/username/some_test_dir on the web by localhost/some_web_dir (set in alias).
And also enables you to run cgi scripts there.
In the shell:
a2enmod cgi #enable cgi
a2ensite site-filename
/etc/init.d/apache2 reload #in debian

Where filename, is the name of a file containing the following text, placed in /etc/apache2/sites-available/:
<VirtualHost *:80>
 ServerAdmin this_is_my_name@my_test_domain.com

 DocumentRoot /home/username/some_test_dir/
 <Directory "/home/username/some_test_dir">
  AllowOverride None
  Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
         allow from all
                AddHandler cgi-script .cgi .pl .sh
 </Directory>
        Alias /some_web_dir "/home/username/some_test_dir/"

 ErrorLog ${APACHE_LOG_DIR}/some_web_dir_error.log

 # Possible values include: debug, info, notice, warn, error, crit,
 # alert, emerg.
 LogLevel warn

 CustomLog ${APACHE_LOG_DIR}/some_web_dir_access.log combined
</VirtualHost>

simple cgi shell script

You just print out an html page. The only other thing you have to care about is outputting:
Content-type: text/html

(followed by two newlines characters)
And then, as long as your apache settings are working and the file has executable permission (chmod a+x filename), your browser should display the proper page.

#!/bin/sh
#you need this part for cgi handling
printf "Content-type: text/html\n\n"

# contents
echo "<html>"
echo "<head><title>Dude</title></head>"
echo "<body>"
echo "<h1>some header</h1>"
echo "awesome"
echo "</body>"
echo "</html>"

Thursday, July 21, 2011

connect to mysql with perl

Just as the title says:
Install the package libdbi-perl.

Create the databases/tables/users/permissions as needed.
This is a sample script:

#!/usr/bin/env perl

use DBI;

# MySQL CONFIG VARIABLES
$host = "localhost";
$database = "your_database";
$tablename = "test";
$user = "this_is_me";
$pw = "this_is_my_password";

#connect to database
$connect = DBI->connect('DBI:mysql:database=' . $database . ';host=' . $host, $user, $pw);


# select stuffs
my $sel = $connect->prepare("select * from $tablename");
$sel->execute;
while( @row = $sel->fetchrow_array) {
    foreach (@row) {
 print;
 print "\t";
    }
    print "\n";
}

This code prints every row, tab-separated from the selected table.

Tuesday, July 19, 2011

damn Osaka

I finally got her.
The perfect Japanese girl I've longed so much for.
I'm crazy about her, and she for some reason likes me too!
I ask her to marry me, she says OK!

And then...


Then she gets transferred to Osaka><

For god's sake...
Twenty five f*cking years living like crap, hoping for a better tomorrow, enduring so much sh*t, only for the day I'd meet someone like her, and make me feel it was all worth it...
And in the 3th month since we joined the company, she gets transferred to Osaka...
It has to be a bad joke.

Who do I have to beg for to bring her back to the Tokyo branch?><

Whatever, I'll either wait for her to come back here, or just over there myself.
I do like my job (? what I think it will be like), but I know I'll never find someone like her again and I'm not planning to waste that.

Oh, come on, we've had enough for a life time, just bring her back to Tokyo and let us be together (@whoever can change this...).