+from contextlib import closing
+from bs4 import BeautifulSoup as BS4
+URLTEMPLATE = (r'http://www.pathofexile.com/forum/view-forum'
+ r'/%(forumid)s/page/%(page)s')
+DBFILE = os.path.abspath(os.path.join(
+ os.path.dirname(__file__), os.path.splitext(__file__)[0] + '.db',
+if os.path.exists(DBFILE):
+dbi = sqlite3.connect(DBFILE)
+with closing(dbi.cursor()) as csr:
+create table if not exists threads(
+ views integer not null,
+ replies integer not null,
+ 'create index if not exists threads_idx_forum on threads(forum);')
+ 'create index if not exists threads_idx_title on threads(title);')
+ 'create index if not exists threads_idx_views on threads(views);')
+ 'create index if not exists threads_idx_replies on threads(replies);')
+ 'create index if not exists threads_idx_page on threads(page);')
+for forum, forumid in FORUMS.items():
+ url = URLTEMPLATE % {'page': pageno, 'forumid': forumid}
+ body = requests.get(url).text
+ if 'No Threads' in body:
+ table = soup.find('table', id='view_forum_table')
+ for row in table.find_all('tr'):
+ thread = row.find('td', class_='thread')
+ link = thread.find(class_='title').find('a')
+ if url.startswith('/'):
+ url = 'http://www.pathofexile.com%s' % url
+ views = int(row.find('td', class_='views').text)
+ replies = int(row.find('td', class_='replies').text)
+ with closing(dbi.cursor()) as csr:
+ 'insert into threads values (?, ?, ?, ?, ?, ?)',
+ (forum, title, views, replies, pageno, url))
+ print 'FORUM %s: PAGE %s' % (forum, pageno)
+with closing(dbi.cursor()) as csr: