2014-10-27 16:29:04 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-04-20 17:49:07 -05:00
|
|
|
import config
|
|
|
|
from requests.auth import HTTPProxyAuth
|
2015-02-08 05:48:40 -06:00
|
|
|
from twitter import compose, utils
|
2014-10-27 16:29:04 -06:00
|
|
|
from twython import TwythonStreamer
|
2014-11-12 20:41:29 -06:00
|
|
|
from pubsub import pub
|
2014-10-27 16:29:04 -06:00
|
|
|
import logging as original_logger
|
2015-03-07 20:23:41 -06:00
|
|
|
log = original_logger.getLogger("TimelinesStream")
|
2014-10-27 16:29:04 -06:00
|
|
|
|
2014-11-12 20:41:29 -06:00
|
|
|
class timelinesStreamer(TwythonStreamer):
|
|
|
|
def __init__(self, app_key, app_secret, oauth_token, oauth_token_secret, timeout=300, retry_count=None, retry_in=10, client_args=None, handlers=None, chunk_size=1, session=None):
|
|
|
|
self.session = session
|
2015-04-20 17:49:07 -05:00
|
|
|
args = None
|
|
|
|
if config.app["proxy"]["server"] != "" and config.app["proxy"]["port"] != "":
|
|
|
|
args = {"proxies": {"http": "http://{0}:{1}".format(config.app["proxy"]["server"], config.app["proxy"]["port"]),
|
|
|
|
"https": "https://{0}:{1}".format(config.app["proxy"]["server"], config.app["proxy"]["port"])}}
|
|
|
|
if config.app["proxy"]["user"] != "" and config.app["proxy"]["password"] != "":
|
|
|
|
auth = HTTPProxyAuth(config.app["proxy"]["user"], config.app["proxy"]["password"])
|
|
|
|
args["auth"] = auth
|
|
|
|
super(timelinesStreamer, self).__init__(app_key, app_secret, oauth_token, oauth_token_secret, timeout=60, retry_count=None, retry_in=180, client_args=args, handlers=None, chunk_size=1)
|
2015-04-27 16:08:02 -05:00
|
|
|
self.lists = self.session.lists
|
2014-10-27 16:29:04 -06:00
|
|
|
|
|
|
|
def on_error(self, status_code, data):
|
|
|
|
log.debug("%s: %s" % (status_code, data))
|
|
|
|
|
2015-01-18 17:19:39 -06:00
|
|
|
def on_timeout(self, *args, **kwargs):
|
|
|
|
log.debug("Twitter timeout Error")
|
|
|
|
pub.sendMessage("stream-error")
|
|
|
|
|
2014-10-27 16:29:04 -06:00
|
|
|
def check_tls(self, data):
|
2014-11-12 20:41:29 -06:00
|
|
|
for i in self.session.settings["other_buffers"]["timelines"]:
|
2014-10-27 16:29:04 -06:00
|
|
|
if data["user"]["screen_name"] == i:
|
2015-09-23 10:01:00 -05:00
|
|
|
if utils.find_item(data["id"], self.session.db["%s-timeline" % (i,)]) != None:
|
|
|
|
log.error("duplicated tweet. Ignoring it...")
|
|
|
|
return
|
2015-09-29 08:38:05 -05:00
|
|
|
data = self.session.check_quoted_status(data)
|
2015-01-22 08:54:41 -06:00
|
|
|
if self.session.settings["general"]["reverse_timelines"] == False: self.session.db["%s-timeline" % (i,)].append(data)
|
|
|
|
else: self.session.db["%s-timeline" % (i,)].insert(0, data)
|
2014-11-12 20:41:29 -06:00
|
|
|
pub.sendMessage("item-in-timeline", data= data, user= self.session.db["user_name"], who= i)
|
2015-04-27 16:08:02 -05:00
|
|
|
for i in self.session.lists:
|
|
|
|
try:
|
|
|
|
i.users.index(data["user"]["id"])
|
|
|
|
usr = data["in_reply_to_user_id"]
|
|
|
|
if (usr != None and usr in self.friends) or data.has_key("retweeted_status"):
|
2015-09-29 08:38:05 -05:00
|
|
|
data = self.session.check_quoted_status(data)
|
2015-04-27 16:08:02 -05:00
|
|
|
if self.session.settings["general"]["reverse_timelines"] == False: self.session.db["%s" % (i.name,)].append(data)
|
|
|
|
else: self.session.db["%s" % (i,)].insert(0, data)
|
|
|
|
pub.sendMessage("item-in-list", data= data, user= self.session.db["user_name"], where= i.name)
|
|
|
|
elif usr == None:
|
|
|
|
if self.session.settings["general"]["reverse_timelines"] == False: self.session.db["%s" % (i.name,)].append(data)
|
|
|
|
else: self.session.db["%s" % (i,)].insert(0, data)
|
|
|
|
pub.sendMessage("item-in-list", data= data, user= self.session.db["user_name"], where= i.name)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2014-11-12 20:41:29 -06:00
|
|
|
|
2015-04-27 16:08:02 -05:00
|
|
|
def set_friends(self, friends):
|
|
|
|
self.friends = friends
|
2014-10-27 16:29:04 -06:00
|
|
|
|
|
|
|
def on_success(self, data):
|
|
|
|
try:
|
2015-04-27 16:08:02 -05:00
|
|
|
if "text" in data and utils.is_allowed(data, self.session.settings["twitter"]["ignored_clients"]) == True:
|
|
|
|
self.check_tls(data)
|
2014-10-27 16:29:04 -06:00
|
|
|
except:
|
2015-04-27 16:08:02 -05:00
|
|
|
pass
|