From b0d2ed5fd2d73927bf126e732ee87991606b4a80 Mon Sep 17 00:00:00 2001 From: Andreas Kotes Date: Sun, 15 Nov 2020 20:55:59 +0100 Subject: [PATCH 1/3] Only talk to external services when enabled explicitly --- modules/common/module.py | 2 ++ modules/common/pollingservice.py | 2 +- modules/flog.py | 1 + modules/googlecal.py | 1 + modules/ig.py | 1 + modules/twitter.py | 7 +++++-- modules/url.py | 2 ++ 7 files changed, 13 insertions(+), 3 deletions(-) diff --git a/modules/common/module.py b/modules/common/module.py index f4c0d43..53f10f1 100644 --- a/modules/common/module.py +++ b/modules/common/module.py @@ -98,6 +98,8 @@ class BotModule(ABC): def enable(self): self.enabled = True + if hasattr(self, "__init_enabled"): + self.__init_enabled(self.name) def disable(self): self.enabled = False diff --git a/modules/common/pollingservice.py b/modules/common/pollingservice.py index 6108df3..86ae0e7 100644 --- a/modules/common/pollingservice.py +++ b/modules/common/pollingservice.py @@ -17,7 +17,7 @@ class PollingService(BotModule): self.send_all = False # Set to true to send all received items, even on first sync async def matrix_poll(self, bot, pollcount): - if len(self.account_rooms): + if self.enabled and len(self.account_rooms): await self.poll_all_accounts(bot) async def poll_all_accounts(self, bot): diff --git a/modules/flog.py b/modules/flog.py index 47a8abe..0f52f3b 100644 --- a/modules/flog.py +++ b/modules/flog.py @@ -21,6 +21,7 @@ class MatrixModule(BotModule): self.logged_flights = [] self.logged_flights_date = "" self.first_poll = True + self.enabled = False async def matrix_poll(self, bot, pollcount): if len(self.api_key) > 0: diff --git a/modules/googlecal.py b/modules/googlecal.py index 37b7817..588f506 100644 --- a/modules/googlecal.py +++ b/modules/googlecal.py @@ -27,6 +27,7 @@ class MatrixModule(BotModule): self.bot = None self.service = None self.calendar_rooms = dict() # Contains room_id -> [calid, calid] .. + self.enabled = False def matrix_start(self, bot): super().matrix_start(bot) diff --git a/modules/ig.py b/modules/ig.py index 09d9764..83de68d 100644 --- a/modules/ig.py +++ b/modules/ig.py @@ -15,6 +15,7 @@ class MatrixModule(PollingService): super().__init__(name) self.instagram = Instagram() self.service_name = 'Instagram' + self.enabled = False async def poll_implementation(self, bot, account, roomid, send_messages): try: diff --git a/modules/twitter.py b/modules/twitter.py index 49f20bb..a79e9f4 100644 --- a/modules/twitter.py +++ b/modules/twitter.py @@ -1,8 +1,6 @@ import sys import traceback -from twitterscraper import query_tweets_from_user - from modules.common.pollingservice import PollingService @@ -12,6 +10,11 @@ class MatrixModule(PollingService): def __init__(self, name): super().__init__(name) self.service_name = 'Twitter' + self.enabled = False + + def __init_enabled(self, name): + # importing this has massive sideeffects, so let's only do that when enabled + from twitterscraper import query_tweets_from_user async def poll_implementation(self, bot, account, roomid, send_messages): try: diff --git a/modules/url.py b/modules/url.py index 8763f25..cb0c63d 100644 --- a/modules/url.py +++ b/modules/url.py @@ -32,6 +32,8 @@ class MatrixModule(BotModule): "BOTH": "Spamming this channel with both title and description", } + self.enabled = False + def matrix_start(self, bot): """ Register callback for all RoomMessageText events on startup From 0e4a55f77a27d5effb8f9de4f8ce914636d7b56f Mon Sep 17 00:00:00 2001 From: Andreas Kotes Date: Sun, 15 Nov 2020 21:22:15 +0100 Subject: [PATCH 2/3] Update README about disabled modules --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 020e68d..745d779 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,8 @@ Commands: * !googlecal del [calendar id] - Delete calendar from room (Must be done as room admin) * !googlecal list - List calendars in this room +NOTE: disabled by default + ### Cron Can schedule things to be done. @@ -203,6 +205,8 @@ without any authentication or api key. See: https://github.com/realsirjoe/instagram-scraper/ +NOTE: disabled by default + #### Twitter Polls twitter account(s). Uses twitter scraper library @@ -269,6 +273,8 @@ Example: * !url status +NOTE: Disabled by default, i.e. you also need to enable it before activating it + ### Cmd Can be used to pre-configure shell commands run by bot. This is easy way to add @@ -373,6 +379,8 @@ bot ownership) * !flog rmlive - disable live field log for this room * !flog timezone 3 - set timezone (relative to UTC, see API docs) +NOTE: disabled by default + ### Jitsi If enabled, Jitsi calls created with Matrix clients will be sent as text messages From 0839b653f446daeed7a15453c905b636cb32ec36 Mon Sep 17 00:00:00 2001 From: Andreas Kotes Date: Sun, 15 Nov 2020 21:24:17 +0100 Subject: [PATCH 3/3] Yank unreliable Twitter module and related stuff --- Pipfile | 1 - README.md | 9 +-------- modules/common/module.py | 2 -- modules/twitter.py | 32 -------------------------------- 4 files changed, 1 insertion(+), 43 deletions(-) delete mode 100644 modules/twitter.py diff --git a/Pipfile b/Pipfile index 022e6ee..3e3a562 100644 --- a/Pipfile +++ b/Pipfile @@ -13,7 +13,6 @@ google-auth-httplib2 = "*" google-auth-oauthlib = "*" requests = "*" igramscraper = "*" -twitterscraper = "*" httpx = "*" PyYAML = "==5.3" wolframalpha = "*" diff --git a/README.md b/README.md index 745d779..5a87a96 100644 --- a/README.md +++ b/README.md @@ -189,7 +189,7 @@ New posts are sent to room. Polls only randomly every 30 to 60 minutes to keep Commands: -Prefix with selected service, for example "!ig add accountname" or "!twitter list" +Prefix with selected service, for example "!ig add accountname" or "!teamup list" * add [accountname] - Add account to this room (Must be done as room admin) * del [accountname] - Delete account from room (Must be done as room admin) @@ -207,13 +207,6 @@ See: https://github.com/realsirjoe/instagram-scraper/ NOTE: disabled by default -#### Twitter - -Polls twitter account(s). Uses twitter scraper library -without any authentication or api key. - -See: https://github.com/taspinar/twitterscraper/tree/master/twitterscraper - #### Matrix Messaging API (mxma) This is a simple API to ask bot to send messages in Matrix using JSON file from external service. diff --git a/modules/common/module.py b/modules/common/module.py index 53f10f1..f4c0d43 100644 --- a/modules/common/module.py +++ b/modules/common/module.py @@ -98,8 +98,6 @@ class BotModule(ABC): def enable(self): self.enabled = True - if hasattr(self, "__init_enabled"): - self.__init_enabled(self.name) def disable(self): self.enabled = False diff --git a/modules/twitter.py b/modules/twitter.py deleted file mode 100644 index a79e9f4..0000000 --- a/modules/twitter.py +++ /dev/null @@ -1,32 +0,0 @@ -import sys -import traceback - -from modules.common.pollingservice import PollingService - - -# https://github.com/taspinar/twitterscraper/tree/master/twitterscraper - -class MatrixModule(PollingService): - def __init__(self, name): - super().__init__(name) - self.service_name = 'Twitter' - self.enabled = False - - def __init_enabled(self, name): - # importing this has massive sideeffects, so let's only do that when enabled - from twitterscraper import query_tweets_from_user - - async def poll_implementation(self, bot, account, roomid, send_messages): - try: - tweets = query_tweets_from_user(account, limit=1) - self.logger.info(f'Polling twitter account {account} - got {len(tweets)} tweets') - for tweet in tweets: - if tweet.tweet_id not in self.known_ids: - if send_messages: - await bot.send_html(bot.get_room_by_id(roomid), - f'Twitter {account}: {tweet.text}', - f'Twitter {account}: {tweet.text} - https://twitter.com{tweet.tweet_url}') - self.known_ids.add(tweet.tweet_id) - except Exception: - self.logger.error('Polling twitter account failed:') - traceback.print_exc(file=sys.stderr)