<commit_before>#!/usr/bin/env python2

"""blockify:
    Mute adverts on spotify (wine version only)

    Usage:
    When you find an advert you want to mute, you need to add it to
    ~/.blockify_list either manually (find out the name with wmctrl -l) or via:
    pkill -USR1 -f python2.*blockify
"""

import subprocess
import time
import sys
import os
import signal

# Check if we can use the wnck library, otherwise resort to wmctrl.
use_wnck = False
try:
    import wnck
    import gtk
    import pygtk
    pygtk.require('2.0')
    use_wnck = True
except ImportError:
    try:
        devnull = open(os.devnull)
        subprocess.Popen(["wmctrl"], stdout=devnull, stderr=devnull).communicate()
    except OSError:
        print "Please install wnck or wmctrl first."
        sys.exit(1)

# Globals
VERSION=0.5
spotify = "Spotify - "
is_muted = True
home = os.path.expanduser("~")
SONGFILE = os.path.join(home, ".blockify_list")

#########################################
# Functions that work with the SONGFILE #
#########################################

def load_song_list():
    try:
        song_file = open(SONGFILE, "r")
    except IOError:
        # If SONGFILE didn't exist create it.
        song_file = open(SONGFILE, "w")
        song_file.write("")
        song_file.close()
        return []
    
    song_list = song_file.read()
    song_file.close()

    # Split the list into lines.
    song_list = song_list.split("\n")
    
    # Remove all empty lines.
    clean_list = [i for i in song_list if len(i.strip())]
    song_list = clean_list

    # Return song list for any function that may need it.
    return song_list

def add_to_list(new_song):
    print 'Adding', new_song, 'to', SONGFILE

    # Read in the old .blockify_list.
    song_list_file = open(SONGFILE, "r")
    current_song_list = song_list_file.read()
    song_list_file.close()
    
    # Add item to song list
    new_list = current_song_list + "\n" + new_song
    song_list_file = open(SONGFILE, "w")
    song_list_file.write(new_list)
    song_list_file.close()
    
    # Reload song list, and return new song list.
    return load_song_list()

##############################################
# Functions that retrieve the song from wine #
##############################################

def get_windows():
    if use_wnck:
        # Get the current screen.
        screen = wnck.screen_get_default()

        while gtk.events_pending():
            gtk.main_iteration(False)

        # Object list of windows in screen.
        windows = screen.get_windows()
        # Actual window list.
        return [win.get_name() for win in windows if len(windows)]
    else:
        pipe = subprocess.Popen(['wmctrl', '-l'], stdout=subprocess.PIPE).stdout
        return pipe.read().split("\n")
 
######################################
# Functions that get the actual song #
######################################

def get_current_song():
    current_song = ""

    # Check if a Spotify window exists and return the current songname.
    pipe = get_windows()
    for line in pipe:
        if (line.find(spotify) >= 0):
            # Remove "Spotify - " and assign the name.
            if use_wnck:
                current_song = " ".join(line.split()[2:])
            else:
                current_song = " ".join(line.split()[5:])<commit_msg>small fix in get_windows()<commit_after>#!/usr/bin/env python2

"""blockify:
    Mute adverts on spotify (wine version only)

    Usage:
    When you find an advert you want to mute, you need to add it to
    ~/.blockify_list either manually (find out the name with wmctrl -l) or via:
    pkill -USR1 -f python2.*blockify
"""

import subprocess
import time
import sys
import os
import signal

# Check if we can use the wnck library, otherwise resort to wmctrl.
use_wnck = False
try:
    import wnck
    import gtk
    import pygtk
    pygtk.require('2.0')
    use_wnck = True
except ImportError:
    try:
        devnull = open(os.devnull)
        subprocess.Popen(["wmctrl"], stdout=devnull, stderr=devnull).communicate()
    except OSError:
        print "Please install wnck or wmctrl first."
        sys.exit(1)

# Globals
VERSION=0.5
spotify = "Spotify - "
is_muted = True
home = os.path.expanduser("~")
SONGFILE = os.path.join(home, ".blockify_list")

#########################################
# Functions that work with the SONGFILE #
#########################################

def load_song_list():
    try:
        song_file = open(SONGFILE, "r")
    except IOError:
        # If SONGFILE didn't exist create it.
        song_file = open(SONGFILE, "w")
        song_file.write("")
        song_file.close()
        return []
    
    song_list = song_file.read()
    song_file.close()

    # Split the list into lines.
    song_list = song_list.split("\n")
    
    # Remove all empty lines.
    clean_list = [i for i in song_list if len(i.strip())]
    song_list = clean_list

    # Return song list for any function that may need it.
    return song_list

def add_to_list(new_song):
    print 'Adding', new_song, 'to', SONGFILE

    # Read in the old .blockify_list.
    song_list_file = open(SONGFILE, "r")
    current_song_list = song_list_file.read()
    song_list_file.close()
    
    # Add item to song list
    new_list = current_song_list + "\n" + new_song
    song_list_file = open(SONGFILE, "w")
    song_list_file.write(new_list)
    song_list_file.close()
    
    # Reload song list, and return new song list.
    return load_song_list()

##############################################
# Functions that retrieve the song from wine #
##############################################

def get_windows():
    if use_wnck:
        # Get the current screen.
        screen = wnck.screen_get_default()

        while gtk.events_pending():
            gtk.main_iteration(False)

        # Object list of windows in screen.
        windows = screen.get_windows()
        # Actual window list.
        return [win.get_name() for win in windows if len(windows)]
    else:
        pipe = subprocess.Popen(['wmctrl', '-l'], stdout=subprocess.PIPE).stdout
        # Split the lines into a window list.
        return pipe.read().split("\n")
 
######################################
# Functions that get the actual song #
######################################

def get_current_song():
    current_song = ""

    # Check if a Spotify window exists and return the current songname.
    pipe = get_windows()
    for line in pipe:
        if (line.find(spotify) >= 0):
            # Remove "Spotify - " and assign the name.
            if use_wnck:
                current_song = " ".join(line.split()[2:])
            else:
                current_song = " ".join(line.split()[5:])
{'fraction_non_alphanumeric': 0.16976917349218168, 'fraction_numerical': 0.003276247207743857, 'mean_word_length': 2.826780626780627, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '166284', 'n_tokens_mistral': 2141, 'n_tokens_neox': 2049, 'n_words': 603}