Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. from urllib.request import urlopen
  5. from urllib.error import URLError
  6. import logging
  7. import re
  8. import time
  9.  
  10. coding = 'sjis'
  11.  
  12. def main():
  13.     while True:
  14.         check(server='vipper.2ch.net', ita='news4vip', titlePattern=re.compile('プログラミング'))
  15.         time.sleep(60)
  16.  
  17. def check(server, ita, titlePattern):
  18.     ''' 2chのスレッド一覧を取得し、スレタイがtitlePatternに一致するスレを見つけたらそのスレのURLを表示する '''
  19.     subbackUrl = 'https://{}/{}/subback.html'.format(server, ita)
  20.     threadUrl = 'http://{}/{}/test/read.cgi/{}'
  21.     try:
  22.         req = urlopen(subbackUrl)
  23.         html = req.read().decode(coding, 'replace')
  24.     except URLError:
  25.         return 1
  26.  
  27.     ''' line = "<a href="スレッドID/l50">番号: スレタイ</a>" '''
  28.     linePattern = re.compile('<a href="(?P<thread>[0-9]+)/l50">[0-9]+: (?P<title>.*)</a>')
  29.     for line in html.splitlines():
  30.         matchObj = linePattern.match(line)
  31.         if not matchObj:
  32.             continue
  33.         threadId = matchObj.group('thread')
  34.         title = matchObj.group('title')
  35.         if titlePattern.search(title):
  36.             print(threadUrl.format(server, ita, threadId))
  37.  
  38. if __name__ == '__main__':
  39.     exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement