-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
66 lines (51 loc) · 2.71 KB
/
app.py
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
from bs4 import BeautifulSoup
from urllib import request
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import pyperclip
import constant
import datetime
def get_latest_post(blog_id):
with request.urlopen(
"https://blog.naver.com/PostList.nhn?blogId={}&categoryNo=0&from=postList".format(blog_id)) as response:
html = response.read()
soup = BeautifulSoup(html, 'html.parser')
print(soup.find('h4', {'class': 'category_title pcol2'}).text, datetime.datetime.now())
return soup.find('h4', {'class': 'category_title pcol2'}).text
class Process:
def __init__(self):
self.driver = webdriver.Firefox(executable_path=constant.GECKO_DRIVER_PATH)
self.driver.implicitly_wait(3)
def clipboard_input(self, user_xpath, user_input):
temp_user_input = pyperclip.paste() # 사용자 클립보드를 따로 저장
pyperclip.copy(user_input)
self.driver.find_element_by_xpath(user_xpath).click()
ActionChains(self.driver).key_down(Keys.COMMAND).send_keys('v').key_up(Keys.COMMAND).perform()
pyperclip.copy(temp_user_input) # 사용자 클립보드에 저장 된 내용을 다시 가져 옴
def run(self):
self.driver.get('https://nid.naver.com/nidlogin.login')
self.driver.implicitly_wait(1)
self.clipboard_input('//*[@id="id"]', constant.NAVER_ID)
self.clipboard_input('//*[@id="pw"]', constant.NAVER_PW)
self.driver.find_element_by_xpath('//*[@id="log.login"]').click()
# 초기 값 셋팅
total_count_text = get_latest_post(constant.BLOG_ID)
sleep(constant.INTERVAL_SEC)
while True:
if constant.MAX_DATETIME < datetime.datetime.now():
break
if total_count_text == get_latest_post(constant.BLOG_ID):
print(constant.INTERVAL_SEC)
sleep(constant.INTERVAL_SEC)
else:
self.driver.get("https://blog.naver.com/PostList.nhn?blogId={}&categoryNo=0&from=postList".format(constant.BLOG_ID))
reply_button = self.driver.find_element(By.XPATH, "//div[@class='area_comment pcol2']")
reply_button.click()
reply_input = self.driver.find_element(By.XPATH, "//div[@class='u_cbox_text u_cbox_text_mention']")
self.driver.execute_script("arguments[0].innerHTML = arguments[1]", reply_input, constant.REPLY_CONTENT)
submit_btn = self.driver.find_element(By.XPATH, "//button[@class='u_cbox_btn_upload']")
submit_btn.click()
break;