def make_backup(channelid): twitchid = channelid response = "{\n" # Setups setups = database.list_setups(twitchid) response += '\t"setups": [\n' fields = "category", "title", "tags", "mature", "tweet" for setup in setups: setup = {field: setup[field] for field in fields} response += "\t\t" + json.dumps(setup) + ",\n" response += '\t\t""\n\t],\n' # Twitter config (formerly Schedule) [sched_tweet] = database.get_twitter_config(twitchid) response += '\t"twitter_config": [%d],\n' % sched_tweet # Checklist checklist = database.get_checklist(twitchid).strip().split("\n") response += '\t"checklist": [\n' for item in checklist: response += "\t\t" + json.dumps(item) + ",\n" response += '\t\t""\n\t],\n' # Empty string as shim. Ignored on import. # Timers timers = database.list_timers(twitchid, full=True) response += '\t"timers": [\n' for timer in timers: item = dict(zip("id title delta maxtime styling".split(), timer)) response += "\t\t" + json.dumps(item) + ",\n" response += '\t\t""\n\t],\n' # Footer (marker to show that the file was correctly downloaded) # This must NOT include any sort of timestamp, as the backup file # must be completely stable (taking two backups without changing # anything should result in bit-for-bit identical files). response += '\t"": "Mustard-Mine Backup"\n}\n' return Response(response, mimetype="application/json", headers={"Content-disposition": "attachment"})
def mainpage(channelid=None): # NOTE: If we've *reduced* the required scopes, this will still force a re-login. # However, it'll be an easy login, as Twitch will recognize the existing auth. if "twitch_token" not in session or session.get( "twitch_auth_scopes") != REQUIRED_SCOPES: return render_template("login.html") user = session["twitch_user"] if channelid is None: channelid = user["_id"] try: channelid = str(int(channelid)) except ValueError: # If you go to /editor/somename, redirect to /editor/equivalent-id # Bookmarking the version with the ID will be slightly faster, but # streamers will usually want to share the version with the name. users = query("helix/users", token="app", params={"login": channelid})["data"] # users is either an empty list (bad login) or a list of one. if not users: return redirect("/") return redirect("/editor/" + users[0]["id"]) if not may_edit_channel(user["_id"], channelid): return redirect(url_for("mainpage")) database.create_user( channelid ) # Just in case, make sure the database has the basic structure channel = get_channel_setup(channelid) sched_tz, schedule, sched_tweet = database.get_schedule(channelid) if "twitter_oauth" in session: auth = session["twitter_oauth"] username = auth["screen_name"] twitter = "Twitter connected: " + username tweets = list_scheduled_tweets(auth["oauth_token"], auth["oauth_token_secret"], sched_tz) else: twitter = Markup( """<div id="login-twitter"><a href="/login-twitter"><img src="/static/Twitter_Social_Icon_Square_Color.svg" alt="Twitter logo"><div>Connect with Twitter</div></a></div>""" ) tweets = [] error = session.get("last_error_message", "") session["last_error_message"] = "" return render_template( "index.html", twitter=twitter, username=user["display_name"], channel=channel, channelid=channelid, error=error, setups=database.list_setups(channelid), sched_tz=sched_tz, schedule=schedule, sched_tweet=sched_tweet, checklist=database.get_checklist(channelid), timers=database.list_timers(channelid), tweets=tweets, )
def list_setups(channelid): return jsonify(database.list_setups(channelid))