Example #1
0
def start_auth(message):
    con,cur = get_mysql_cursor(mysql)
    user_id = message["from"]["id"]
    cur.callproc('check_tg_user',[user_id])
    con.commit()
    cur.close()
    return render_template("welcome_auth.txt", user_id=user_id, client_id=app.config["picarto_client_id"])
Example #2
0
def get_followers(message):
    con,cur = get_mysql_cursor(mysql)
    tguser_id = message["from"]["id"]
    cur.execute("SELECT * FROM `users` WHERE `tguser_id` = %s",[tguser_id])
    user = mysql_fetch_assoc(cur)[0]
    channels = picarto_profiles.get_channels_followed(user["picarto_access_token"])
    names = [ x["name"] for x in channels.values() ]
    return "\n".join(names)
Example #3
0
def delete_user(message):
    params = bot.find_params(message["text"])
    if params == None:
        return "You must add 'yes' as an argument"
    elif params == "yes":
        con,cur = get_mysql_cursor(mysql)
        cur.execute("DELETE FROM `users` where `tguser_id` = %s",[message["from"]["id"]])
        con.commit()
        return "Sorry to see you go! Come back soon!"
    else:
        return "Didn't understand what you said. You must say 'yes' after the command."
Example #4
0
def get_online_streamers(message):
    con,cur = get_mysql_cursor(mysql)
    tguser_id = message["from"]["id"]
    #bot.send_message(tguser_id,"One sec...")
    cur.execute("SELECT * FROM `users` WHERE `tguser_id` = %s",[tguser_id])
    user = mysql_fetch_assoc(cur)[0]
    channels = picarto_profiles.get_online_channels_followed(user["picarto_access_token"])
    #channels_followed = [ x["user_id"] for x in picarto_profiles(results[1])["following"]]
    #print(str(channels))
    names = [ x["name"] + (" (NSFW)" if x["adult"] else "") for x in channels.values() if (int(x["adult"]) <= user["show_nsfw"]) and (int(x["gaming"]) <= user["show_games"]) ]
    names_string = "\n".join(names)
    if len(names_string) > 0:
        return "These people are streaming:\n" + names_string
    else:
        return "No one's currently streaming that you follow."
Example #5
0
def picarto_auth():
    if (request.args.get("code") == None) or (request.args.get("state") == None):
        return render_template("auth_failed.html") + " There's not needed args."
    con,cur = get_mysql_cursor(mysql)
    user_id = request.args.get("state")
    cur.callproc('check_tg_user',[user_id])
    data = cur.fetchall()
    if len(data) is 0:
        cur.execute("delete from `users` where `tguser_id` = %s", [user_id])
        return render_template("auth_failed.html") + " Your user ID isn't set up yet."
    data = { "code":request.args.get("code"),\
        "redirect_uri" : "https://puffydrake.com/oauth.php",\
        "client_id" : app.config["picarto_client_id"],\
        "client_secret" : app.config["picarto_client_secret"],\
        "scope" : "readpriv", "grant_type" : "authorization_code"}
    r = requests.post("https://oauth.picarto.tv/token", data = data)
    response = r.json()
    url = "https://api.telegram.org/bot" + app.config["TG_API_KEY"] + "/getChat"
    ur = requests.get(url,json={"chat_id":user_id})
    tgusername = ur.json()["result"]["username"]
    try:
        dt = timedelta(seconds=response["expires_in"]-86400)
        today = datetime.today()
        expire_datetime = today + dt
        cur.execute("UPDATE `users` SET `picarto_access_token` = %s, `picarto_refresh_token` = %s, `picarto_token_expires` = %s WHERE `tguser_id` = %s",\
            (response["access_token"], response["refresh_token"], expire_datetime,user_id))
        con.commit()
        cur.close()
        con.close()
        bot.send_message(user_id,render_template("auth_success.txt"))
        msg = "@%s has started using me!" % tgusername
        bot.send_message(54326855,msg)
    except:
        bot.send_message(54326855,"Something went wrong with @%s getting access. Please check the logs." % tgusername)
        bot.send_message(user_id,"I'm really sorry but something went wrong. @Ceralor is looking into it.")
        import pprint
        print("Error authorizing picarto for %s" % tgusername)
        print("HTTP Status Code: %s" % str(r.status_code))
        try:
            pprint.pprint(r.json())
        except:
            pprint.pprint(r.text)
        return "Something went wrong, sorry. We're looking into it."
    return render_template("auth_success.html")
Example #6
0
def get_settings(message):
    con,cur = get_mysql_cursor(mysql)
    tguser_id = message["from"]["id"]
    cur.execute("SELECT * FROM `users` WHERE `tguser_id` = %s LIMIT 1",[tguser_id])
    results = mysql_fetch_assoc(cur)
    return "Muted: "+str(bool(results[0]["paused"]))
#!/usr/bin/python3
from app import app, bot, mysql
from helpers import get_mysql_cursor, mysql_fetch_assoc
from flask import url_for, render_template
from picarto_profiles import *
import requests, json

con, cur = get_mysql_cursor(mysql)
cur.execute(
    "SELECT * FROM `users` WHERE `paused` = 0 and `picarto_access_token` is not null"
)
unpaused_users = mysql_fetch_assoc(cur)
cur.close()
con.close()

for user in unpaused_users:
    online_pull = get_online_channels_followed(user["picarto_access_token"])
    prev_on_json = user["picarto_lastonline"] or "[]"
    previous_online = json.loads(prev_on_json)
    if len(online_pull) > 0:
        streams = online_pull.values()
        new_streams = [ stream for stream in streams if (int(stream["adult"]) <= user["show_nsfw"]) \
            and (int(stream["gaming"]) <= user["show_games"]) \
            and (stream["user_id"] not in previous_online) ]
        ##and (stream["private"] == False) ]
        if len(new_streams) > 0:
            for stream in new_streams:
                streamer_name = stream["name"]
                suffix = (" (NSFW)" if stream["adult"] else
                          "") + (" (Game)" if stream["gaming"] else "")
                preview_url = "https://relay.omgdragons.com:8443/picarto_preview/" + stream[