본문 바로가기
Project

파이썬으로 영화 예매 오픈 알리미 만들기-2 [완료]

by 배애앰이 좋아 2020. 2. 11.
반응형

공부 및 참고 동영상 :

https://www.inflearn.com/course/%EC%98%81%ED%99%94%EC%98%88%EB%A7%A4-%ED%8C%8C%EC%9D%B4%EC%8D%AC#

 

파이썬으로 영화 예매 오픈 알리미 만들기 - 인프런

파이썬을 통해 웹 크롤링, 텔레그램 봇, AWS EC2를 이용하여 CGV 용산아이파크몰의 IMAX 영화 예매 오픈 알리미를 만들어보는 강좌입니다. 이 강좌를 통해, 웹 크롤링에 대한 기본적인 이해부터, 텔레그램 봇을 만들어서 서버로 구축하는 과정까지 학습하실 수 있습니다! 입문 초급 웹 개발 서버 프로그래밍 언어 서비스 개발 파이썬 온라인 강의 파이썬, 영화예매, 오픈알리미

www.inflearn.com

이 전에 한 부분들 : https://88-it.tistory.com/14

 

파이썬으로 영화 예매 오픈 알리미 만들기-1

공부 및 참고 동영상 : https://www.inflearn.com/course/%EC%98%81%ED%99%94%EC%98%88%EB%A7%A4-%ED%8C%8C%EC%9D%B4%EC%8D%AC# 파이썬으로 영화 예매 오픈 알리미 만들기 - 인프런 파이썬을 통해 웹 크롤링, 텔레..

88-it.tistory.com

저번에 텔레그램을 연결하는 것까지 진행하였는데요. 

bot.sendMessage(chat_id = 975825541, text = "test")

아래 코드를 통해서 원하는 메세지를 봇->사용자에게 보낼 수 있어요. 이때 chat_id는 보내고 싶은 사용자의 고유번호입니다. 이것을 전 크롤링 코드와 합치게 되면

import requests
import telegram
from bs4 import BeautifulSoup
bot = telegram.Bot(token = '텔레그램 고유번호')
url = 'http://www.cgv.co.kr/common/showtimes/iframeTheater.aspx?areacode=01&theatercode=0056&date=20200213'
html = requests.get(url)
soup = BeautifulSoup(html.text, 'html.parser')
title_list = soup.select('div.info-movie')
bot.sendMessage(chat_id = 975825541, text = "현재 있는 영화 목록")
for i in title_list:
    s = i.select('a > strong')
    bot.sendMessage(chat_id = 975825541, text = s)
now_open = soup.select_one('span.round.brown')
if(now_open):
    now_open = now_open.find_parent('div', class_ = 'col-times')
    title = now_open.select_one('div.info-movie > a > strong').text.strip()
    bot.sendMessage(chat_id = 사용자 고유번호, text = title + "예매중입니다.")
else:
    bot.sendMessage(chat_id = 사용자 고유번호, text = "모든 영화가 상영 중입니다.")

이렇게 코드가 됩니다. 이때에 "텔레그램 고유번호"와 "사용자 고유번호"에는 각자 맞는 고유번호를 넣으셔야합니다. (그대로 한글로 치시는 거 아닙니다.)

from apscheduler.schedulers.blocking import BlockingScheduler

sched = BlockingScheduler()
sched.add_job(job_function, 'interval', seconds = 30)
sched.start()

그리고 일정 시간마다 자동으로 반복시켜주는 코드를 추가하였습니다. 아쉽게도 제가 돌려보니깐 CGV 사이트 점검 들어갔더라고요...ㅠ 제대로 못 하고 계속 진행하게 되었습니다.

C:\Users\USER\Desktop\aws-key>ssh -i aws-key.pem ubuntu@18.191.158.46
nohup python3 Untitled12.py & # 컴이 꺼져도 자동으로 해당 파일 실행시키기
ssh -i aws-key.pem ubuntu@18.191.158.46  # cmd로 서버 접속하는 방법
ps -ef  # 서버 명령어 확인

그 뒤의 과정은 개인 정보가 너무 드러나는 바람에 추가하지 않았습니다. 결론적인 풀코드

import requests
import telegram
from bs4 import BeautifulSoup
from apscheduler.schedulers.blocking import BlockingScheduler
bot = telegram.Bot(token = '??')
url = 'http://www.cgv.co.kr/common/showtimes/iframeTheater.aspx?areacode=01&theatercode=0056&date=20200213'

def job_function():
    html = requests.get(url)
    soup = BeautifulSoup(html.text, 'html.parser')
    title_list = soup.select('div.info-movie')
    bot.sendMessage(chat_id = ??, text = "현재 있는 영화 목록")
    for i in title_list:
        s = i.select('a > strong')
        bot.sendMessage(chat_id = ??, text = s)
    now_open = soup.select_one('span.round.brown')
    if(now_open):
        now_open = now_open.find_parent('div', class_ = 'col-times')
        title = now_open.select_one('div.info-movie > a > strong').text.strip()
        bot.sendMessage(chat_id = ??, text = title + "예매중입니다.")
    else:
        bot.sendMessage(chat_id = ??, text = "모든 영화가 상영 중입니다.")

sched = BlockingScheduler()
sched.add_job(job_function, 'interval', seconds = 30)
sched.start()

동영상이 워낙 설명이 잘 되어있는지라 주석이 거의 없는.... 암튼 성공적으로 잘 마무리 되었습니다. 혹시나 궁금한 점 있으시면 댓글로 남겨주시면 답해드리겠습니다.

반응형

댓글