Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
d1ffuz0r
committed
Jan 5, 2012
0 parents
commit db93172
Showing
7 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
.idea | |||
*.pyc |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
__author__ = 'd1ffuz0r' | |||
from api import Api |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,76 @@ | |||
import json | |||
import urllib2 | |||
|
|||
|
|||
class Post(object): | |||
def __init__(self, post): | |||
self.lasthit = post[u"lasthit"] | |||
self.comment = post[u"comment"] | |||
self.name = post[u"name"] | |||
self.parent = post[u"parent"] | |||
self.timestamp = post[u"timestamp"] | |||
self.banned = post[u"banned"] | |||
self.sticky = post[u"sticky"] | |||
self.height = post[u"height"] | |||
self.width = post[u"width"] | |||
self.num = post[u"num"] | |||
self.video = post[u"video"] | |||
self.tn_height = post[u"tn_height"] | |||
self.closed = post[u"closed"] | |||
self.tn_width = post[u"tn_width"] | |||
self.date = post[u"date"] | |||
self.subject = post[u"subject"] | |||
self.image = post[u"image"] | |||
self.thumbnail = post[u"thumbnail"] | |||
self.op = post[u"op"] | |||
self.size = post[u"size"] | |||
|
|||
def __repr__(self): | |||
return "<Post: {num}>".format(num=self.num) | |||
|
|||
|
|||
class Thread(object): | |||
def __init__(self, thread): | |||
self.reply_count = int(thread[u"reply_count"]) | |||
self.posts = [Post(post[0]) for post in thread[u"posts"]] | |||
|
|||
def __repr__(self): | |||
return "<Thread: {num}>".format(num=self.posts[0].num) | |||
|
|||
|
|||
class Api(object): | |||
def __init__(self): | |||
self.board = None | |||
self._url = "http://2ch.so/" | |||
|
|||
def get_threads(self, board=None): | |||
""" | |||
Get all threads from :board: | |||
@param board: code of board | |||
@return dict: list threads on board | |||
""" | |||
if self.board is None: | |||
self.board = board | |||
threads = self._get(url="wakaba.json")[u'threads'] | |||
return [Thread(thread) for thread in threads] | |||
|
|||
def _get(self, url): | |||
""" | |||
Get page | |||
@param url: | |||
""" | |||
if not self.board: | |||
raise ValueError("Board is not selected") | |||
else: | |||
return json.load( | |||
urllib2.urlopen(self._url + self.board + "/" + url) | |||
) | |||
|
|||
def get_thread(self, thread): | |||
""" | |||
Get thread | |||
@param thread: id of thread | |||
@return: dict | |||
""" | |||
posts = self._get("res/" + str(thread) + ".json")[u"thread"] | |||
return [Post(post[0]) for post in posts] |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
__author__ = 'd1ffuz0r' |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,37 @@ | |||
from api import Api, Thread, Post | |||
import unittest | |||
import os | |||
|
|||
class MyTestCase(unittest.TestCase): | |||
|
|||
def setUp(self): | |||
self.api = Api() | |||
self.api._url = "file:///" + os.path.join(os.path.dirname(__file__)) + "/" | |||
self.api.board = "pr" | |||
|
|||
def test_api(self): | |||
self.assertTrue(isinstance(self.api, object)) | |||
|
|||
def test_set_board(self): | |||
self.assertEqual(self.api.board, "pr") | |||
|
|||
def test_get_board_true(self): | |||
threads = self.api.get_threads() | |||
thread = threads[0] | |||
self.assertTrue(isinstance(threads, list)) | |||
self.assertTrue(isinstance(thread, Thread)) | |||
self.assertEqual(thread.__repr__(), "<Thread: 87848>") | |||
|
|||
def test_get_board_false(self): | |||
self.api.board = None | |||
self.assertRaises(ValueError, self.api.get_threads) | |||
|
|||
def test_get_thread_true(self): | |||
posts = self.api.get_thread(87848) | |||
post = posts[0] | |||
self.assertTrue(isinstance(posts, list)) | |||
self.assertTrue(isinstance(post, Post)) | |||
self.assertEqual(post.__repr__(), "<Post: 87848>") | |||
|
|||
if __name__ == '__main__': | |||
unittest.main() |