-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·99 lines (72 loc) · 2.68 KB
/
app.py
File metadata and controls
executable file
·99 lines (72 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
import facebook
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
from selenium import webdriver
from PIL import Image
from config import Config
class Log:
def setLog(self, str):
f = open('./log/notification.txt', 'w')
f.write(str)
f.close()
def getLog(self):
f = open('./log/notification.txt', 'r+')
str = f.readline()
f.close()
return str
class ScrapKgsh:
def get_page(self, url):
# URL Load
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
res = urlopen(req)
res = res.read()
html = res.decode('utf-8')
return BeautifulSoup(html, 'html.parser')
def start(self, callback):
# 게임고 공지 홈페이지
base_url = "http://www.game.hs.kr/2013/"
notice_list_url = "inner.php?sMenu=G1000"
driver = webdriver.PhantomJS('./bin/mac/phantomjs')
# 페이지 가져오기
soup = self.get_page(url=base_url + notice_list_url)
# 최상단 요소 가져오
element = soup.select("#Con > div.boardnew2011 > div.table")
element_item = element[0].find_all('tr')[1]
# print(element_item)
notice_url = element_item.find('a')['href']
next_title = element_item.find('a')['title']
facebook_message = next_title
next_title = str(next_title.encode('utf-8'))
# 이전 값과 비교하기
prev_title = log.getLog()
if prev_title != next_title:
print('new post')
# 값 세팅
log.setLog(next_title)
# 글 작성하기
driver.get(base_url + notice_url)
form_location = driver.find_element_by_id('form_view').location
form_size = driver.find_element_by_id('form_view').size
driver.execute_script('document.body.style.background = "white"')
driver.save_screenshot('./img/cap.png')
driver.quit()
left = form_location['x'] - 35
top = form_location['y'] + 50
right = left + form_size['width'] + 65
bottom = top + form_size['height'] - 70
cap_image = Image.open('./img/cap.png')
cap_image = cap_image.crop((left, top, right, bottom))
cap_image.save('./img/cap.png')
# callback
callback(facebook_message)
else:
print('old post')
def put_facebook(message):
graph = facebook.GraphAPI(
access_token=config.getAccessToken())
graph.put_photo(image=open('./img/cap.png', 'rb'), message=message)
config = Config()
log = Log()
scrap = ScrapKgsh()
scrap.start(put_facebook)