refactor/fix: Fix module name collision

This commit is contained in:
PapaTutuWawa 2021-06-13 13:17:39 +02:00
parent 3fcf5d52b3
commit e953486494
5 changed files with 75 additions and 24 deletions

0
papatutuwawa/__init__.py Normal file
View File

View File

View File

View File

@ -14,7 +14,6 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
# TODO: Prevent subscriptions when subscribed to 'Alle'
# TODO: remove Alle sollte jede subscription entfernen
# TODO: Deal with weird casing and typos
# TODO: pollen hilfe <CMD> should print help per command
@ -63,8 +62,8 @@ class PollenModule(BaseModule):
super().__init__(base, **kwargs)
PollenModule.__instance = self
self._subcommand_table = {
'add': self._subscribe,
'remove': self._unsubscribe,
'subscribe': self._subscribe,
'unsubscribe': self._unsubscribe,
'hilfe': self._help
}
self._sleep_duration = self.get_option('sleep_duration', 12 * 3600)
@ -84,11 +83,18 @@ class PollenModule(BaseModule):
'''
msg = '{} (*{}*)\n'.format(date, plz)
if allergies == ['Alle']:
if 'Alle' in [x['type'] for x in allergies]:
allergies = list(self._pollen_data[plz][date].keys)
count = 0
for allergy in allergies:
msg += '{}: {}'.format(allergy, intensity_str(self._pollen_data[plz][date][allergy]))
intensity = self._pollen_data[plz][date][allergy['type']]
if intensity >= allergy['level']:
msg += '{}: {}'.format(allergy['type'], intensity_str(intensity))
count += 1
if count == 0:
return ''
return msg
def _is_notified(self, jid, date):
@ -121,7 +127,10 @@ class PollenModule(BaseModule):
only the data for the pollen types in filter_allergies will be sent.
'''
for plz, data in self._sum.get_subscriptions_for(jid).items():
allergies = data['data'] if not filter_allergies else filter_allergies
if filter_allergies:
allergies = filter_allergies
else:
allergies = data['data']
notified = []
for date in self._pollen_data[plz]:
@ -129,10 +138,12 @@ class PollenModule(BaseModule):
continue
notified.append(date)
self.send_message(aioxmpp.JID.fromstr(jid),
self.notification_body(plz,
date,
allergies))
msg = self.notification_body(plz,
date,
allergies)
if msg:
self.send_message(aioxmpp.JID.fromstr(jid),
msg)
if notified:
self._set_dates_notified(jid, notified)
@ -182,25 +193,40 @@ class PollenModule(BaseModule):
await asyncio.sleep(self._sleep_duration)
async def _subscribe(self, cmd, msg):
if len(cmd) != 3:
self.send_message(msg.from_, 'Verwendung: pollen add <PLZ> <Allergen>')
if len(cmd) < 3 or len(cmd) > 4:
self.send_message(msg.from_, 'Verwendung: pollen add <PLZ> <Allergen> [<Level>]')
return
plz = cmd[1]
allergy = cmd[2]
jid = str(msg.from_.bare())
level = int(cmd[3]) if len(cmd) == 4 else 0
if self._sum.is_subscribed_to_data(jid, plz, allergy):
self.send_message(msg.from_, 'Du hast das schon aboniert')
if self._sum.is_subscribed_to_data(jid, plz, 'Alle'):
self.send_message(msg.from_, 'Du hast schon alle Allergene abonniert')
return
self._sum.append_data_for_subscription(jid, plz, allergy)
self.send_message(msg.from_, 'OK')
if self._sum.is_subscribed_to_data(jid, plz, allergy):
self.send_message(msg.from_, 'Du hast das schon abonniert')
return
if level < 0 or level > 3:
self.send_message(msg.from_, 'Das Level muss zwischen 0 und 3 liegen')
return
self._sum.append_data_for_subscription(jid, plz, {
'type': allergy,
'level': level
})
self.send_message(msg.from_, 'Du erhälst nun Pollenmeldungen zu %s' % allergy)
# Just some bandwidth saving measure
if plz not in self._pollen_data:
await self._request_pollen_data(False)
self._broadcast_jid(jid, [allergy])
self._broadcast_jid(jid, [{
'type': allergy,
'level': level
}])
async def _unsubscribe(self, cmd, msg):
if len(cmd) != 3:
@ -211,20 +237,28 @@ class PollenModule(BaseModule):
allergy = cmd[2]
jid = str(msg.from_.bare())
if not self._sum.is_subscribed_to_data(jid, plz, allergy):
self.send_message(msg.from_, 'Du hast das nicht aboniert')
if allergy == 'Alle':
self._sum.filter_items_for_subscriptions(jid, plz, lambda x: False)
self.send_message(msg.from_, 'Du erhälst keine Pollenmeldungen mehr')
return
self._sum.remove_data_for_subscription(str(msg.from_.bare()), plz, allergy)
self.send_message(msg.from_, 'OK')
if not self._sum.is_subscribed_to_data_one(jid, plz, lambda x: x['type'] == allergy):
self.send_message(msg.from_, 'Du hast %s nicht abonniert' % (allergy))
return
self._sum.filter_items_for_subscription(str(msg.from_.bare()),
plz,
lambda x: x['type'] != allergy)
self.send_message(msg.from_, 'Du erhälst nun keine Pollenmeldungen zu %s mehr' % (allergy))
async def _help(self, cmd, msg):
body = '''Verfügbare Befehle:
pollen add <PLZ> <Typ>
pollen remove <PLZ> <Typ>
pollen subscribe <PLZ> <Typ> [<Min. Level>]
pollen unsubscribe <PLZ> <Typ>
pollen hilfe
Pollentypen: Ambrosia, Ampfer, Beifuß, Birke, Buche, Eiche, Erle, Esche, Gräser, Hasel, Pappel, Roggen, Ulme, Wegerich, Weide, Alle
Pollentypen: Ambrosia, Ampfer, Beifuß, Birke, Buche, Eiche, Erle, Esche, Gräser, Hasel, Pappel, Roggen, Ulme, Wegerich, Weide, Alle (Alias für alle Pollentypen)
Level: 0 (Kein Pollenflug) - 3 (Starker Pollenflug)
'''
self.send_message(msg.from_, body)

17
setup.py Normal file
View File

@ -0,0 +1,17 @@
from setuptools import setup, find_packages
setup(
name = 'mira-modules',
version = '0.1.0',
description = 'Various modules for mira',
url = 'https://git.polynom.me/PapaTutuWawa/mira-modules',
author = 'Alexander "PapaTutuWawa"',
author_email = 'papatutuwawa <at> polynom.me',
license = 'GPLv3',
packages = find_packages(),
install_requires = [
'mira>=0.1.0',
'requests>=2.23.0'
],
zip_safe=True
)