3 ms·
Show HN: Intriguing Python One-Liner
- nvusuvu 7y agoThat is very interesting. Would love to know what is going on here.
- wvlia5 7y agoStay tuned for updates
- timeattack 7y agoNothing really interesting. It just following python script encoded in binary mapped to utf-8 characters: song=b'<truncated>' class PlaysoundException(Exception): pass def _playsoundWin(sound, block = True): from ctypes import c_buffer, windll from random import random from time import sleep from sys import getfilesystemencoding def winCommand(*command): buf = c_buffer(255) command = ' '.join(command).encode(getfilesystemencoding()) errorCode = int(windll.winmm.mciSendStringA(command, buf, 254, 0)) if errorCode: errorBuffer = c_buffer(255) windll.winmm.mciGetErrorStringA(errorCode, errorBuffer, 254) exceptionMessage = (' Error ' + str(errorCode) + ' for command:' + ' ' + command.decode() + ' ' + errorBuffer.value.decode()) raise PlaysoundException(exceptionMessage) return buf.value alias = 'playsound_' + str(random()) winCommand('open "' + sound + '" alias', alias) winCommand('set', alias, 'time format milliseconds') durationInMS = winCommand('status', alias, 'length') winCommand('play', alias, 'from 0 to', durationInMS.decode()) if block: sleep(float(durationInMS) / 1000.0) def _playsoundOSX(sound, block = True): from AppKit import NSSound from Foundation import NSURL from time import sleep if '://' not in sound: if not sound.startswith('/'): from os import getcwd sound = getcwd() + '/' + sound sound = 'file://' + sound url = NSURL.URLWithString_(sound) nssound = NSSound.alloc().initWithContentsOfURL_byReference_(url, True) if not nssound: raise IOError('Unable to load sound named: ' + sound) nssound.play() if block: sleep(nssound.duration()) def _playsoundNix(sound, block=True): if not block: raise NotImplementedError( "block=False cannot be used on this platform yet") import os try: from urllib.request import pathname2url except ImportError: from urllib import pathname2url import gi gi.require_version('Gst', '1.0') from gi.repository import Gst Gst.init(None) playbin = Gst.ElementFactory.make('playbin', 'playbin') if sound.startswith(('http://', 'https://')): playbin.props.uri = sound else: playbin.props.uri = 'file://' + pathname2url(os.path.abspath(sound)) set_result = playbin.set_state(Gst.State.PLAYING) if set_result != Gst.StateChangeReturn.ASYNC: raise PlaysoundException( "playbin.set_state returned " + repr(set_result)) bus = playbin.get_bus() bus.poll(Gst.MessageType.EOS, Gst.CLOCK_TIME_NONE) playbin.set_state(Gst.State.NULL) from platform import system system = system() if system == 'Windows': playsound = _playsoundWin elif system == 'Darwin': playsound = _playsoundOSX else: playsound = _playsoundNix del system try: import base64, os, threading def play(): try: fname='./rickroll018377.mp3' with open(fname,'wb') as f: f.write(base64.b64decode(song)) playsound(fname) os.remove(fname) except:pass threading.Thread(target=play).start() except: pass a,t,m="ALERT!! : ","You've been RickRolled",'''Never gonna give you up, Never gonna let you down, Never gonna run around and desert you.''' try: from tkinter import Tk, messagebox;Tk().withdraw();messagebox.showwarning(a+t,m) except: try: import Tkinter,tkMessageBox;Tkinter.Tk().withdraw();tkMessageBox.showwarning(a+t,m) except: print(m,t)