{"action":"created","comment":{"url":"https://api.github.com/repos/xiachufang/bandit-client/pulls/comments/74873402","id":74873402,"diff_hunk":"@@ -51,59 +35,99 @@ def clear(self):\n \n class BanditClientInstance(object):\n \n-    def __init__(self, bc):\n+    def __init__(self, bc, timeout=5, max_queue_length=100, max_seconds=86400):\n         self.host_info = bc\n+        self.timeout = timeout\n+        self.max_queue_length = max_queue_length\n+        self.max_seconds = max_seconds\n         self.click_queue = BanditSendQueue(\"%s/api/click.json\" % self.host_info.host)\n         self.show_queue = BanditSendQueue(\"%s/api/show.json\" % self.host_info.host)\n         self.adjust_url = \"%s/api/adjust.json\" % self.host_info.host\n-        self.max_queue_length = bc.max_queue_length\n-        self.max_seconds = bc.max_seconds\n+        self._session = None\n \n     def __del__(self):\n         if self.click_queue.queue:\n-            self.push(self.click_queue)\n+            self.send(self.click_queue)\n         if self.show_queue.queue:\n-            self.push(self.show_queue)\n+            self.send(self.show_queue)\n+\n+    def sign(self, d, salt=None):\n+        md5_str = ''\n+        for key, value in sorted(d.items(), key=lambda x: x[0]):\n+            if isinstance(value, unicode):\n+                value = value.encode('utf8')\n+            else:\n+                value = str(value)\n+            if isinstance(value, str):\n+                md5_str += \"%s%s\" % (key, value)\n+        md5_str += str(self.host_info.secret_key)\n+        sign = hashlib.md5(md5_str).hexdigest()\n+        return sign\n+\n+    @property\n+    def session(self):\n+        if not self._session:\n+            self._session = requests.session()\n+        return self._session\n+\n+    def post(self, *args, **kwargs):\n+        return self.session.post(*args, timeout=self.timeout, **kwargs)\n+\n+    def get(self, *args, **kwargs):\n+        return self.session.get(*args, timeout=self.timeout, **kwargs)\n+\n+    def verify(self, d):\n+        sign = d.pop('signature')\n+        if sign == self.sign(d):\n+            return True\n+        return False\n \n     def _signature(self, data):\n-        return dict(zip(data.keys() + ['signature'],\n-                        data.values() + [compute_sign(data, self.host_info.secret_key)]))\n-\n-    def click(self, query, query_time, target, sk):\n-        if type(query_time) != str:\n-            query_time = query_time.strftime(\"%Y-%m-%d %H:%M:%S\")\n-        self.add_queue(self.click_queue, {'query': query,\n-                                     'query_time': query_time,\n-                                     'target': target,\n-                                     'session_key': sk})\n-\n-    def show(self, query, query_time, target, sk):\n-        if type(query_time) != str:\n-            query_time = query_time.strftime(\"%Y-%m-%d %H:%M:%S\")\n-        self.add_queue(self.show_queue, {'query': query,\n-                                    'query_time': query_time,\n-                                    'target': target,\n-                                    'session_key': sk})\n-\n-    def push(self, q):\n+        return dict(zip(data.keys() + ['signature'], data.values() + [self.sign(data)]))\n+\n+    def _strftime(self, date_time):\n+        if type(datetime) == str:\n+            return date_time\n+        return date_time.strftime(\"%Y-%m-%d %H:%M:%S\")\n+\n+    def click(self, list_id, click_time, target, sk):\n+        self.add_queue(self.click_queue, {'list_id': list_id,\n+                                          'query_time': self._strftime(click_time),\n+                                          'target': target,\n+                                          'session_key': sk})\n+\n+    def show(self, list_id, click_time, shows, sk):\n+        self.add_queue(self.show_queue, {'list_id': list_id,\n+                                         'query_time': self._strftime(click_time),\n+                                         'shows': shows,\n+                                         'session_key': sk})\n+\n+    def send(self, q):\n         data = {'content': simplejson.dumps(q.queue),\n                 'total': len(q.queue),\n                 'public_key': self.host_info.public_key}\n-        ret = requests.post(q.url, self._signature(data)).json()\n-        if ret['status'] != 'ok':\n-            raise PostError(ret)\n-        q.clear()\n+\n+        resp = self.post(q.url, parms=self._signature(data))\n+        if resp.status_code == requests.codes.ok:\n+            return resp.json()['content']\n+        raise BanditApiError(resp.text)\n \n     def add_queue(self, q, data):\n         q.queue.append(data)\n         if len(q.queue) == self.max_queue_length or (datetime.datetime.now() - q.last_send_time).seconds > self.max_seconds:\n-            self.push(q)\n+            self.send(q)\n \n-    def adjust(self, hits, query):\n-        data = {'hits': simplejson.dumps(hits),\n-                'query': query,\n+    def adjust(self, list_id, scores, limit=20, offset=15, **kwargs):\n+        data = {'list_id': list_id,\n+                'scores': simplejson.dumps(scores),\n+                'offset': offset,\n+                'limit': limit,","path":"bandit_client/client.py","position":166,"original_position":166,"commit_id":"3cac0b87359b01bd20bc0f7633fd61b1917d1edd","original_commit_id":"3cac0b87359b01bd20bc0f7633fd61b1917d1edd","user":{"login":"fjkfwz","id":8098261,"avatar_url":"https://avatars.githubusercontent.com/u/8098261?v=3","gravatar_id":"","url":"https://api.github.com/users/fjkfwz","html_url":"https://github.com/fjkfwz","followers_url":"https://api.github.com/users/fjkfwz/followers","following_url":"https://api.github.com/users/fjkfwz/following{/other_user}","gists_url":"https://api.github.com/users/fjkfwz/gists{/gist_id}","starred_url":"https://api.github.com/users/fjkfwz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fjkfwz/subscriptions","organizations_url":"https://api.github.com/users/fjkfwz/orgs","repos_url":"https://api.github.com/users/fjkfwz/repos","events_url":"https://api.github.com/users/fjkfwz/events{/privacy}","received_events_url":"https://api.github.com/users/fjkfwz/received_events","type":"User","site_admin":false},"body":"搜索在一段时间内都不会用这个来调权吧 需要考虑一下其他列表","created_at":"2016-08-16T04:04:33Z","updated_at":"2016-08-16T04:04:33Z","html_url":"https://github.com/xiachufang/bandit-client/pull/3#discussion_r74873402","pull_request_url":"https://api.github.com/repos/xiachufang/bandit-client/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/xiachufang/bandit-client/pulls/comments/74873402"},"html":{"href":"https://github.com/xiachufang/bandit-client/pull/3#discussion_r74873402"},"pull_request":{"href":"https://api.github.com/repos/xiachufang/bandit-client/pulls/3"}}},"pull_request":{"url":"https://api.github.com/repos/xiachufang/bandit-client/pulls/3","id":81268622,"html_url":"https://github.com/xiachufang/bandit-client/pull/3","diff_url":"https://github.com/xiachufang/bandit-client/pull/3.diff","patch_url":"https://github.com/xiachufang/bandit-client/pull/3.patch","issue_url":"https://api.github.com/repos/xiachufang/bandit-client/issues/3","number":3,"state":"open","locked":false,"title":"add timeout","user":{"login":"fjkfwz","id":8098261,"avatar_url":"https://avatars.githubusercontent.com/u/8098261?v=3","gravatar_id":"","url":"https://api.github.com/users/fjkfwz","html_url":"https://github.com/fjkfwz","followers_url":"https://api.github.com/users/fjkfwz/followers","following_url":"https://api.github.com/users/fjkfwz/following{/other_user}","gists_url":"https://api.github.com/users/fjkfwz/gists{/gist_id}","starred_url":"https://api.github.com/users/fjkfwz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fjkfwz/subscriptions","organizations_url":"https://api.github.com/users/fjkfwz/orgs","repos_url":"https://api.github.com/users/fjkfwz/repos","events_url":"https://api.github.com/users/fjkfwz/events{/privacy}","received_events_url":"https://api.github.com/users/fjkfwz/received_events","type":"User","site_admin":false},"body":"@yangyangxcf @kebinwang 接口改了下","created_at":"2016-08-15T04:16:01Z","updated_at":"2016-08-16T04:04:33Z","closed_at":null,"merged_at":null,"merge_commit_sha":"70474fbe9d8603e3c3f34e0eb321d2d4dade40cd","assignee":{"login":"yangyangxcf","id":17398258,"avatar_url":"https://avatars.githubusercontent.com/u/17398258?v=3","gravatar_id":"","url":"https://api.github.com/users/yangyangxcf","html_url":"https://github.com/yangyangxcf","followers_url":"https://api.github.com/users/yangyangxcf/followers","following_url":"https://api.github.com/users/yangyangxcf/following{/other_user}","gists_url":"https://api.github.com/users/yangyangxcf/gists{/gist_id}","starred_url":"https://api.github.com/users/yangyangxcf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yangyangxcf/subscriptions","organizations_url":"https://api.github.com/users/yangyangxcf/orgs","repos_url":"https://api.github.com/users/yangyangxcf/repos","events_url":"https://api.github.com/users/yangyangxcf/events{/privacy}","received_events_url":"https://api.github.com/users/yangyangxcf/received_events","type":"User","site_admin":false},"assignees":[{"login":"yangyangxcf","id":17398258,"avatar_url":"https://avatars.githubusercontent.com/u/17398258?v=3","gravatar_id":"","url":"https://api.github.com/users/yangyangxcf","html_url":"https://github.com/yangyangxcf","followers_url":"https://api.github.com/users/yangyangxcf/followers","following_url":"https://api.github.com/users/yangyangxcf/following{/other_user}","gists_url":"https://api.github.com/users/yangyangxcf/gists{/gist_id}","starred_url":"https://api.github.com/users/yangyangxcf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yangyangxcf/subscriptions","organizations_url":"https://api.github.com/users/yangyangxcf/orgs","repos_url":"https://api.github.com/users/yangyangxcf/repos","events_url":"https://api.github.com/users/yangyangxcf/events{/privacy}","received_events_url":"https://api.github.com/users/yangyangxcf/received_events","type":"User","site_admin":false}],"milestone":null,"commits_url":"https://api.github.com/repos/xiachufang/bandit-client/pulls/3/commits","review_comments_url":"https://api.github.com/repos/xiachufang/bandit-client/pulls/3/comments","review_comment_url":"https://api.github.com/repos/xiachufang/bandit-client/pulls/comments{/number}","comments_url":"https://api.github.com/repos/xiachufang/bandit-client/issues/3/comments","statuses_url":"https://api.github.com/repos/xiachufang/bandit-client/statuses/3cac0b87359b01bd20bc0f7633fd61b1917d1edd","head":{"label":"fjkfwz:add_timeout","ref":"add_timeout","sha":"3cac0b87359b01bd20bc0f7633fd61b1917d1edd","user":{"login":"fjkfwz","id":8098261,"avatar_url":"https://avatars.githubusercontent.com/u/8098261?v=3","gravatar_id":"","url":"https://api.github.com/users/fjkfwz","html_url":"https://github.com/fjkfwz","followers_url":"https://api.github.com/users/fjkfwz/followers","following_url":"https://api.github.com/users/fjkfwz/following{/other_user}","gists_url":"https://api.github.com/users/fjkfwz/gists{/gist_id}","starred_url":"https://api.github.com/users/fjkfwz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fjkfwz/subscriptions","organizations_url":"https://api.github.com/users/fjkfwz/orgs","repos_url":"https://api.github.com/users/fjkfwz/repos","events_url":"https://api.github.com/users/fjkfwz/events{/privacy}","received_events_url":"https://api.github.com/users/fjkfwz/received_events","type":"User","site_admin":false},"repo":{"id":64359424,"name":"bandit-client","full_name":"fjkfwz/bandit-client","owner":{"login":"fjkfwz","id":8098261,"avatar_url":"https://avatars.githubusercontent.com/u/8098261?v=3","gravatar_id":"","url":"https://api.github.com/users/fjkfwz","html_url":"https://github.com/fjkfwz","followers_url":"https://api.github.com/users/fjkfwz/followers","following_url":"https://api.github.com/users/fjkfwz/following{/other_user}","gists_url":"https://api.github.com/users/fjkfwz/gists{/gist_id}","starred_url":"https://api.github.com/users/fjkfwz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fjkfwz/subscriptions","organizations_url":"https://api.github.com/users/fjkfwz/orgs","repos_url":"https://api.github.com/users/fjkfwz/repos","events_url":"https://api.github.com/users/fjkfwz/events{/privacy}","received_events_url":"https://api.github.com/users/fjkfwz/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/fjkfwz/bandit-client","description":null,"fork":true,"url":"https://api.github.com/repos/fjkfwz/bandit-client","forks_url":"https://api.github.com/repos/fjkfwz/bandit-client/forks","keys_url":"https://api.github.com/repos/fjkfwz/bandit-client/keys{/key_id}","collaborators_url":"https://api.github.com/repos/fjkfwz/bandit-client/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/fjkfwz/bandit-client/teams","hooks_url":"https://api.github.com/repos/fjkfwz/bandit-client/hooks","issue_events_url":"https://api.github.com/repos/fjkfwz/bandit-client/issues/events{/number}","events_url":"https://api.github.com/repos/fjkfwz/bandit-client/events","assignees_url":"https://api.github.com/repos/fjkfwz/bandit-client/assignees{/user}","branches_url":"https://api.github.com/repos/fjkfwz/bandit-client/branches{/branch}","tags_url":"https://api.github.com/repos/fjkfwz/bandit-client/tags","blobs_url":"https://api.github.com/repos/fjkfwz/bandit-client/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/fjkfwz/bandit-client/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/fjkfwz/bandit-client/git/refs{/sha}","trees_url":"https://api.github.com/repos/fjkfwz/bandit-client/git/trees{/sha}","statuses_url":"https://api.github.com/repos/fjkfwz/bandit-client/statuses/{sha}","languages_url":"https://api.github.com/repos/fjkfwz/bandit-client/languages","stargazers_url":"https://api.github.com/repos/fjkfwz/bandit-client/stargazers","contributors_url":"https://api.github.com/repos/fjkfwz/bandit-client/contributors","subscribers_url":"https://api.github.com/repos/fjkfwz/bandit-client/subscribers","subscription_url":"https://api.github.com/repos/fjkfwz/bandit-client/subscription","commits_url":"https://api.github.com/repos/fjkfwz/bandit-client/commits{/sha}","git_commits_url":"https://api.github.com/repos/fjkfwz/bandit-client/git/commits{/sha}","comments_url":"https://api.github.com/repos/fjkfwz/bandit-client/comments{/number}","issue_comment_url":"https://api.github.com/repos/fjkfwz/bandit-client/issues/comments{/number}","contents_url":"https://api.github.com/repos/fjkfwz/bandit-client/contents/{+path}","compare_url":"https://api.github.com/repos/fjkfwz/bandit-client/compare/{base}...{head}","merges_url":"https://api.github.com/repos/fjkfwz/bandit-client/merges","archive_url":"https://api.github.com/repos/fjkfwz/bandit-client/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/fjkfwz/bandit-client/downloads","issues_url":"https://api.github.com/repos/fjkfwz/bandit-client/issues{/number}","pulls_url":"https://api.github.com/repos/fjkfwz/bandit-client/pulls{/number}","milestones_url":"https://api.github.com/repos/fjkfwz/bandit-client/milestones{/number}","notifications_url":"https://api.github.com/repos/fjkfwz/bandit-client/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/fjkfwz/bandit-client/labels{/name}","releases_url":"https://api.github.com/repos/fjkfwz/bandit-client/releases{/id}","deployments_url":"https://api.github.com/repos/fjkfwz/bandit-client/deployments","created_at":"2016-07-28T02:58:02Z","updated_at":"2016-07-18T04:11:43Z","pushed_at":"2016-08-15T11:20:51Z","git_url":"git://github.com/fjkfwz/bandit-client.git","ssh_url":"git@github.com:fjkfwz/bandit-client.git","clone_url":"https://github.com/fjkfwz/bandit-client.git","svn_url":"https://github.com/fjkfwz/bandit-client","homepage":null,"size":6,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"xiachufang:master","ref":"master","sha":"45284eb5a9f056b8649146e2cf9187827cb203c6","user":{"login":"xiachufang","id":990080,"avatar_url":"https://avatars.githubusercontent.com/u/990080?v=3","gravatar_id":"","url":"https://api.github.com/users/xiachufang","html_url":"https://github.com/xiachufang","followers_url":"https://api.github.com/users/xiachufang/followers","following_url":"https://api.github.com/users/xiachufang/following{/other_user}","gists_url":"https://api.github.com/users/xiachufang/gists{/gist_id}","starred_url":"https://api.github.com/users/xiachufang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xiachufang/subscriptions","organizations_url":"https://api.github.com/users/xiachufang/orgs","repos_url":"https://api.github.com/users/xiachufang/repos","events_url":"https://api.github.com/users/xiachufang/events{/privacy}","received_events_url":"https://api.github.com/users/xiachufang/received_events","type":"Organization","site_admin":false},"repo":{"id":63569541,"name":"bandit-client","full_name":"xiachufang/bandit-client","owner":{"login":"xiachufang","id":990080,"avatar_url":"https://avatars.githubusercontent.com/u/990080?v=3","gravatar_id":"","url":"https://api.github.com/users/xiachufang","html_url":"https://github.com/xiachufang","followers_url":"https://api.github.com/users/xiachufang/followers","following_url":"https://api.github.com/users/xiachufang/following{/other_user}","gists_url":"https://api.github.com/users/xiachufang/gists{/gist_id}","starred_url":"https://api.github.com/users/xiachufang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xiachufang/subscriptions","organizations_url":"https://api.github.com/users/xiachufang/orgs","repos_url":"https://api.github.com/users/xiachufang/repos","events_url":"https://api.github.com/users/xiachufang/events{/privacy}","received_events_url":"https://api.github.com/users/xiachufang/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/xiachufang/bandit-client","description":null,"fork":false,"url":"https://api.github.com/repos/xiachufang/bandit-client","forks_url":"https://api.github.com/repos/xiachufang/bandit-client/forks","keys_url":"https://api.github.com/repos/xiachufang/bandit-client/keys{/key_id}","collaborators_url":"https://api.github.com/repos/xiachufang/bandit-client/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/xiachufang/bandit-client/teams","hooks_url":"https://api.github.com/repos/xiachufang/bandit-client/hooks","issue_events_url":"https://api.github.com/repos/xiachufang/bandit-client/issues/events{/number}","events_url":"https://api.github.com/repos/xiachufang/bandit-client/events","assignees_url":"https://api.github.com/repos/xiachufang/bandit-client/assignees{/user}","branches_url":"https://api.github.com/repos/xiachufang/bandit-client/branches{/branch}","tags_url":"https://api.github.com/repos/xiachufang/bandit-client/tags","blobs_url":"https://api.github.com/repos/xiachufang/bandit-client/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/xiachufang/bandit-client/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/xiachufang/bandit-client/git/refs{/sha}","trees_url":"https://api.github.com/repos/xiachufang/bandit-client/git/trees{/sha}","statuses_url":"https://api.github.com/repos/xiachufang/bandit-client/statuses/{sha}","languages_url":"https://api.github.com/repos/xiachufang/bandit-client/languages","stargazers_url":"https://api.github.com/repos/xiachufang/bandit-client/stargazers","contributors_url":"https://api.github.com/repos/xiachufang/bandit-client/contributors","subscribers_url":"https://api.github.com/repos/xiachufang/bandit-client/subscribers","subscription_url":"https://api.github.com/repos/xiachufang/bandit-client/subscription","commits_url":"https://api.github.com/repos/xiachufang/bandit-client/commits{/sha}","git_commits_url":"https://api.github.com/repos/xiachufang/bandit-client/git/commits{/sha}","comments_url":"https://api.github.com/repos/xiachufang/bandit-client/comments{/number}","issue_comment_url":"https://api.github.com/repos/xiachufang/bandit-client/issues/comments{/number}","contents_url":"https://api.github.com/repos/xiachufang/bandit-client/contents/{+path}","compare_url":"https://api.github.com/repos/xiachufang/bandit-client/compare/{base}...{head}","merges_url":"https://api.github.com/repos/xiachufang/bandit-client/merges","archive_url":"https://api.github.com/repos/xiachufang/bandit-client/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/xiachufang/bandit-client/downloads","issues_url":"https://api.github.com/repos/xiachufang/bandit-client/issues{/number}","pulls_url":"https://api.github.com/repos/xiachufang/bandit-client/pulls{/number}","milestones_url":"https://api.github.com/repos/xiachufang/bandit-client/milestones{/number}","notifications_url":"https://api.github.com/repos/xiachufang/bandit-client/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/xiachufang/bandit-client/labels{/name}","releases_url":"https://api.github.com/repos/xiachufang/bandit-client/releases{/id}","deployments_url":"https://api.github.com/repos/xiachufang/bandit-client/deployments","created_at":"2016-07-18T04:11:43Z","updated_at":"2016-08-09T02:22:01Z","pushed_at":"2016-08-15T11:20:52Z","git_url":"git://github.com/xiachufang/bandit-client.git","ssh_url":"git@github.com:xiachufang/bandit-client.git","clone_url":"https://github.com/xiachufang/bandit-client.git","svn_url":"https://github.com/xiachufang/bandit-client","homepage":null,"size":4,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":1,"forks":2,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/xiachufang/bandit-client/pulls/3"},"html":{"href":"https://github.com/xiachufang/bandit-client/pull/3"},"issue":{"href":"https://api.github.com/repos/xiachufang/bandit-client/issues/3"},"comments":{"href":"https://api.github.com/repos/xiachufang/bandit-client/issues/3/comments"},"review_comments":{"href":"https://api.github.com/repos/xiachufang/bandit-client/pulls/3/comments"},"review_comment":{"href":"https://api.github.com/repos/xiachufang/bandit-client/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/xiachufang/bandit-client/pulls/3/commits"},"statuses":{"href":"https://api.github.com/repos/xiachufang/bandit-client/statuses/3cac0b87359b01bd20bc0f7633fd61b1917d1edd"}}}}
{"actor":{"display_login":"fjkfwz"}}