Last active
August 31, 2020 18:07
-
-
Save yoonbae81/d85e45b51f20170fc0bda9396f65291a to your computer and use it in GitHub Desktop.
클리앙 중고장터 모니터링
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
### Configuration ################################################ | |
CLIEN_USERID = 'XXXXXXXX' | |
CLIEN_PASSWD = 'XXXXXXXX' | |
TELEGRAM_TOKEN = '000000000:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' | |
TELEGRAM_CHAT_ID = '000000000' | |
### End of Configuration ######################################### | |
import argparse | |
import re | |
import requests | |
import sys | |
import os | |
import tempfile | |
from bs4 import BeautifulSoup | |
def login(session, userid, passwd): | |
res = session.get('https://m.clien.net/service/auth/login') | |
bs = BeautifulSoup(res.text, 'html.parser') | |
params = {'userId': userid, | |
'userPassword': passwd, | |
'_csrf': bs.find('input', {'name': '_csrf'}).get('value')} | |
res = session.post('https://m.clien.net/service/login', params=params) | |
if res.text.find('확인하세요') > 0: | |
raise PermissionError | |
def fetch(session, keyword): | |
params = {'sk': 'title', 'sv': keyword} | |
res = session.get('https://m.clien.net/service/search/board/sold', params=params) | |
bs = BeautifulSoup(res.text, 'html.parser') | |
item = bs.find_all('a', class_='list_subject')[0] | |
return 'https://m.clien.net' + item['href'], item.get_text().strip() | |
def is_new(keyword, link): | |
pattern = re.compile('.+sold/([0-9]+)\?.+') | |
article_id = pattern.match(link)[1] | |
filepath = os.path.join(tempfile.gettempdir(), 'clien-{}'.format(keyword)) | |
try: | |
file = open(filepath, 'r') | |
written_id = file.read() | |
except FileNotFoundError: | |
written_id = -1 | |
with open(filepath, 'w') as file: | |
file.write(article_id) | |
return article_id != written_id | |
def notify(link, title): | |
data = { 'chat_id': TELEGRAM_CHAT_ID, | |
'text': '<a href="{}">{}</a>'.format(link, title), | |
'parse_mode': 'HTML' } | |
res = requests.post("https://api.telegram.org/bot{}/sendMessage".format(TELEGRAM_TOKEN), data=data) | |
if res.status_code != 200: | |
raise RuntimeError | |
def main(keyword): | |
session = requests.session() | |
try: | |
login(session, CLIEN_USERID, CLIEN_PASSWD) | |
link, title = fetch(session, keyword) | |
if (is_new(keyword, link) and | |
'삽니다' not in title and '구매' not in title): | |
notify(link, title) | |
print('Notified: ' + title) | |
else: | |
print('Nothing new') | |
except PermissionError: | |
print('Login failed', file=sys.stderr) | |
sys.exit(1) | |
except IndexError: | |
print('Not found', file=sys.stderr) | |
sys.exit(1) | |
except RuntimeError: | |
print('Telegram error', file=sys.stderr) | |
sys.exit(1) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('keyword', nargs='*') | |
args = parser.parse_args() | |
main(' '.join(args.keyword)) | |
''' codelet for debug | |
import easydict | |
args = easydict.EasyDict({'keyword': 'XXXX'}) | |
keyword = 'XXXX' | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment