« WiiでもWikipediaを見たい! | Main | ネットラジオが聞ければいいんじゃないの? »

Wiipediaのソースコード晒し

先日エントリしたWiipedia & Unwiipediaのコードを晒しておきます。

やってることといえば、

  1. ページを取ってきて<!-- start content -->から<!-- end content -->までを抜き出す。
  2. 元ページのリンクを自分自身(wiipedia.cgi)を通すように差し替える。
  3. ヘッダとフッタを付ける。

だけなので、CSSさえ書き換えれば携帯向けにもなんにでもなります。

試してないけど英語版のWikipediaやUncyclopediaを見るのもできるはず。

wiipedia.cgi
#!/usr/bin/perl
#
# - Wikipedia Gateway - 
# 要はCSSを書き換えてるだけなので、それさえ書き換えれば携帯向けにでもなんにでも。
# by ぶ at 天狗屋敷.jp
#

use strict;
use warnings;

use Jcode;
use LWP::UserAgent;
use CGI;# qw(-debug);
use CGI::Carp qw(fatalsToBrowser);

my $srcurl = 'http://ja.wikipedia.org';				# 見に行くサイトURL
#my $srcurl = 'http://ja.uncyclopedia.info';		# Uncyclopediaを見に行くならこっち。

my $script = 'wiipedia.cgi';						# スクリプトのファイル名。
my $siteurl = 'http://tenguyasiki.jp/wiipedia/';	# 設置URL。リンクの張り替えに使います。

# 引数無しで呼び出したときに表示するページ
my $defaultpage = 'Special:Random';	

# プロキシ指定
my $proxy	= "";

#
#### 設定ここまで
#

my $charset = 'utf-8';

my $cgi = CGI->new();
my $page = $cgi->param('index');

my $ua = LWP::UserAgent->new; 
$ua->proxy(['http'] => $proxy);

unless(defined $page){
	$page = $defaultpage;
}

my $res = $ua->get( $srcurl.'/wiki/'.$page );
my ($t, $c) = pickup_content($res->as_string, $srcurl);

my $html = make_html($t, $c);
print $cgi->header(-charset=>"$charset");
print $html;

exit;

# 文字列と補完用URLを渡すと、タイトルとページ本体(リンク先変更済み、画像URLの補完済み)を返す。
sub pickup_content{
	my $str = $_[0];

	my ($title, $content) = ("", "");
	my ($begin, $end) = (0, 0);

	my @lines = split(/\n/, $str);

	# <!-- start content --> ~ <!-- end content-->の間を抜き出してテンプレートに挟み込むだけ。
	foreach my $line (@lines){
		$title = $1		if( $title eq "" and $line =~ /<h1 class=\"firstHeading\">(.+)<\/h1>/);
		$begin = 1		if( $begin == 0 and $line =~ /<\!--\s+start\s+content\s+-->/);
		if( $begin == 1 and $end == 0){
			$line =~ s/<a href=\"\/wiki\//<a href=\"$script\?index\=/g;
			$line =~ s/<a href=\"\/index\.php\?title=/<a name=\"/g;
			$line =~ s/<img src=\"\//<img src=\"$srcurl\//g;
			$content .= $line;
		}
		$end   = 1		if( $end == 0 and $line =~ /<\!--\s+end\s+content\s+-->/);
	}
	
	return ($title, $content);
}

# サイト表示のテンプレート
sub make_html{
	my ($t, $c) = @_;

	#ヘッダ
	my $html = << "_EoHEAD";
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="styles.css" type="text/css" />
<title>Wikipedia : $t ( Wiipedia / tenguyasiki.jp )</title>
</head>
<body>
<div>このページはNintendo Wii向けに再レイアウトされています。本来のサイトは<a href="$srcurl">$srcurl</a>です。</div>
_EoHEAD

	$html .= "<h1>$t</h1>\n";
	$html .= $c;

	#フッタ	
	$html .= << "_EoFOOT";
<hr />
by <a href="http://tenguyasiki.jp/">天狗屋敷.jp</a>
</body>
</html>
_EoFOOT

	return $html;
}
styles.css
body {
	font-size : 28px;
	line-height:1.1em;
}

div.content{
	overflow: hidden;
}

span.editsection {
	visibility : hidden;
	float : right;
}

h1 {
	display:block;
	margin:20px 0 10px 10px;
	padding:10px;
	font-family:"Trebuchet MS",arial,sans-serif;
	font-size:1.8em;
	color:#c00;
	border-color:#ddd;
	border-style:solid;
	border-width:0 0 1px 0;
	text-align : right;
}
h2 {
	display:block;
	margin:20px 0 0 30px;
	padding:0;
	font-family:"Trebuchet MS",arial,sans-serif;
	font-size:1.1em;
	color:#000;
	border-color:#c00;
	border-style:solid;
	border-width:0 0 1px 0;
}

h3 {
	display:block;
	margin:20px 0 0 30px;
	padding:0;
	font-family:"Trebuchet MS",arial,sans-serif;
	color:#000;
	border-color:#c00;
	border-style:solid;
	border-width:0 0 1px 0;
}
	
	
div.uncyclopedia{
	float : right;
	width : 40%;
	height : 200px;
	margin : 5px;
	padding : 5px;
	background-color : #cccccc;
}

div.wikipedia{
	float : left;
	width : 40%;
	height : 200px;
	margin : 5px;
	padding : 5px;
	background-color : #cccccc;
}

div.info{
	clear : both;
}

TrackBack

TrackBack URL for this entry:
http://tenguyasiki.jp/MT/mt-tb.cgi/37

Listed below are links to weblogs that reference Wiipediaのソースコード晒し:

» viejas casino alpine from viejas casino alpine
suckle levied punted [Read More]

About

This page contains a single entry from the blog posted on 2007年01月09日 21:08.

The previous post in this blog was WiiでもWikipediaを見たい!.

The next post in this blog is ネットラジオが聞ければいいんじゃないの?.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.
Powered by
Movable Type 3.31