]> git.leonardobizzoni.com Git - holonotify/commitdiff
Send notification and quit if there's another instance running
authorLeonardoBizzoni <leo2002714@gmail.com>
Wed, 18 Aug 2021 09:14:01 +0000 (11:14 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Wed, 18 Aug 2021 09:14:01 +0000 (11:14 +0200)
holonotify.py

index 955f2659286f0c6f261bbde8364a8568d7e3b871..a2623afe44477d059dc9ea9e85289b42a64c7bb2 100755 (executable)
@@ -3,6 +3,7 @@
 import os
 import threading
 import time
+import psutil
 
 import urllib.request
 import json
@@ -21,11 +22,8 @@ def updateValues():
         live = holoapi["live"]
         ended = holoapi["ended"]
 
-    time.sleep(60*5)
-    updateValues()
-
-
 def main():
+    updateValues()
     for ch in live:
         notBlacklist = True
         chName = ch["channel"]["name"].lower()
@@ -43,13 +41,29 @@ def main():
         if(ch["yt_video_key"] in ended):
             liveAnnouced.remove(ch["channel"]["name"])
 
-    time.sleep(60*5)
-
 if __name__ == "__main__":
-    thread = threading.Thread(target=updateValues)
-    thread.start()
+    running = True
+    checkedPs = False
+    psCount = 0
+    blacklist = utils.getBlacklist()
 
-    time.sleep(5)
-    while True:
-        blacklist = utils.getBlacklist()
+    while running:
         main()
+
+        # If there's another instance running, show who's live and quit
+        if checkedPs == False:
+            for pid in psutil.pids():
+                    try:
+                        p = psutil.Process(pid)
+                        if p.name() == "python":
+                            if psCount >= 1:
+                                running = False
+                                break
+                            if "holonotify" in p.cmdline()[1]:
+                                psCount +=  1
+                    except:
+                        continue
+            checkedPs = True
+
+        if running:
+            time.sleep(60*5)