Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SLRCLUB 중고장터 모니터링
#!/usr/bin/env python3
### Configuration ################################################
SLRCLUB_USERID = 'XXXXXXXX'
SLRCLUB_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
from requests.packages.urllib3.exceptions import InsecureRequestWarning
def login(session, userid, passwd):
params = {'user_id': userid, 'password': passwd}
res = session.post('https://www.slrclub.com/login/process.php', data=params, verify=False)
if res.text.find('실패') > 0:
raise PermissionError
def fetch(session, keyword):
params = {'setsearch': 'subject', 'keyword': keyword}
res = session.get('http://m.slrclub.com/l/used_market', params=params)
bs = BeautifulSoup(res.text, 'html.parser')
item = bs.find_all('div', class_='article')[0].a
return 'http://m.slrclub.com' + item['href'], item.get_text().strip()
def is_new(keyword, link):
pattern = re.compile('.+used_market/([0-9]+)\?.+')
article_id = pattern.match(link)[1]
filepath = os.path.join(tempfile.gettempdir(), 'slrclub-{}'.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):
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
session = requests.session()
try:
login(session, SLRCLUB_USERID, SLRCLUB_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()
# print(args)
main(' '.join(args.keyword))
''' codelet
import easydict
args = easydict.EasyDict({'keyword': 'D4'})
userid = SLRCLUB_USERID
passwd = SLRCLUB_PASSWD
keyword = 'D4'
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.