Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

/flaterszaj.py Secret

Created December 24, 2016 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2a2a2c00750bcc47bbee5ca1c78b3d10 to your computer and use it in GitHub Desktop.
Save anonymous/2a2a2c00750bcc47bbee5ca1c78b3d10 to your computer and use it in GitHub Desktop.
# plan był żeby napisać to w jednym pliku i ładnie, czy jest ładnie to nie wiem ale mi się podoba
# na windowsa wymaga notifu.exe w tym samym folderze, do pobrania z https://www.paralint.com/projects/notifu/download.html#Download
# działa na pythonie 3.5
# pep8 może mi pysiora siorbać
import platform
from sys import argv
import time
is_gui = len(argv) >= 2 and argv[1] == "notify"
if(not is_gui):
from requests import get
from threading import Timer
import subprocess
elif(platform.system() == "Windows"):
import webbrowser
import subprocess
elif(platform.system() == "Linux"):
from gi.repository import Notify
from gi.repository import Gtk
from threading import Timer
import webbrowser
BOARDS = ["flutter"] # <-------------------------------- o tu konfig
BASE_URL = 'https://8ch.net'
THREAD_URL = BASE_URL + "/{}/res/{}.html#bottom"
SLEEP_TIME = 45
BROWSER = "" # puste dla domyślnej
script_name = __file__ # inaczej threading coś psuje, ja się tam nie znam
################################################ "backend" ####################################################
def set_interval(func, sec):
def func_wrapper():
set_interval(func, sec)
func()
t = Timer(sec, func_wrapper)
t.start()
return t
class Post:
def __init__(self, board, id):
self.url = THREAD_URL.format(board, id)
self.id = id
self.board = board
class Pajonk:
def __init__(self, board, on_new_post=print):
self.url = '{}/{}/threads.json'.format(BASE_URL, board)
self.threads = []
while(True):
try:
self.threads = self.fetch_threads(self.url)
except Exception as e:
print("init threads exception {}, retrying in 5 seconds...".format(e))
time.sleep(5)
continue
break
self.on_new_post = on_new_post
self.board = board
def fetch_json(self, url):
return get(url).json()
def fetch_threads(self, url):
print("Fetchuje.")
res = self.fetch_json(url)[0]['threads']
return res
def select_new_threads(self, threads):
return [t for t in threads if t not in self.threads]
def update(self):
print("update")
while(True):
try:
threads = self.fetch_threads(self.url)
except Exception as e:
print("fetch threads exception {}, retrying in 5 seconds...".format(e))
time.sleep(5)
continue
break
new_threads = self.select_new_threads(threads)
self.threads = threads
for t in new_threads:
print(new_threads)
self.on_new_post(Post(self.board, str(t["no"])))
def is_get_near(n):
return False # nie dla psa, sam sobie napisz
def handle_new_post(p):
args = [interpreter, script_name, "notify", p.url, p.board]
if is_get_near(p.id): args += ["GET"]
subprocess.Popen(args)
def hello():
subprocess.Popen([interpreter, script_name, "notify", "hello"])
def backend_main():
hello()
for board in BOARDS:
print("startuje", board)
p = Pajonk(board, handle_new_post)
set_interval(p.update, SLEEP_TIME)
####################################################### "gui" dla linuxa ################################################
def linux_notify_new_post(url, board_name, get=False):
Notify.init('8ch notyfikator')
notif = Notify.Notification.new("", "Nowy post na /{}/{}".format(board_name, "\nPS, get blisko!" if get else ""), 'dialog-information')
def on_notify_clicked(notify, action_name):
print("Klikniete")
webbrowser.get(BROWSER if BROWSER else None).open_new_tab(url)
Gtk.main_quit()
notif.add_action('clicked', 'Otwórz', on_notify_clicked)
return notif
def linux_notify_hello():
notif = Notify.Notification.new("Witam", "Notyfikator wystartował", 'dialog-information')
notif.set_timeout(3000)
return notif
def linux_notify(args):
Notify.init('8ch notyfikator')
if args[0] == "hello":
notify = linux_notify_hello()
else:
notify = linux_notify_new_post(*args)
notify.connect('closed', Gtk.main_quit)
notify.show()
Gtk.main()
################################### "gui" dla windowsa ###############################################################
def windows_notify_new_post(url, board_name, get=False):
if subprocess.call(["notifu","/p","Nowy post","/m","Na /{}/{}".format(board_name, "\nPS, get blisko!" if get else "")]) == 3:
webbrowser.get(BROWSER if BROWSER else None).open_new_tab(url)
def windows_notify_hello():
subprocess.call(["notifu", "/d", "3000", "/p", "Witam", "/m", "Notyfikator wystartował"])
def windows_notify(args):
if args[0] == "hello":
windows_notify_hello()
else:
windows_notify_new_post(*args)
#####################################################################################################################
if __name__ == "__main__":
if is_gui:
notify = {
"Windows" : windows_notify,
"Linux" : linux_notify}[platform.system()]
notify(argv[2:])
else:
print("Startuje za 10 sekund bo przy rozruchu systemu jakies dziwne bledy...")
time.sleep(10)
interpreter = {
"Windows" : "pythonw",
"Linux" : "python"}[platform.system()]
backend_main()
# Ale twilight najlepsza.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment