Browse Source

Added config file

itec 4 years ago
parent
commit
dff604e6e9
2 changed files with 39 additions and 16 deletions
  1. 1 0
      .gitignore
  2. 38 16
      playlistalo.py

+ 1 - 0
.gitignore

@@ -3,3 +3,4 @@ cache/**
 fallback/**
 playlist/**
 __pycache__/**
+playlistalo.conf

+ 38 - 16
playlistalo.py

@@ -13,14 +13,49 @@ import time
 import subprocess
 import random
 from telegram.ext import Updater, MessageHandler, Filters
-
+import configparser
 
 scriptpath = os.path.dirname(os.path.realpath(__file__))
 
+#Crea le cartelle
+if not os.path.exists("playlist"):
+    os.makedirs("playlist")
+if not os.path.exists("cache"):
+    os.makedirs("cache")
+if not os.path.exists("fallback"):
+    os.makedirs("fallback")
+if not os.path.exists("archive"):
+    os.makedirs("archive")
+
+#Scrivi la prima configurazione
+configfile = 'playlistalo.conf'
+if not os.path.exists(configfile):
+    config = configparser.ConfigParser()
+    config['playlistalo'] = {'ShuffleUsers': False, 'ShuffleFallback': False, 'Telegram_token': "", 'Mastodon_token': ""}
+    with open(configfile, 'w') as f:
+        config.write(f)
+
+#Leggi la configurazione
+config = configparser.ConfigParser()
+config.read(configfile)
+playlistaloconf = config['playlistalo']
+SHUFFLEUSERS = playlistaloconf.getboolean('ShuffleUsers')
+SHUFFLEFALLBACK = playlistaloconf.getboolean('ShuffleUsers')
+TELEGRAM_TOKEN = playlistaloconf.get('Telegram_token')
+MASTODON_TOKEN = playlistaloconf.get('Mastodon_token')
+
+
+
+
+
+
+
+
+
+
 
 def add(url, user = "-unknown-", sortrandom = False):
     #print ('--- Inizio ---')
-    init()
 
     ydl_opts = {
         'format': 'bestaudio[ext=m4a]',       
@@ -106,16 +141,6 @@ def __normalizetext(s):
         s = " ".join(s.split())
     return s
     
-def init():
-    if not os.path.exists("playlist"):
-        os.makedirs("playlist")
-    if not os.path.exists("cache"):
-        os.makedirs("cache")
-    if not os.path.exists("fallback"):
-        os.makedirs("fallback")
-    if not os.path.exists("archive"):
-        os.makedirs("archive")
-
 
 def list():
     pl = []
@@ -216,9 +241,6 @@ def playloop():
     while True:
         playsingle()
 
-
-
-
 def clean():
     #cancella tutto dalla playlist
     shutil.rmtree("playlist")
@@ -270,7 +292,7 @@ def __telegram_msg_parser(bot, update):
 def telegram_bot():
     print ("Bot avviato")
     # Create the EventHandler and pass it your bot's token.
-    updater = Updater(os.environ['TELEGRAM_TOKEN'])
+    updater = Updater(TELEGRAM_TOKEN)
     # Get the dispatcher to register handlers
     dp = updater.dispatcher
     # parse message