Xu Xiaodong avatar Xu Xiaodong committed 11e9c89

add define, playvcd and set_mobi_author

Comments (0)

Files changed (4)

+#!/usr/bin/env perl
+#
+# name:      define
+# author:    Xu Xiaodong <xxdlhy@gmail.com>
+# license:   GPL
+# created:   2010 Jun 28
+# modified:  2011 May 10
+#
+
+use Modern::Perl;
+use LWP::Simple qw(get);
+use URI::Escape qw(uri_escape);
+
+sub usage {
+  ( my $me = $0 ) =~ s{.*/}{};
+  die "Usage: $me <word>\n";
+}
+
+sub define {
+  my $word    = shift;
+  my $url     = "http://dict.hjenglish.com/app/w/" . uri_escape($word);
+  my $content = get($url);
+
+  die "Couldn't open $url" unless defined $content;
+
+  if ( $content =~ m{comment">(.*?)</div} ) {
+    ( $word = $1 ) =~ s{<br/?>}{\n}g;
+    binmode STDOUT, ":utf8";
+    say "$word";
+  }
+  else {
+    say "Not found";
+  }
+}
+
+if ( $#ARGV != 0 ) {
+  usage();
+}
+else {
+  define( $ARGV[0] );
+}
+
+# vim: ai:et:ts=2:sw=2:sts=2:tw=78:ft=perl
 # author:    Xu Xiaodong <xxdlhy@gmail.com>
 # license:   GPL
 # created:   2011 May 06
-# modified:  2011 May 06
+# modified:  2011 May 07
 #
 
 require 'nokogiri'
 require 'open-uri'
 
-def fetch_images(url, suffix)
-  doc = Nokogiri::HTML(open(url))
-  doc.xpath('//img').each do |img|
-    img_src = img['src']
-    if img_src.to_s.match("\.#{suffix}$")
-      puts "fetching #{img_src}"
-      system('wget', img_src)
+class Fetcher
+  def initialize(url, suffix)
+    @url, @suffix = url, suffix
+  end
+
+  def fetch
+    doc = Nokogiri::HTML(open(@url))
+    doc.xpath('//img').each do |img|
+      img_src = img['src']
+      if img_src.to_s.match(/\.#@suffix$/)
+        puts "*** #{img_src}"
+        puts '... fetching'
+        ret = system('wget', '-q', img_src)
+        puts '... done' if ret
+      end
     end
   end
+
 end
 
 if __FILE__ == $0
   begin
-    fetch_images(*ARGV)
+    fe = Fetcher.new(*ARGV)
+    fe.fetch
   rescue ArgumentError => err
     puts "#{err}, usage: fetch_images <url> <suffix>"
   end
+#!/usr/bin/env perl
+#
+# name:      playvcd, playing vcd dat files with mplayer
+# author:    Xu Xiaodong <xxdlhy@gmail.com>
+# license:   GPL
+# created:   2010 Jun 30
+# modified:  2011 May 10
+#
+
+use strict;
+use warnings;
+
+my $mount   = "/bin/mount";
+my $filesys = "iso9660";
+my $cdrom   = "/dev/hda";
+my $path    = "/mnt/cdrom/";
+system $mount, "-t", $filesys, $cdrom, $path;
+
+$path .= "mpegav/";
+opendir( my $vcd, $path );
+my @files = grep { /\.dat$/ } readdir($vcd);
+closedir($vcd);
+
+my $mplayer = "/usr/bin/mplayer";
+for my $number ( 2 .. scalar @files ) {
+  system $mplayer, "-cdrom-device", $cdrom, "vcd://$number";
+}
+
+# vim: ai:et:ts=2:sw=2:sts=2:tw=78:ft=perl
+#!/usr/bin/env perl
+#
+# name:      set_mobi_author
+# author:    Xu Xiaodong <xxdlhy@gmail.com>
+# license:   GPL
+# created:   2011 Feb  7
+# modified:  2011 May 10
+#
+
+use Modern::Perl;
+use FindBin qw($RealBin);
+use lib "$RealBin";
+use Palm::PDB;
+use Palm::Doc;
+use MobiPerl::MobiHeader;
+use MobiPerl::Util;
+
+my $author   = shift;
+my $in_file  = shift;
+my $out_file = $$ . 'mobi';
+
+my $pdb = new Palm::PDB;
+$pdb->Load($in_file);
+
+my @records       = @{ $pdb->{records} };
+my $r0            = $records[0];
+my $palmdocheader = substr( $r0->{data}, 0, 16 );
+
+my $mh = substr( $r0->{data}, 16 );
+$mh = MobiPerl::MobiHeader::set_exth_data( $mh, "author", $author );
+
+$r0->{data} = $palmdocheader . $mh;
+
+$pdb->{type}    = "BOOK";
+$pdb->{creator} = "MOBI";
+
+$pdb->Write($out_file);
+
+rename $out_file => $in_file;
+
+# vim: ai:et:ts=2:sw=2:sts=2:tw=78:ft=perl
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.