summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsand <daniel@spatof.org>2013-05-14 13:57:19 (GMT)
committer sand <daniel@spatof.org>2013-05-14 13:57:19 (GMT)
commite27216b9f6a8ed94781d733b424aa7b507c0d6f9 (patch)
tree714afde35d828300de8ecb6654ce5679ac5a30a1
parent63dc18c0e2461771ab6256b16b971b6d14a172cf (diff)
User-Agent per query HTTP; tentativo con functools.partial
-rw-r--r--pinolo/plugins/_ddg.py31
-rw-r--r--pinolo/plugins/eztv.py4
-rw-r--r--pinolo/plugins/twitter.py4
3 files changed, 26 insertions, 13 deletions
diff --git a/pinolo/plugins/_ddg.py b/pinolo/plugins/_ddg.py
index b105c0e..2deac7c 100644
--- a/pinolo/plugins/_ddg.py
+++ b/pinolo/plugins/_ddg.py
@@ -1,14 +1,28 @@
# -*- coding: utf-8 -*-
-"""DuckDuckGo plugin"""
+"""
+ pinolo.plugins.ddg
+ ~~~~~~~~~~~~~~~~~~
+
+ DuckDuckGo API client (WIP).
+
+ :copyright: (c) 2013 Daniel Kertesz
+ :license: BSD, see LICENSE for more details.
+"""
import urllib
import json
+import requests
from pinolo.plugins import Plugin
-from pinolo.utils.network import gevent_url_open
+from pinolo.tasks import Task
+from pinolo import USER_AGENT
+# URL for search API
SEARCH_URL = "http://api.duckduckgo.com/"
+
MAX_RESULTS = 5
+
SEARCH_LANG = "it"
+
TYPES = {
u'A': u'article',
u'D': u'disambiguation',
@@ -27,15 +41,10 @@ def search_ddg(text):
'no_redirect': '1',
})
- url = SEARCH_URL + "?" + query
- response = gevent_url_open(url)
- headers = response.headers
- if 'content-type' in headers:
- encoding = headers['content-type'].split('charset=')[-1]
- data = unicode(response.read(), encoding)
- else:
- data = response.read()
- data.decode('utf-8', 'replace')
+ payload = dict(q=text, format="json", no_html="1", no_redirect="1")
+ response = requests.get(SEARCH_URL, params=payload)
+ data = response.json()
+
r = json.loads(data)
# toglie i value 'empty'
# pr = dict((k, v) for k,v in r.items() if v)
diff --git a/pinolo/plugins/eztv.py b/pinolo/plugins/eztv.py
index 056981a..0459bc4 100644
--- a/pinolo/plugins/eztv.py
+++ b/pinolo/plugins/eztv.py
@@ -13,6 +13,7 @@ import requests
from bs4 import BeautifulSoup
from pinolo.plugins import Plugin
from pinolo.tasks import Task
+from pinolo import USER_AGENT
# URL with search form
@@ -39,8 +40,9 @@ def search_eztv(text):
payload = {
'SearchString1': text
}
+ headers = {"User-Agent": USER_AGENT}
- r = requests.post(SEARCH_URL, data=payload)
+ r = requests.post(SEARCH_URL, data=payload, headers=headers)
soup = BeautifulSoup(r.text)
results = []
diff --git a/pinolo/plugins/twitter.py b/pinolo/plugins/twitter.py
index fb56fb1..77b67c2 100644
--- a/pinolo/plugins/twitter.py
+++ b/pinolo/plugins/twitter.py
@@ -11,6 +11,7 @@
import re
import requests
import logging
+from functools import partial
from pinolo.plugins import Plugin
from pinolo.tasks import Task
@@ -42,7 +43,8 @@ class TwitterTask(Task):
return
reply = u'"%s" @%s: %s' % (name, username, text)
- self.put_results(self.event.reply, reply)
+ reply_fn = partial(self.event.reply, prefix=False)
+ self.put_results(reply_fn, reply)
class TwitterPlugin(Plugin):