mod_pubsub_twitter/mod_pubsub_twitter.lua
changeset 1343 7dbde05b48a9
parent 978 c8f4502c764f
equal deleted inserted replaced
1342:0ae065453dc9 1343:7dbde05b48a9
     7 -- }
     7 -- }
     8 -- twitter_searches = { -- node -> query
     8 -- twitter_searches = { -- node -> query
     9 --   prosody = "prosody xmpp";
     9 --   prosody = "prosody xmpp";
    10 -- }
    10 -- }
    11 -- twitter_pull_interval = 20 -- minutes
    11 -- twitter_pull_interval = 20 -- minutes
    12 -- 
    12 --
    13 
    13 
    14 local pubsub = module:depends"pubsub";
    14 local pubsub = module:depends"pubsub";
    15 
    15 
    16 local json = require "util.json";
    16 local json = require "util.json";
    17 local http = require "net.http";
    17 local http = require "net.http";
    38 local active_searches = {};
    38 local active_searches = {};
    39 
    39 
    40 local function publish_result(search_name, result)
    40 local function publish_result(search_name, result)
    41 	local node, id = search_name, result.id_str;
    41 	local node, id = search_name, result.id_str;
    42 	--"Tue, 02 Apr 2013 15:40:54 +0000"
    42 	--"Tue, 02 Apr 2013 15:40:54 +0000"
    43 	local timestamp_date, timestamp_month, timestamp_year, timestamp_time = 
    43 	local timestamp_date, timestamp_month, timestamp_year, timestamp_time =
    44 		result.created_at:match(" (%d+) (%a+) (%d+) (%d%d:%d%d:%d%d)");
    44 		result.created_at:match(" (%d+) (%a+) (%d+) (%d%d:%d%d:%d%d)");
    45 	
    45 
    46 	local timestamp = ("%s-%s-%sT%sZ"):format(timestamp_year, month_number[timestamp_month], timestamp_date, timestamp_time);
    46 	local timestamp = ("%s-%s-%sT%sZ"):format(timestamp_year, month_number[timestamp_month], timestamp_date, timestamp_time);
    47 	
    47 
    48 	local item = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = id })
    48 	local item = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = id })
    49 		:tag("entry", { xmlns = xmlns_atom })
    49 		:tag("entry", { xmlns = xmlns_atom })
    50 			:tag("id"):text(id):up()
    50 			:tag("id"):text(id):up()
    51 			:tag("author")
    51 			:tag("author")
    52 				:tag("name"):text(result.from_user_name.." (@"..result.from_user..")"):up()
    52 				:tag("name"):text(result.from_user_name.." (@"..result.from_user..")"):up()
    53 				:tag("uri"):text("http://twitter.com/"..result.from_user):up()
    53 				:tag("uri"):text("http://twitter.com/"..result.from_user):up()
    54 				:up()
    54 				:up()
    55 			:tag("published"):text(timestamp):up()
    55 			:tag("published"):text(timestamp):up()
    56 			:tag("title"):text(result.text):up()
    56 			:tag("title"):text(result.text):up()
    57 			:tag("link", { rel = "alternate" , href = "https://twitter.com/"..result.from_user.."/status/"..id}):up();
    57 			:tag("link", { rel = "alternate" , href = "https://twitter.com/"..result.from_user.."/status/"..id}):up();
    58 	
    58 
    59 	module:log("debug", "Publishing Twitter result: %s", tostring(item));
    59 	module:log("debug", "Publishing Twitter result: %s", tostring(item));
    60 	
    60 
    61 	local ok, err = pubsub.service:publish(node, true, id, item);
    61 	local ok, err = pubsub.service:publish(node, true, id, item);
    62 	if not ok then
    62 	if not ok then
    63 		if err == "item-not-found" then -- try again
    63 		if err == "item-not-found" then -- try again
    64 			local ok, err = pubsub.service:create(node, true);
    64 			local ok, err = pubsub.service:create(node, true);
    65 			if not ok then
    65 			if not ok then
   104 end
   104 end
   105 
   105 
   106 function module.load()
   106 function module.load()
   107 	local config_searches = set.new(array.collect(it.keys(twitter_searches)));
   107 	local config_searches = set.new(array.collect(it.keys(twitter_searches)));
   108 	local current_searches = set.new(array.collect(it.keys(active_searches)));
   108 	local current_searches = set.new(array.collect(it.keys(active_searches)));
   109 	
   109 
   110 	local disable_searches = current_searches - config_searches;
   110 	local disable_searches = current_searches - config_searches;
   111 	local new_searches = config_searches - current_searches;
   111 	local new_searches = config_searches - current_searches;
   112 	
   112 
   113 	for search_name in disable_searches do
   113 	for search_name in disable_searches do
   114 		module:log("debug", "Disabled old Twitter search '%s'", search_name);
   114 		module:log("debug", "Disabled old Twitter search '%s'", search_name);
   115 		active_searches[search_name] = nil;
   115 		active_searches[search_name] = nil;
   116 	end
   116 	end
   117 
   117