Simple-Bookmarking-Service/server.py

81 lines
2.2 KiB
Python
Raw Normal View History

2020-01-19 03:39:47 +00:00
#!/usr/bin/env python3
# Copyright 2020 - David Todd (c0de@c0defox.es)
# Licensed under the MIT License (https://opensource.org/licenses/MIT)
"""
This file contains the web server for a simple bookmarking application
"""
2020-01-20 00:57:35 +00:00
import os
import _thread
2020-01-19 03:39:47 +00:00
from bottle import Bottle, run, response, route, template
from api import API
2020-01-20 00:57:35 +00:00
from telethon.sync import TelegramClient, events
2020-01-19 03:39:47 +00:00
_api = API()
2020-01-20 00:57:35 +00:00
# Load in the API tokens required for a telegram bot
api_id = os.environ.get('telethon_api_id', False)
api_hash = os.environ.get('telethon_api_hash', False)
api_token = os.environ.get('telethon_token', False)
# Create an instance of the Telethon bot
if api_id and api_hash and api_token:
bot = TelegramClient('bot', api_id, api_hash).start(bot_token=api_token)
else:
bot = False
bot_state = False
@bot.on(events.NewMessage)
async def handle_message_events(event):
if 'start' in event.raw_text:
await event.reply('started')
def start_bot_thread():
"""
We run the telegram bot in a seperate thread
to be able to also serve the HTTP content.
The bot thread will be idle most of the time
due to the async nature of it, and the fact
that it won't receive much traffic
"""
bot.start()
bot.run_until_disconnected()
2020-01-19 03:39:47 +00:00
@route('/')
def index():
2020-01-19 04:50:06 +00:00
return "This is the index"
2020-01-19 03:39:47 +00:00
# I haven't figured out how to get these routes inside the API yet...
@route('/save/<title>/<uri:path>')
def save_bookmark(title, uri):
return _api.save_bookmark(title, uri)
@route('/getall')
@route('/get/all')
def get_all_bookmarks():
return _api.get_all_bookmarks()
@route('/get/<bookmark_id>')
def get_bookmark(bookmark_id):
return _api.get_bookmark(bookmark_id)
@route('/delete/<bookmark_id>')
def delete_bookmark(bookmark_id):
return _api.delete_bookmark(bookmark_id)
2020-01-19 04:07:26 +00:00
@route('/update/title/<bookmark_id>/<title>')
def update_bookmark_title(bookmark_id, title):
return _api.update_bookmark_title(bookmark_id, title)
@route('/update/uri/<bookmark_id>/<uri:path>')
def update_bookmark_uri(bookmark_id, uri):
return _api.update_bookmark_uri(bookmark_id, uri)
2020-01-19 03:39:47 +00:00
if __name__ == '__main__':
2020-01-20 00:57:35 +00:00
# Start the Telegram bot and the bottle server
_thread.start_new_thread(start_bot_thread, ())
2020-01-19 03:39:47 +00:00
run(host='localhost', port=8080)