Recent Posts
Recent Comments
04-24 09:21
관리 메뉴

동글동글 라이프

프로야구 순위 출력 본문

개발자 이야기/Perl

프로야구 순위 출력

동글동글라이프 2008. 10. 7. 16:00
얼마전 해커쏜에 참여하여 간단한 프로그램을 하나 만들었다.
그 당시 a3r0님께 많은 도움을 받아서 그것을 바로 흡수!!
타자 및 투수의 정보까지 처리하는 루틴까지 만들었던 기억이 난다.

 

소개하자면 일명 Baseball::KBO
한국야구위원회
(http://www.koreabaseball.com/ ) 참고하여 만들어 졌으며

사이트의 정보들을 LWP::Simple 모듈을 이용하여 웹에서 해당 정보들을 얻어온

정규식을 이용해서 원하는 정보들을 찾아 저장하는 형식으로 코드를 작성하였다.

 

그 뒤 GTK문서를 보고 적당한 예제가 하나 있어 그 예제를 참고하여
GUI모드로 구현을 해보았다. 소스는 허접하지만...
누군가 더 나은 코드를 코멘트로 적어 줄까 싶어 공개한다.

블로그를 시작한지 글을 8건이나 썼는데... 아직 코맨트가 하나도 남겨지지 않았다. ㅠㅠ

#!/usr/bin/perl
=doc
This example shows one way to colorize rows in a SimpleList.
The basic approach is to add a color attribute to the columns
in the TreeView
(remember that a SimpleList is a TreeView), and store the color information in a hidden column in the model. Since the Glib type system will be used to fetch the color attribute, the hidden column must be of type Gtk2::Gdk::Color, which requires us to add a custom column type to SimpleList. =cut use strict; use warnings; use Glib qw/TRUE FALSE/; use Gtk2 -init; use Gtk2::SimpleList; use Encode qw(decode); use LWP::Simple; # in gtk+ 2.0.x, the cell renderers don't have the cell_background_gdk # property, so this test doesn't work right and spews messages. object # properties require no extra binding glue, so if you've upgraded to # gtk+ 2.2.x or newer after installing Gtk2-Perl, this test will magically # start to work. thus, this is one of those rare times when we need to # use the lower-case runtime version check. my $page; my $url = 'http://www.koreabaseball.com/record/standings.asp'; # 순위 $page = LWP::Simple::get($url); $page =~ m{<tr class="style26">(.*?)<tr>}s; #정규식을 사용하여 목록을 저장 my $menu = $1; my @menu = $menu =~ m{>(.*?)</td>}g; sub _d { decode("cp-949", shift) }; #한글 사용을 위한 decode



my
$msg = Gtk2->check_version (2,2,0); die "This example requires gtk+ 2.2.0, but we're linked against " . join (".", Gtk2->get_version_info)."\n" . "$msg\n" if $msg; # add a new hidden column that holds a Gtk2::Gdk::Color. Gtk2::SimpleList->add_column_type( 'color', type => 'Gtk2::Gdk::Color', renderer => 'Gtk2::CellRendererText', attr => 'hidden', ); #배열의 값들을 순서대로 넣어서 객체를 생성 my $slist = Gtk2::SimpleList->new ( _d($menu[0]) => 'int', _d($menu[1]) => 'text', _d($menu[2]) => 'text', _d($menu[3]) => 'text', _d($menu[4]) => 'text', _d($menu[5]) => 'text', _d($menu[6]) => 'text', _d($menu[7]) => 'text', _d($menu[8]) => 'text', _d($menu[9]) => 'text', _d($menu[10]) => 'text', 'row color' => 'color', ); # add a color attribute to each column, getting the color information # from the hidden column. # foreach my $col ($slist->get_columns) { foreach my $cell ($col->get_cell_renderers) { $col->add_attribute ($cell, cell_background_gdk => 11); }#여기서 객체의 수를 잘 명시 해 주어야 한다. 이걸 안해줘서 고생... ㅡ.ㅠ }
# now put some data into the list; note the data for the hidden column. # we'll leave a couple of rows uncolored.

# 정규식을 사용하여 값을 읽어와서 출력
$page =~ m{<table width="715" border="0" cellpadding="0" cellspacing="1" bgcolor="dcdcdc">(.*?)</table>}s; my $str = $1; my @array = $str =~ m{<tr class="style9" bgcolor="#f9f9f9">(.*?)</tr>}sg; my $x=0; foreach my $str2 (@array) { my @array2 = $str2 =~ m{>(.*?)</td>}g; my $y = 0; foreach my $str3 (@array2){ if($y==0){ ${$slist->{data}}[$x] = $x+1; }else{ ${$slist->{data}}[$x]->[$y] = _d($str3); }
         $y++;	
	if($y==11 && $x%2){
	${$slist->{data}}[$x]->[$y]	= Gtk2::Gdk::Color->new (0xFFFF, 0xFFFF, 0xCCCC)
	} #짝수에만 베이지 색을 넣었음
  }
  $x++;
}
# the rest is uninteresting. my $win = Gtk2::Window->new; $win->set_title (_d('프로야구 순위 ')); $win->set_border_width (6); $win->signal_connect (delete_event => sub { #종료를 눌렀을 때 다이알로그 표시 my ($button, $url) = @_; my $message = Gtk2::MessageDialog->new ($button->get_toplevel, [], 'info', 'ok', _d("Perl_Mania\n\n"). _d("한국 프로야구 순위 출력\n"). _d("http://www.koreabaseball.com 를 참고\n"). _d("\n"). "-by H0ney" ); $message->run; $message->destroy; Gtk2->main_quit }); $win->add ($slist); $win->show_all; Gtk2->main;


원문의 코드가 짤리기 때문에... 링크 : http://codepad.org/kZj8bjXn



롯데가 결국 3위로  정규시즌을 마감했다. 조금만 더 힘을내면 좋았을껄.. 하는 아쉬움이 크다.

이번에는 한국시리즈 가야지~ 롯데 아자아자!



이건.. 뭐... 알림창 한번 사용해 보고 싶었다...라고 생각해 주시길;;

별 의미는 없지만.. 그래도 이뻐서~

누구나 이런거 만들잖아욧!  >.<




덤으로... 콘솔용 EXE 파일도 배포(?)

인자값으로 도움말이나 투수 & 타자 & 모두 를 넣어 보시길..





낙타 아이콘 이쁘다는...


'개발자 이야기 > Perl' 카테고리의 다른 글

음악을 들어보자! #2  (2) 2008.10.09
음악을 들어보자!  (1) 2008.10.08
Chapter 4. Getting Started  (3) 2008.10.07
Tutorial Availability & Introduction  (0) 2008.10.07
N번째로 발생하는 매치 찾기.  (1) 2008.10.07
Comments