Skip to content
This repository has been archived by the owner on Nov 19, 2018. It is now read-only.

Commit

Permalink
Updated version and database ^^
Browse files Browse the repository at this point in the history
Improved working, smoothness and database connection (added value for registering hours of usage)
  • Loading branch information
Javinator9889 authored Jun 8, 2017
1 parent 93702db commit 1c65b2c
Show file tree
Hide file tree
Showing 22 changed files with 1,217 additions and 853 deletions.
30 changes: 15 additions & 15 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import time # Standard python libraries


logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', # Starting logging at WARNING level (for errors information)
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)

token_file = open("TOKEN.txt", 'r')
Expand All @@ -44,7 +44,7 @@
dispatcher.add_handler(echo_handler)
loc_handler = MessageHandler(Filters.location, lc.location)
dispatcher.add_handler(loc_handler)
pref_handler = CommandHandler('preferences', pf.key_pref)
pref_handler = CommandHandler('config', pf.key_pref)
dispatcher.add_handler(pref_handler)
dev_handler = CommandHandler('develop', dev.develop)
dispatcher.add_handler(dev_handler)
Expand Down Expand Up @@ -74,13 +74,13 @@
updater.start_polling(timeout=30)
print("Starting bot...\n\n")

print("Iniciando programación...")

print("NewsBot (Telegram) Copyright (C) 2017 Javinator9889\n\n\
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.\n\
This is free software, and you are welcome to redistribute it\
under certain conditions; type `show c' for details.")

print("Iniciando programación...")

try:
while 1:
data = db_m.fetch_chatids_progtimes()
Expand All @@ -98,19 +98,17 @@
.strftime("\"%H:%M, %d-%m-%Y on %A\""))
prog.prog(updater.bot, updater, row[0])
prog_hours = db_m.get_time_prog(row[0]).lower().split(",")
if 'lun' in prog_hours[0] or 'mar' in prog_hours[0] or 'mie' in prog_hours[0] or\
'jue' in prog_hours[0] or 'vie' in prog_hours[0] or 'sab' in prog_hours[0] or\
'dom' in prog_hours[0] or 'mon' in prog_hours[0] or 'tue' in prog_hours[0] or\
'wed' in prog_hours[0] or 'thu' in prog_hours[0] or 'fri' in prog_hours[0] or\
'sat' in prog_hours[0] or 'sun' in prog_hours[0]:
if 'lun' in prog_hours[0] or 'mar' in prog_hours[0] or 'mie' in prog_hours[0] or 'jue' in prog_hours[0] or 'vie' in prog_hours[0] or 'sab' in prog_hours[0] or 'dom' in prog_hours[0] or 'mon' in prog_hours[0] or 'tue' in prog_hours[0] or 'wed' in prog_hours[0] or 'thu' in prog_hours[0] or 'fri' in prog_hours[0] or 'sat' in prog_hours[0] or 'sun' in prog_hours[0]:
next_time = add_time.add_week(times[0], row[0], 1)
else:
next_time = add_time.add_day(times[0], row[0], 1)
if times[1] is not None:
final_time = next_time + "," + times[1]
else:
try:
if times[1] is not None:
final_time = next_time + "," + times[1]
except IndexError:
final_time = next_time
db_m.update_prog_time(final_time, row[0])
print("Time scheduled correctly", row[0])
elif times[1] == dt.datetime.now(pytz.timezone("Europe/Madrid")).strftime("%Y-%m-%d %H:%M %a"):
print("Sending news to", row[0], "at time:", dt.datetime.now(pytz.timezone("Europe/Madrid")) \
.strftime("\"%H:%M, %d-%m-%Y on %A\""))
Expand All @@ -124,11 +122,13 @@
next_time = add_time.add_week(times[1], row[0], 2)
else:
next_time = add_time.add_day(times[1], row[0], 2)
if times[0] is not None:
final_time = times[0] + "," + next_time
else:
try:
if times[0] is not None:
final_time = times[0] + "," + next_time
except IndexError:
final_time = next_time
db_m.update_prog_time(final_time, row[0])
print("Time scheduled correctly", row[0])
except IndexError:
# print("Values for", row[0], "are not set-up")
pass
Expand Down
4 changes: 3 additions & 1 deletion functions/add_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ def add_day(text, chat_id, which):
if which == 1 and len(actual) > 1:
act = actual[0]
elif which == 1 and len(actual) < 2:
act = actual
act = actual[0]
elif which == 2:
act = actual[1]
else:
print("Returning none...")
return None

t_zone = db_m.get_time_diff(chat_id)
Expand Down Expand Up @@ -45,6 +46,7 @@ def add_week(text, chat_id, which):
elif which == 2:
act = actual[1]
else:
print("Returning none...")
return None

if 'lun' in act:
Expand Down
47 changes: 47 additions & 0 deletions functions/botan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# ----------------
# dont forget 'pip install requests' first
# ----------------
# usage example:
#
# import botan
#
# print botan.track(1111, 1, {'text':2}, 'Search')

import requests
import json


TRACK_URL = 'https://api.botan.io/track'
SHORTENER_URL = 'https://api.botan.io/s/'


def track(token, uid, message, name='Message'):
try:
r = requests.post(
TRACK_URL,
params={"token": token, "uid": uid, "name": name},
data=json.dumps(message),
headers={'Content-type': 'application/json'},
)
return r.json()
except requests.exceptions.Timeout:
# set up for a retry, or continue in a retry loop
return False
except (requests.exceptions.RequestException, ValueError) as e:
# catastrophic error
print(e)
return False


def shorten_url(url, botan_token, user_id):
"""
Shorten URL for specified user of a bot
"""
try:
return requests.get(SHORTENER_URL, params={
'token': botan_token,
'url': url,
'user_ids': str(user_id),
}).text
except:
return url
Loading

0 comments on commit 1c65b2c

Please sign in to comment.