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