Header

  1. View current page

    종텐의 작업실

Profile_img_60x60_01
1

weather2sms

weather2sms.py (version: 2008.08.06)

Daum 날씨 서비스의 오늘 날씨를 문자로 보내주는 프로그램입니다.

파이썬으로 작성되었습니다.

*nix계열의 서버면, crontab에 등록하시면, 특정 시간마다 그날그날 문자로 받을 수 있습니다.

윈도 서버라면 at 명령어에 등록하면 됩니다. ㅋㅋㅋㅋㅋㅋㅋ

 

 

라이센스

Copyleft, allrights are FREE!!...입니다. -_-;;

 

 

다운로드

★ 다운로드: weather2sms.py

(아래쪽에 붙여놓은 소스코드와 이 파일의 내용은 동일함.)

 

 

준비사항

파이썬 2.4 버전 이상에서 정상 작동합니다!!

2.3에선 CJK코덱을 깔아도 안되더군요. -_-

 

이 프로그램은 문자천국(http://skysms.co.kr)을 이용합니다.

여기에 가입하셔야 합니다. -_-;;;;;;;;;;

 

문자천국은

유료는 아무때나 보낼 수 있고,

무료는, 점심 12시, 오후 6시에 각각 선착순 3천통이 무료입니다. (개인당은 하루 5건)

이 프로그램은 문자천국의 유료모드와, 무료모드를 모두 지원합니다.

무료모드는 당연히, 점심 12시, 오후 6시...라는 시간에 맞춰 실행해야 합니다.

그 이외의 시간에 실행하면.. 씹히더군요? orz

 

이 프로그램을 실행하려면,

Beautiful Soup 라이브러리와,

Twill 라이브러리가 필요합니다.

다운로드해서 같은 폴더에 넣어주거나, 파이썬의 라이브러리 폴더에 넣어야 합니다.

 

 

사용법! (Usage)

[무료모드] (점심 12시와, 오후 6시에 선착순 3천통.. 개인당은 하루에 5통)

프롬프트> python weather2sms.py -f "아이디;비번;전화번호"

 

여러개도 됩니다.

프롬프트> python weather2sms.py -f "아이디1;비번1;전화번호1" "아이디2;비번2;전화번호2"

 

[유료모드] (문자천국에서 문자 쿠폰을 구입한 경우..)

프롬프트> python weather2sms.py -F "아이디;비번;전화번호"

// 당연히, 유료모드도 여러개 됩니다.

★ 이 프로그램의 소스파일의 인코딩이 UTF-8이 아니면, 에러 납니다. -_-;

★ 옵션 없으면 안됩니다. -_-;;;;;;;; (에러처리 그런거 없음.)

 

 

전체 소스코드 (UTF-8로 저장해야합니다. 위의 다운로드와 같은 내용입니다.)
  1. #!/usr/bin/python
    # -*- coding: utf-8 -*-

    # weather2sms.py
    # Daum 사이트에서, 오늘 날씨를 문자로 보내주는 프로그램
    # site : http://jong10.springnote.com/pages/1590644
    # version: 2008.08.06.a
    # coded by jong10

    # need: http://twill.idyll.org/
    # need: http://www.crummy.com/software/BeautifulSoup/

    import sys
    import re
    import urllib2
    import codecs

    from twill.commands import *
    from BeautifulSoup import BeautifulSoup

    print '-=+=-=+=< weather2sms.py >=+=-=+=-'

    if len(sys.argv) <= 2:
        print 'Usage: python food2sms.py -(f|F) ["id;pass;phonenum"]...'
        # -f : free mode
        # -F : non-free mode
        exit(1)

    if sys.argv[1] == '-f':
        url = 'http://skysms.co.kr/send_event/'
        index = 9
    elif sys.argv[1] == '-F':
        url = 'http://skysms.co.kr/send_s/'
        index = 14
    else:
        print "option is '-f' or '-F' only!"
        exit(2)

    ####################################################

    html = urllib2.urlopen('http://weather.media.daum.net/where/11B00000/11B10101.html').read()
    html = html.decode('euc-kr', 'ignore').encode('utf-8')

    p = re.compile('<.*?>|\n')
    today_weather = ""

    soup = BeautifulSoup(html)

    # 날짜
    list = soup.fetch('td', {'class':'d_11_03036f'})
    today_weather += p.sub('', str(list[0]).decode('utf-8', 'ignore').encode('euc-kr')) + '\n'

    # 날씨
    list = soup.fetch('td', {'class':'g_12_03036f'})
    today_weather += p.sub('', str(list[0]).decode('utf-8', 'ignore').encode('euc-kr')) + '\n'

    # 예상기온, 강수확률
    list = soup.fetch('td', {'class':'g_12_3f3f3f'})
    today_weather += p.sub('', str(list[0]).decode('utf-8', 'ignore').encode('euc-kr')) + '\n'
    today_weather += str("강수 : ").decode('utf-8', 'ignore').encode('euc-kr')
    today_weather += p.sub('', str(list[1]).decode('utf-8', 'ignore').encode('euc-kr'))

    print today_weather

    ####################################################

    args = sys.argv[2:]
    for arg in args:
        arg = arg.split(';')
        userid = arg[0]
        passwd = arg[1]
        phone  = arg[2]
       
        go(url)
        formvalue('login_form', 'userid', userid)
        formvalue('login_form', 'passwd', passwd)
        submit()
       
        go(url)
        formvalue('msg_form', str(index), phone)
        formvalue('msg_form', 'body', today_weather)
        submit()
       
        go('http://skysms.co.kr/login/logout.php')

 

 

동작 원리

실행을 하면..
http://weather.media.daum.net/where/11B00000/11B10101.html 에 접속을 해서..
Beautiful Soup 라이브러리로 html을 파싱해서..
오늘날씨에 해당하는 항목들을 가져오고,
Twill 라이브러리를 사용해서..
문자천국(무료:http://skysms.co.kr/send_event/ , 유료: http://skysms.co.kr/send_s/ )에 접속을 해서,
id, passwd 를 입력해서 로그인하고,
내용에 채우고, 받는 사람 번호에 해당 번호를 써주고..
보내기 버튼을 클릭해줌.

 

 

후기

사실, 지난번에 만들었던, food2sms.py를 그냥 몇줄만 수정만 한거라.. 30분도 안 걸렸네요. -_-;

귀찮아서 계속 안 하고 있었는데, 진작 할껄. ㅎㅎ

 

-- 망상을 달리는 프로그래머, Jong10 /(_^_)\

History

Last edited on 08/06/2008 13:00 by jong10

Comments (0)

You must log in to leave a comment. Please sign in.