Home › Category Archives › Coding

CakePHP Automatic Admin Plugin

CakePHP Plugin for automagic admin using Twitter Bootstrap. Version 1.0 for CakePHP 2.x

This plugin uses the cake scaffolding to create admin panel automagically!

Source Code Hosted in Github: https://github.com/Maldicore/Admin

Youtube Video Download Ruby Script

The nation is mourning on the loss of 4 students and the principal of Hiriyaa School in a drowning accident. After some time out with a friend I decided to spend the day indoor. When at home and with my computer can’t help, but get productive. So I decided to do a little experiment for a download manager that we are working on. Here is a ruby experimental script that can download YouTube Videos. Working as of September 9, 2011 but YouTube changes a lot so let me know when this breaks.

Note this a rough script, so feel free to improve.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
require 'open-uri'
youtubepath = 'http://www.youtube.com/watch?v=6tWLqFmaNdQ'
uri = URI.parse(youtubepath)
open(uri) do |file|
  openedsource = file.read
#  search for the title
  rgtitlesearch = Regexp.new(/\<meta name="title" content=.*/)
  vidtitle = rgtitlesearch.match(openedsource)
 vidtitle = vidtitle[0].gsub("<meta name=\"title\" content=\"","").gsub("\">","").gsub(/ /,'')+".flv"
# search for the download link
  rglinksearch = Regexp.new(/,url=.*\\u0026quality=/)
  vidlink = rglinksearch.match(openedsource)
 vidlink[0].split(",url=").each do |foundlinks|
   vidlink = foundlinks.gsub(",url=","").gsub("\\u0026quality=","").gsub("%3A",":").gsub("%2F","/").gsub("%3F","?").gsub("%3D","=").gsub("%252C",",").gsub("%253A",":").gsub("%26","&")
  end
def download vidlink, vidfile
 writeOut = open(vidfile, "wb")
 writeOut.write(open(vidlink).read)
 writeOut.close
end
 download(vidlink,vidtitle)

 puts "Download Done!"
end

Things missing and you might want to improve…

- Easier YouTube link inputs
- Assign Video Type (mp4, flv, etc) and Quality
- Download Progress Bar
- Clean up the code, including remove dups

Ruby simply rocks!

Working on a SMS Gateway Portal (PHP and Kannel) and needed a bulk list of Phone numbers to populate table for testing. Wanted to use real data so while searching the Internet I came across Jaa’s Blog (http://www.jawish.org/) with a data set, though it is dated. I hate to use spreadsheets so created the below script using Ruby. Well, I am only learning Ruby so this was rather a test, and I am surprised how fast I came up with this.

Note that I am working on Linux so if you want to use the code you will need to set the directory structure appropriately. Also take note that I have skipped a lot of validation and cleaning up. However, the script managed to populate the table in 72 seconds. Well, did this is a hurry, 36 lines of coding in 20 min (including testing).

For the inspiration of newbies like me I am sharing the code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require 'rubygems'
require 'mysql'
timebegin = Time.now
database_name = "directory"
table_name = "dhiraagu"
# Phone List Downloaded FROM
# http://www.jawish.org/blog/archives/347-Dhiraagu-e-Directory-data-for-download-2009-04-12.html
csvfile = '/home/yusuf/workspace/csv/edirectory/edirectory_2009-04-12.txt'
m = Mysql.new("localhost", "username", "password")
m.select_db(database_name)
m.query("DROP TABLE IF EXISTS "+table_name)
ptable_create = "CREATE TABLE "+table_name+"(
id INT auto_increment primary key,
name CHAR(100),
number INT(11));"

m.query(ptable_create)
putrecord = 0
File.open(csvfile).each do |record|
i = 1
putrecord = putrecord + 1
puts putrecord.to_s()
pname = ''
record = record.force_encoding('ISO-8859-1').chomp.gsub(/[\"\'\\]/,"").gsub(/\s,/,",\s").to_s()
record.split("
\t").each do |field|
case field
when /^[79365]./ then
pnumber = field
pquery = "
INSERT INTO `"+table_name+"` VALUES('','"+pname.to_s()+"','"+pnumber.to_s()+"');"
m.query(pquery)
else
pname = field
end
end
end
m.close
puts "
Time elapsed #{Time.now - timebegin} seconds"

I am loving Ruby especially the one liners, gems and clear codes. All I can say is Ruby simply rocks!

You can download the codes here: csv.rb