From 91614a1c63aa8922ac53d70a5e19b4577ef85ae4 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Tue, 16 Aug 2016 13:19:24 +0200 Subject: [PATCH] - encoding() -> unescape() Nummer2 - Entferne Tweet (da unfertig) --- miku/plugins/app_store.lua | 4 +- miku/plugins/myanimelist.lua | 7 +- miku/plugins/reddit.lua | 4 +- miku/plugins/reddit_post.lua | 6 +- miku/plugins/steam.lua | 4 +- miku/plugins/tweet.lua | 199 ---------------------------------- miku/plugins/twitter_send.lua | 4 +- miku/plugins/twitter_user.lua | 2 +- 8 files changed, 10 insertions(+), 220 deletions(-) delete mode 100644 miku/plugins/tweet.lua diff --git a/miku/plugins/app_store.lua b/miku/plugins/app_store.lua index e7e8b79..5ef13af 100644 --- a/miku/plugins/app_store.lua +++ b/miku/plugins/app_store.lua @@ -1,7 +1,5 @@ local app_store = {} -require "./miku/encoding" - app_store.triggers = { "itunes.apple.com/(.*)/app/(.*)/id(%d+)", "^/[Ii][Tt][Uu][Nn][Ee][Ss] (%d+)$", @@ -36,7 +34,7 @@ function app_store:send_appstore_data(data) local version = data.version -- Body - local description = string.sub(encoding(data.description), 1, 150) .. '...' + local description = string.sub(unescape(data.description), 1, 150) .. '...' local min_ios_ver = data.minimumOsVersion local size = string.gsub(round(data.fileSizeBytes / 1000000, 2), "%.", ",") -- wtf Apple, it's 1024, not 1000! local release = makeOurDate(data.releaseDate) diff --git a/miku/plugins/myanimelist.lua b/miku/plugins/myanimelist.lua index 3f923c7..a0fd012 100644 --- a/miku/plugins/myanimelist.lua +++ b/miku/plugins/myanimelist.lua @@ -1,7 +1,6 @@ local mal = {} local xml = require("xml") -require "./miku/encoding" mal.command = 'anime , /manga ' @@ -118,7 +117,7 @@ function mal:send_anime_data(result, receiver) end if xml.find(result, 'synopsis')[1] then - desc = '\n_'..encoding(mal:delete_tags(string.sub(xml.find(result, 'synopsis')[1], 1, 250)))..'..._' + desc = '\n_'..unescape(mal:delete_tags(string.sub(xml.find(result, 'synopsis')[1], 1, 250)))..'..._' else desc = '' end @@ -145,7 +144,7 @@ function mal:send_manga_data(result) end if xml.find(result, 'synonyms')[1] then - alt_name = '\noder: '..encoding(mal:delete_tags(xml.find(result, 'synonyms')[1])) + alt_name = '\noder: '..unescape(mal:delete_tags(xml.find(result, 'synonyms')[1])) else alt_name = '' end @@ -187,7 +186,7 @@ function mal:send_manga_data(result) end if xml.find(result, 'synopsis')[1] then - desc = '\n'..encoding(mal:delete_tags(string.sub(xml.find(result, 'synopsis')[1], 1, 200))) .. '...' + desc = '\n'..unescape(mal:delete_tags(string.sub(xml.find(result, 'synopsis')[1], 1, 200))) .. '...' else desc = '' end diff --git a/miku/plugins/reddit.lua b/miku/plugins/reddit.lua index 5862efb..cc46488 100644 --- a/miku/plugins/reddit.lua +++ b/miku/plugins/reddit.lua @@ -1,7 +1,5 @@ local reddit = {} -require "./miku/encoding" - reddit.command = 'reddit [r/subreddit | Suchbegriff]' function reddit:init(config) @@ -20,7 +18,7 @@ local format_results = function(posts) title = utilities.trim(title) .. '...' end local short_url = 'https://redd.it/' .. post.id - local s = '[' .. encoding(title) .. '](' .. short_url .. ')' + local s = '[' .. unescape(title) .. '](' .. short_url .. ')' if post.domain and not post.is_self and not post.over_18 then s = '`[`[' .. post.domain .. '](' .. post.url:gsub('%)', '\\)') .. ')`]` ' .. s end diff --git a/miku/plugins/reddit_post.lua b/miku/plugins/reddit_post.lua index 3a50d05..cc2d16d 100644 --- a/miku/plugins/reddit_post.lua +++ b/miku/plugins/reddit_post.lua @@ -1,7 +1,5 @@ local reddit_post = {} -require "./miku/encoding" - reddit_post.triggers = { "reddit.com/r/([A-Za-z0-9-/-_-.]+)/comments/([A-Za-z0-9-/-_-.]+)" } @@ -21,9 +19,9 @@ function reddit_post:send_reddit_data(data) local author = utilities.md_escape(data[1].data.children[1].data.author) local subreddit = utilities.md_escape(data[1].data.children[1].data.subreddit) if string.len(data[1].data.children[1].data.selftext) > 300 then - selftext = string.sub(encoding(data[1].data.children[1].data.selftext:gsub("%b<>", "")), 1, 300) .. '...' + selftext = string.sub(unescape(data[1].data.children[1].data.selftext:gsub("%b<>", "")), 1, 300) .. '...' else - selftext = encoding(data[1].data.children[1].data.selftext:gsub("%b<>", "")) + selftext = unescape(data[1].data.children[1].data.selftext:gsub("%b<>", "")) end if not data[1].data.children[1].data.is_self then url = data[1].data.children[1].data.url diff --git a/miku/plugins/steam.lua b/miku/plugins/steam.lua index 9fe1e6f..145b1ea 100644 --- a/miku/plugins/steam.lua +++ b/miku/plugins/steam.lua @@ -1,7 +1,5 @@ local steam = {} -require "./miku/encoding" - steam.triggers = { "store.steampowered.com/app/([0-9]+)", "steamcommunity.com/app/([0-9]+)" @@ -38,7 +36,7 @@ function steam:price_info(data) end function steam:send_steam_data(data, self, msg) - local description = string.sub(encoding(data.about_the_game:gsub("%b<>", "")), 1, DESC_LENTH) .. '...' + local description = string.sub(unescape(data.about_the_game:gsub("%b<>", "")), 1, DESC_LENTH) .. '...' local title = data.name local price = steam:price_info(data.price_overview) diff --git a/miku/plugins/tweet.lua b/miku/plugins/tweet.lua deleted file mode 100644 index 4894ced..0000000 --- a/miku/plugins/tweet.lua +++ /dev/null @@ -1,199 +0,0 @@ -local tweet = {} - -require "./miku/encoding" - -function tweet:init(config) - if not cred_data.tw_consumer_key then - print('Fehlender Key: tw_consumer_key.') - print('tweet.lua wird nicht aktiviert.') - return - elseif not cred_data.tw_consumer_secret then - print('Fehlender Key: tw_consumer_secret.') - print('tweet.lua wird nicht aktiviert.') - return - elseif not cred_data.tw_access_token then - print('Fehlender Key: tw_access_token.') - print('tweet.lua wird nicht aktiviert.') - return - elseif not cred_data.tw_access_token_secret then - print('Fehlender Key: tw_access_token_secret.') - print('tweet.lua wird nicht aktiviert.') - return - end - - tweet.triggers = { - "^/tweet (id) ([%w_%.%-]+)$", - "^/tweet (id) ([%w_%.%-]+) (last)$", - "^/tweet (id) ([%w_%.%-]+) (last) ([%d]+)$", - "^/tweet (name) ([%w_%.%-]+)$", - "^/tweet (name) ([%w_%.%-]+) (last)$", - "^/tweet (name) ([%w_%.%-]+) (last) ([%d]+)$" - } - tweet.doc = [[* -]]..config.cmd_pat..[[tweet* id _[id]_: Zufälliger Tweet vom User mit dieser ID -*]]..config.cmd_pat..[[tweet* id _[id]_ last: Aktuellster Tweet vom User mit dieser ID -*]]..config.cmd_pat..[[tweet* name _[Name]_: Zufälliger Tweet vom User mit diesem Namen -*]]..config.cmd_pat..[[tweet* name _[Name]_ last: Aktuellster Tweet vom User mit diesem Namen]] -end - -tweet.command = 'tweet name ' - -local consumer_key = cred_data.tw_consumer_key -local consumer_secret = cred_data.tw_consumer_secret -local access_token = cred_data.tw_access_token -local access_token_secret = cred_data.tw_access_token_secret - -local client = OAuth.new(consumer_key, consumer_secret, { - RequestToken = "https://api.twitter.com/oauth/request_token", - AuthorizeUser = {"https://api.twitter.com/oauth/authorize", method = "GET"}, - AccessToken = "https://api.twitter.com/oauth/access_token" -}, { - OAuthToken = access_token, - OAuthTokenSecret = access_token_secret -}) - -local twitter_url = "https://api.twitter.com/1.1/statuses/user_timeline.json" - -function tweet:analyze_tweet(tweet) - local header = "Tweet von " .. tweet.user.name .. " (@" .. tweet.user.screen_name .. ")\nhttps://twitter.com/statuses/" .. tweet.id_str - local text = tweet.text - - -- replace short URLs - if tweet.entities.urls then - for k, v in pairs(tweet.entities.urls) do - local short = v.url - local long = v.expanded_url - text = text:gsub(short, long) - end - end - - -- remove urls - local urls = {} - if tweet.extended_entities and tweet.extended_entities.media then - for k, v in pairs(tweet.extended_entities.media) do - if v.video_info and v.video_info.variants then -- If it's a video! - table.insert(urls, v.video_info.variants[1].url) - else -- If not, is an image - table.insert(urls, v.media_url) - end - text = text:gsub(v.url, "") -- Replace the URL in text - text = encoding(text) - end - end - - return header, text, urls -end - -function tweet:send_all_files(self, msg, urls) - local data = { - images = { - func = send_photos_from_url, - urls = {} - }, - gifs = { - func = send_gifs_from_url, - urls = {} - }, - videos = { - func = send_videos_from_url, - urls = {} - } - } - - local table_to_insert = nil - for i,url in pairs(urls) do - local _, _, extension = string.match(url, "(https?)://([^\\]-([^\\%.]+))$") - local mime_type = mimetype.get_content_type_no_sub(extension) - if extension == 'gif' then - table_to_insert = data.gifs.urls - elseif mime_type == 'image' then - table_to_insert = data.images.urls - elseif mime_type == 'video' then - table_to_insert = data.videos.urls - else - table_to_insert = nil - end - if table_to_insert then - table.insert(table_to_insert, url) - end - end - for k, v in pairs(data) do - if #v.urls > 0 then - end - v.func(receiver, v.urls) - end -end - -function tweet:sendTweet(self, msg, tweet) - local header, text, urls = tweet:analyze_tweet(tweet) - -- send the parts - local text = encoding(text) - send_reply(self, msg, header .. "\n" .. text) - tweet:send_all_files(self, msg, urls) - return nil -end - -function tweet:getTweet(self, msg, base, all) - local response_code, response_headers, response_status_line, response_body = client:PerformRequest("GET", twitter_url, base) - - if response_code ~= 200 then - return "Konnte nicht verbinden, evtl. existiert der User nicht?" - end - - local response = json.decode(response_body) - if #response == 0 then - return "Konnte keinen Tweet bekommen, sorry" - end - if all then - for i,tweet in pairs(response) do - tweet:sendTweet(self, msg, tweet) - end - else - local i = math.random(#response) - local tweet = response[i] - tweet:sendTweet(self, msg, tweet) - end - - return nil -end - -function tweet:isint(n) - return n==math.floor(n) -end - -function tweet:action(msg, config, matches) - local base = {include_rts = 1} - - if matches[1] == 'id' then - local userid = tonumber(matches[2]) - if userid == nil or not tweet:isint(userid) then - utilities.send_reply(self, msg, "Die ID eines Users ist eine Zahl, du findest sie, indem du den Namen [auf dieser Webseite](http://gettwitterid.com/) eingibst.", true) - return - end - base.user_id = userid - elseif matches[1] == 'name' then - base.screen_name = matches[2] - else - return "" - end - - local count = 200 - local all = false - if #matches > 2 and matches[3] == 'last' then - count = 1 - if #matches == 4 then - local n = tonumber(matches[4]) - if n > 10 then - utilities.send_reply(self, msg, "Du kannst nur 10 Tweets auf einmal abfragen!") - return - end - count = matches[4] - all = true - end - end - base.count = count - - utilities.send_reply(self, msg, tweet:getTweet(self, msg, base, all)) -end - -return tweet diff --git a/miku/plugins/twitter_send.lua b/miku/plugins/twitter_send.lua index d641003..f4f52de 100644 --- a/miku/plugins/twitter_send.lua +++ b/miku/plugins/twitter_send.lua @@ -1,7 +1,5 @@ local twitter_send = {} -require "./miku/encoding" - function twitter_send:init(config) if not cred_data.tw_consumer_key then print('Fehlender Key: tw_consumer_key.') @@ -164,7 +162,7 @@ function twitter_send:twitter_verify_credentials(oauth_token, oauth_token_secret end local header = full_name.. " (@" ..user_name.. ")\n" - local description = encoding(response.description) + local description = unescape(response.description) if response.location then location = response.location else diff --git a/miku/plugins/twitter_user.lua b/miku/plugins/twitter_user.lua index f53783f..8e0fd43 100644 --- a/miku/plugins/twitter_user.lua +++ b/miku/plugins/twitter_user.lua @@ -74,7 +74,7 @@ function twitter_user:action(msg, config, matches) end local header = full_name.. " (@" ..user_name.. ")\n" - local description = encoding(response.description) + local description = unescape(response.description) if response.location then location = response.location else