Module refactoring
This commit is contained in:
parent
3d28e59d46
commit
50872e9ca7
@ -3,7 +3,7 @@
|
||||
__author__ = "Davide Testa"
|
||||
__email__ = "davide@davte.it"
|
||||
__license__ = "GNU General Public License v3.0"
|
||||
__version__ = "1.1.7"
|
||||
__version__ = "1.1.8"
|
||||
__maintainer__ = "Davide Testa"
|
||||
__contact__ = "t.me/davte"
|
||||
|
||||
|
@ -15,6 +15,8 @@ from davtelepot.utilities import (
|
||||
make_lines_of_buttons
|
||||
)
|
||||
|
||||
default_location = None
|
||||
|
||||
_URL = "http://www.ciclopi.eu/frmLeStazioni.aspx"
|
||||
|
||||
ciclopi_webpage = CachedPage.get(
|
||||
@ -104,7 +106,7 @@ def haversine_distance(lat1, lon1, lat2, lon2, degrees='dec', unit='m'):
|
||||
)
|
||||
|
||||
|
||||
class Location():
|
||||
class Location:
|
||||
"""Location in world map."""
|
||||
|
||||
def __init__(self, coordinates):
|
||||
@ -302,13 +304,13 @@ class Station(Location):
|
||||
),
|
||||
}
|
||||
|
||||
def __init__(self, id=0, name='unknown', coordinates=(91.0, 181.0)):
|
||||
def __init__(self, id_=0, name='unknown', coordinates=(91.0, 181.0)):
|
||||
"""Check and set instance attributes."""
|
||||
if id in self.__class__.stations:
|
||||
coordinates = self.__class__.stations[id]['coordinates']
|
||||
name = self.__class__.stations[id]['name']
|
||||
if id_ in self.__class__.stations:
|
||||
coordinates = self.__class__.stations[id_]['coordinates']
|
||||
name = self.__class__.stations[id_]['name']
|
||||
Location.__init__(self, coordinates)
|
||||
self._id = id
|
||||
self._id = id_
|
||||
self._name = name
|
||||
self._active = True
|
||||
self._location = None
|
||||
@ -442,8 +444,9 @@ def ciclopi_custom_sorter(custom_order):
|
||||
Stations will be sorted by queue value in ascending order.
|
||||
"""
|
||||
if station.id in custom_values:
|
||||
return (custom_values[station.id], station.name)
|
||||
return (100, station.name)
|
||||
return custom_values[station.id], station.name
|
||||
return 100, station.name
|
||||
|
||||
return sorter
|
||||
|
||||
|
||||
@ -568,6 +571,7 @@ async def _ciclopi_command(bot, update, user_record, sent_message=None,
|
||||
show_all=False):
|
||||
chat_id = update['chat']['id']
|
||||
default_stations_to_show = 5
|
||||
stations = []
|
||||
placeholder_id = bot.set_placeholder(
|
||||
timeout=datetime.timedelta(seconds=1),
|
||||
sent_message=sent_message,
|
||||
@ -622,9 +626,7 @@ async def _ciclopi_command(bot, update, user_record, sent_message=None,
|
||||
ciclopi_record is not None
|
||||
and isinstance(ciclopi_record, dict)
|
||||
and 'stations_to_show' in ciclopi_record
|
||||
and ciclopi_record[
|
||||
'stations_to_show'
|
||||
] in CICLOPI_STATIONS_TO_SHOW
|
||||
and ciclopi_record['stations_to_show'] in CICLOPI_STATIONS_TO_SHOW
|
||||
):
|
||||
stations_to_show = ciclopi_record[
|
||||
'stations_to_show'
|
||||
@ -843,7 +845,7 @@ def get_menu_back_buttons(bot, update, user_record,
|
||||
return buttons
|
||||
|
||||
|
||||
async def _ciclopi_button_main(bot, update, user_record, arguments):
|
||||
async def _ciclopi_button_main(bot, update, user_record):
|
||||
result, text, reply_markup = '', '', None
|
||||
text = (
|
||||
"⚙️ {settings_title} 🚲\n"
|
||||
@ -1094,7 +1096,7 @@ async def _ciclopi_button_show(bot, update, user_record, arguments):
|
||||
return result, text, reply_markup
|
||||
|
||||
|
||||
async def _ciclopi_button_legend(bot, update, user_record, arguments):
|
||||
async def _ciclopi_button_legend(bot, update, user_record):
|
||||
result, text, reply_markup = '', '', None
|
||||
text = (
|
||||
"<b>{s[name]}</b> | <i>{s[description]}</i>\n"
|
||||
@ -1119,7 +1121,6 @@ async def _ciclopi_button_legend(bot, update, user_record, arguments):
|
||||
|
||||
async def _ciclopi_button_favourites_add(bot, update, user_record, arguments,
|
||||
order_record, ordered_stations):
|
||||
result, text, reply_markup = '', '', None
|
||||
result = bot.get_message(
|
||||
'ciclopi', 'button', 'favourites', 'popup',
|
||||
update=update, user_record=user_record
|
||||
@ -1150,12 +1151,6 @@ async def _ciclopi_button_favourites_add(bot, update, user_record, arguments,
|
||||
db['ciclopi_custom_order'].delete(
|
||||
id=old_record['id']
|
||||
)
|
||||
order_record = list(
|
||||
filter(
|
||||
(lambda r: r['station'] != station_id),
|
||||
order_record
|
||||
)
|
||||
)
|
||||
ordered_stations = list(
|
||||
filter(
|
||||
(lambda s: s.id != station_id),
|
||||
@ -1246,6 +1241,8 @@ def move_favorite_station(
|
||||
for old_record in order_record:
|
||||
if old_record['station'] == station_id:
|
||||
break
|
||||
else: # Error: no record found
|
||||
return
|
||||
with bot.db as db:
|
||||
if action == 'down':
|
||||
db.query(
|
||||
@ -1455,7 +1452,7 @@ async def _ciclopi_button_favourites(bot, update, user_record, arguments):
|
||||
return result, text, reply_markup
|
||||
|
||||
|
||||
async def _ciclopi_button_setpos(bot, update, user_record, arguments):
|
||||
async def _ciclopi_button_setpos(bot, update, user_record):
|
||||
result, text, reply_markup = '', '', None
|
||||
chat_id = (
|
||||
update['message']['chat']['id'] if 'message' in update
|
||||
@ -1510,6 +1507,7 @@ async def _ciclopi_button_setpos(bot, update, user_record, arguments):
|
||||
)
|
||||
return result, text, reply_markup
|
||||
|
||||
|
||||
_ciclopi_button_routing_table = {
|
||||
'main': _ciclopi_button_main,
|
||||
'sort': _ciclopi_button_sort,
|
||||
@ -1544,7 +1542,7 @@ async def _ciclopi_button(bot, update, user_record):
|
||||
return result
|
||||
|
||||
|
||||
def init(bot, ciclopi_messages=None, ciclopi_messages_json=None,
|
||||
def init(telegram_bot, ciclopi_messages=None,
|
||||
_default_location=(43.718518, 10.402165)):
|
||||
"""Take a bot and assign CicloPi-related commands to it.
|
||||
|
||||
@ -1558,9 +1556,9 @@ def init(bot, ciclopi_messages=None, ciclopi_messages_json=None,
|
||||
# Define a global `default_location` variable holding default location
|
||||
global default_location
|
||||
default_location = Location(_default_location)
|
||||
bot.ciclopi_default_location = default_location
|
||||
telegram_bot.ciclopi_default_location = default_location
|
||||
|
||||
with bot.db as db:
|
||||
db = telegram_bot.db
|
||||
if 'ciclopi_stations' not in db.tables:
|
||||
db['ciclopi_stations'].insert_many(
|
||||
sorted(
|
||||
@ -1592,21 +1590,21 @@ def init(bot, ciclopi_messages=None, ciclopi_messages_json=None,
|
||||
from .messages import default_ciclopi_messages as ciclopi_messages
|
||||
except ImportError:
|
||||
ciclopi_messages = {}
|
||||
bot.messages['ciclopi'] = ciclopi_messages
|
||||
telegram_bot.messages['ciclopi'] = ciclopi_messages
|
||||
|
||||
@bot.command(command='/ciclopi', aliases=["CicloPi 🚲", "🚲 CicloPi 🔴"],
|
||||
@telegram_bot.command(command='/ciclopi', aliases=["CicloPi 🚲", "🚲 CicloPi 🔴"],
|
||||
reply_keyboard_button=(
|
||||
bot.messages['ciclopi']['command']['reply_keyboard_button']
|
||||
telegram_bot.messages['ciclopi']['command']['reply_keyboard_button']
|
||||
),
|
||||
show_in_keyboard=True,
|
||||
description=(
|
||||
bot.messages['ciclopi']['command']['description']
|
||||
telegram_bot.messages['ciclopi']['command']['description']
|
||||
),
|
||||
help_section=bot.messages['ciclopi']['help'],
|
||||
help_section=telegram_bot.messages['ciclopi']['help'],
|
||||
authorization_level='everybody')
|
||||
async def ciclopi_command(bot, update, user_record):
|
||||
return await _ciclopi_command(bot, update, user_record)
|
||||
|
||||
@bot.button(prefix='ciclopi:///', authorization_level='everybody')
|
||||
@telegram_bot.button(prefix='ciclopi:///', authorization_level='everybody')
|
||||
async def ciclopi_button(bot, update, user_record):
|
||||
return await _ciclopi_button(bot, update, user_record)
|
||||
|
Loading…
x
Reference in New Issue
Block a user