r/Python • u/IMFAILINGENGLISH • Apr 29 '20
I Made This I made a little program that mutes spotify ads because i dont have the money to get premium . Not anything special but i think its quite neat. Any ideas on cool python projects i can build ?
85
u/_in-the-run_ Apr 29 '20
By the way this program will just mute the ad and not completely remove it. Right?
5
→ More replies (8)73
u/badlukk Apr 29 '20
yes. it will just be quiet while the ad plays, then go back to original volume once your music starts again
165
Apr 29 '20 edited Apr 29 '20
46
16
u/adshin21 Apr 29 '20
This is also a cool idea
56
Apr 29 '20
Coolest part is there is no dead air. Spotify skips the ads if it can't connect to the domain so you just get premium without DLs
20
5
u/adshin21 Apr 29 '20
I'm a little curious. Are you using it or do you use regularly. Because I think when a ad will occur Spotify will try to connect the url for the ad and since it is blocked it will take sometime to trying to connect and then Spotify will recieve no connection. Am I right?
PS: I haven't used windows from last 3 years.
→ More replies (3)15
u/SnowdenIsALegend Apr 29 '20
Does this still work? Been a long time that this hosts trick was fixed by Spotify or something... these days i just listen on open.spotify.com in my browser with uBlock Origin & it blocks everything just fine.
For those willing to try it, this is the location of the file: C:\Windows\System32\drivers\etc
→ More replies (7)→ More replies (11)4
u/Trip-3R Apr 29 '20
This only works with older versions of Spotify. The Spotify team patched it and if you do this with the latest version, some songs in your library don't play properly
3
Apr 29 '20
Have you tried this recently? I used to use this a while ago, but have not in about a year
5
u/Trip-3R Apr 29 '20
I tried it just now. Some songs would just stop after a few seconds, then I found this thread
74
u/_in-the-run_ Apr 29 '20
I am so gonna save this and try it out myself. If possible please upload on git or drive and post the link here or to me. Thanks a tonne in advance
6
16
15
u/Captain_PizzaBoii Apr 29 '20
I made a cool project with the Spotify API that counts how many times I listened to a song then it ranks the most listened songs also it shows the total playtime that I’ve listened to Spotify it was pretty cool
2
0
12
u/luhsya Apr 29 '20
add feature: trigger a play (may be local, idk) of rick roll during the muted duration, or smth
4
18
Apr 29 '20
How to use this??
14
u/Phunterrrrr Apr 29 '20
You need to get authorization for the Spotify Web API: https://developer.spotify.com/documentation/web-api/quick-start/
Register a new app to generate a clientId and clientSecret.
So you'd fill in your username, clientId, and clientSecret in the code, then run it. A minor improvement to this script would be to read from a config file of sorts so that the values aren't hard-coded.
8
u/IMFAILINGENGLISH Apr 29 '20
Thats a good idea. As you saw i had to get rid of my client secret n that before uploading
→ More replies (1)
6
u/Ontariel12 Apr 29 '20
There was a similar script some times ago (heck, I still have it saved), but that one would just mute everything while this seems to mute only spotify, so well done OP
2
6
4
4
u/bsliti Apr 29 '20
Nice idea !
Two tips:
use snake case instead of camel case
Use except Exception on a try/except statement, but the better is to except the possible exceptions and not a general exception object.
Good job !
1
2
6
u/redldr1 Apr 29 '20
Stunts like this, is why we lose access to APIs as developers
1
u/IMFAILINGENGLISH Apr 29 '20
sTuNtS lIkE tHis
→ More replies (1)3
u/redldr1 Apr 29 '20
really man,
This kind of shit is why we lose access to APIs.
A good hearty fuck you
→ More replies (1)
1.9k
u/Jager_Beta Apr 29 '20
"i don't have the money so i created a program to solve it"
that should be the anthem of every programer
498
u/f3xjc Apr 29 '20 edited Apr 29 '20
There's also the I don't want to do it 10 times, so I automated it.
Probably took 9 repetitions worth of time to program it, but worth it :P
427
u/cleverfool11 Apr 29 '20
There was a post yesterday that said "never spend 6 minutes doing something manually that can be automated in 6 hours"
(or something to that affect)
184
62
u/dcpye Apr 29 '20
Also 1 week later i forget that i already automated the task and do it manually again
20
u/Terence_McKenna Apr 29 '20
Then you automate it one more time only to ultimately realize the new solution contains 95% of the EXACT code as the forgotten one.
→ More replies (2)15
u/Sardonislamir Apr 29 '20
Depends... if that action is done over 60 times a year, across multiple users, that's time saving the moment it hits a net positive.
Then add the lessons learned and that 6 hours might not be all that wasteful.
43
u/TheAcanthopterygian Apr 29 '20
Obligatory XKCD: https://xkcd.com/1205/
23
u/f3xjc Apr 29 '20
Nice ! But at some point I'm just unhappy doing stuff that feel like a robot task.
30
u/hugthemachines Apr 29 '20
That part is very important in this discussion. Programming to solve a task is creative work. Doing a boring task is kind of depressing. So naturally we prefer making the automation, even if it takes much time.
→ More replies (1)11
u/Sardonislamir Apr 29 '20
The irony in this chart, is that you don't really know how long a task might take you to program until you've tried. The complexity to program isn't necessarily linear with the tasks length.
→ More replies (2)5
u/darin_n Apr 29 '20
I also love the moment you've done something enough times to realize it SHOULD be automated. It's like you're sitting there and it dawns on you. "Waaaaaait a minute..."
→ More replies (10)3
38
u/garlic_bread_thief Apr 29 '20
I don't have the money to buy Windows so I built my own
/s
17
u/alaudet python hobbyist Apr 29 '20
funny but on a grander scale it still works. How many hours invested in creating and maintaining the Linux kernel and the many distributions that use it? (by thousands and thousands of programmers)
How much is it being used? It was totally worth it.
6
3
→ More replies (1)24
Apr 29 '20
I disagree.
Programmers should have money.
Dear manager: If what I do seems like magic to you why are you convinced that I deserve half your pay? What you do seems pretty easy to me.
→ More replies (3)9
u/SolarFlareWebDesign Apr 29 '20
Try managing a bunch of programmers! Jk.
(IMHO I think the only really effective management of a Dev team is a senior-Dev-turned-PM)
370
u/nekogareth Apr 29 '20
i am once again asking for the code
45
u/Zeftax Apr 30 '20
I am not OP so I may have done some typos, but this should be it.
import spotipy import spotipy.util as util import time from pycaw.pycaw import AudioUtilities # ====================================SETUP====================================== # Spotify IDs # username = sys.argvs[0] from command line spotifyUsername = '' spotifyAccessScope = 'user-read-currently-playing user-modify-playback-state' spotifyClientID = '' spotifyClientSecret = '' spotifyRedirectURI = 'http://google.com/' def setupSpotifyObject(username, scope, clientID, clientSecret, redirectURI): token = util.prompt_for_user_token(username, scope, clientID, clientSecret, redirectURI) return spotipy.Spotify(auth=token) def main(): global spotifyObject try: trackInfo = spotifyObject.current_user_playing_track() except: print("Token Expired") spotifyObject = spotifyObject(spotifyUsername, spotifyAccessScope, spotifyClientID, spotifyClientSecret, spotifyRedirectURI) trackInfo = spotifyObject.current_user_playing_track() try: if trackInfo['currently_playing_type'] == 'ad': MuteSpotifyTab(True) else: MuteSpotifyTab(False) except TypeError: pass def MuteSpotifyTab(mute): sessions = AudioUtilities.GetAllSessions() for session in sessions: volume = session.SimpleAudioVolume if session.Process and session.Process.name() == "Spotify.exe": if mute: volume.SetMute(1, None) else: volume.SetMute(0, None) if __name__ == '__main__': setupSpotifyObject = setupSpotifyObject(spotifyUsername, spotifyAccessScope, spotifyClientID, spotifyClientSecret, spotifyRedirectURI) while True: main() time.sleep(0.1)11
→ More replies (2)5
u/taroofique Jun 08 '20
This code gave me errors (even after setting up the Spotify API properly). I think that one way to fix it is to modify the bit after
print("Token Expired")tospotifyObject = setupSpotifyObject(...).→ More replies (2)11
0
Apr 30 '20
[deleted]
8
u/nekogareth Apr 30 '20
"I'll have to sell my lambo to afford the code"
— Linus Tech Tips, creator of linux and git
2
Apr 29 '20
Nice but not strictly necessary. Blocking spotify ads in DNS or with a Firewall is enough
2
2
u/JumboTrout Apr 29 '20
Thanks for actually posting the code. Although a lot put the GitHub link in the comments, I am lazy and like not having to do anything.
1
2
Apr 29 '20
Come draw me a path to learn and give tips and I will give you Spotify and Netflix and Hulu premium.
1
2
u/jzia93 Apr 29 '20
Was wondering what ingenious way you'd managed to identify ads, before I saw the API literally tells you.
Really like it though - simple and clean solution, good job OP
1
u/IMFAILINGENGLISH Apr 29 '20
yh spotify made it super easy to spot ads , each track has a json file with a category currently_playing_type
2
u/strghst Apr 29 '20
Same idea, but mutes ads when running spotify app on your windows PC. Cool catch, well done with figuring out a way with the API :)
2
u/IMFAILINGENGLISH Apr 29 '20
This was my first attempt using API's. Reading docs is a PAIINN
→ More replies (1)
2
u/Lollievoxel Apr 29 '20
Ive had spotify premium for like, 2 years or so, but the fact this even exists just makes me want to go back to being on free
3
2
Apr 29 '20
My poor man's solution is to always have two media players on at the same time. Once one of them starts playing ads, immediately mute it and play music from the other music player for a few minutes. Repeat the process back and forth.
2
2
u/Jmortswimmer6 Apr 29 '20
Spotify Object could be a class.
1
u/IMFAILINGENGLISH Apr 29 '20
Is that meant to be a pun lol
→ More replies (1)2
u/Jmortswimmer6 Apr 29 '20
No, i typically try to point out were people can use class definitions. It makes a lot of things easier
2
2
2
45
u/kunal70006 Apr 29 '20
Goddamn! You should upload it on Github so we can give you stars for that developer clout
23
11
u/ErikBjare Apr 29 '20
What a coincidence, I wrote the PR that added the current_user_playing_track method to spotipy! Really nice to see my small contribution in use!
I wrote it when I was working on aw-watcher-spotify, a small script which tracks what you listen to on Spotify and stores it with ActivityWatch. I did not imagine it could be used for stuff like this!
8
u/Kengaro Apr 29 '20
Cool, "simple" and really neat.
An idea about mute sections: 2 accounts, sync state, switching mute. I dunno if that is doable ;)
8
u/wholl0p Apr 29 '20
As a student you get it for half the price if that helps
5
u/whorestealinglemon Apr 29 '20
Unfortunately Spotify doesn't offer student discounts in every country.
→ More replies (1)
7
u/KingofGamesYami Apr 29 '20
Very cool. I'm going to stick with this myself but good work!
→ More replies (2)
2
2
Apr 29 '20
I love this idea! I hate their advertisement: 'You don't want to hear us anymore? Pay 10$ per month'
1
u/IMFAILINGENGLISH Apr 29 '20
£10 a month is pretty stupid considering netflix and amazon are less
→ More replies (1)2
1
5
5
4
u/Anres6 Apr 29 '20
I am definitely going to be looking into this when my semester ends. (Also first time using remind me bot, so I hope this works)
RemindMe! One Week
2
u/RemindMeBot Apr 29 '20
I will be messaging you in 7 days on 2020-05-06 15:29:53 UTC to remind you of this link
CLICK THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
Info Custom Your Reminders Feedback → More replies (1)
4
u/chris_conlan Apr 29 '20
Very cool. Are you querying the Spotify API once every 0.1 seconds? If so, I would imagine Spotify would eventually complain.
1
u/Appelmoesje Apr 29 '20
I really want this for Soundcloud. My mom pays for Spotify but not SoundCloud :p
1
u/IMFAILINGENGLISH Apr 29 '20
idk if soundcloud has an API, but if it does then someone has probably made a python library that makes it easy to work with.
1
Apr 29 '20
Why for next step don't you add -now and then, or at certain times- audio reminders of things you should do? Would be cool for users to swap ads' time for something more useful
1
1
1
u/ApoorvWatsky Apr 29 '20
Awesome man. Is this the entire code? I suppose this only works on windows? Please let me know. I use Spotify mostly on linux and I could use this.
→ More replies (1)2
u/IMFAILINGENGLISH Apr 29 '20
The pycaw library only works on windows, however there is probably a different library that u could use to mute tabs in linux
1
u/alaudet python hobbyist Apr 29 '20
Cool job. But this feels like something you should only share with your closest friends and swear them to secrecy. :-) If someone "dumbifies" this down to the masses all will be lost.
1
1
u/TMiguelT Apr 29 '20
I know you're probably not here for code review, but consider either refactoring your functions so that they take the spotifyObject as a parameter, or perhaps grouping all these functions into a class so that you don't need to use and update a global variable.
As it is, imagine if someone wanted to control multiple spotify accounts simultaneously, or import your module and re-use the muting functionality without the other parts. They couldn't do it with the current structure.
Oh and maybe look into PEP-8 as a style guide for your variable and function names.
1
u/IMFAILINGENGLISH Apr 29 '20
This is good idea. I could switch accounts while the ad is playing. Thanks for the feed back :)
1
1
1
Apr 29 '20
Great job! Just a little of code review if you don't mind, the convention for python is to use snake_case for functions and variables names instead of camelCase.
1
1
u/officialhovland Apr 29 '20
Does it have to be on computer
3
u/IMFAILINGENGLISH Apr 30 '20
This program inky works on windows but id like to see someone do it on other devices
1
u/edwcarra17 Apr 30 '20
Here is a project. Build out a system metrics collection agent that sends metrics to an api that stores them on a database (of your choosing). Then a way to visualize your data. Either via cli commands or heck built a simple html/javascript browser app to visualize the metrics on graphs.
That should teach you quite a lot.
2
u/IMFAILINGENGLISH Apr 30 '20
That sounds like a challenge, but ill give it a shot.
→ More replies (1)
1
u/dinovfx It works on my machine May 05 '20
install simpleaudio library i'ts always fails with python3.6 or 3.8
how do you do to work whit that?
2
u/IMFAILINGENGLISH May 05 '20
Im using the pygame mixer as an alternative atm . lmk if you know of any better alternatives
3
u/WiggleBooks Apr 29 '20
Any bets on when Spotify will intervene on this?
In a few weeks? In a couple months? Never?
3
u/Zeftax Apr 30 '20
If anyone is too lazy to rewrite it.
import spotipy
import spotipy.util as util
import time
from pycaw.pycaw import AudioUtilities
# ====================================SETUP======================================
# Spotify IDs
# username = sys.argvs[0] from command line
spotifyUsername = ''
spotifyAccessScope = 'user-read-currently-playing user-modify-playback-state'
spotifyClientID = ''
spotifyClientSecret = ''
spotifyRedirectURI = 'http://google.com/'
def setupSpotifyObject(username, scope, clientID, clientSecret, redirectURI):
token = util.prompt_for_user_token(username, scope, clientID, clientSecret, redirectURI)
return spotipy.Spotify(auth=token)
def main():
global spotifyObject
try:
trackInfo = spotifyObject.current_user_playing_track()
except:
print("Token Expired")
spotifyObject = spotifyObject(spotifyUsername, spotifyAccessScope, spotifyClientID, spotifyClientSecret,
spotifyRedirectURI)
trackInfo = spotifyObject.current_user_playing_track()
try:
if trackInfo['currently_playing_type'] == 'ad':
MuteSpotifyTab(True)
else:
MuteSpotifyTab(False)
except TypeError:
pass
def MuteSpotifyTab(mute):
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
volume = session.SimpleAudioVolume
if session.Process and session.Process.name() == "Spotify.exe":
if mute:
volume.SetMute(1, None)
else:
volume.SetMute(0, None)
if __name__ == '__main__':
setupSpotifyObject = setupSpotifyObject(spotifyUsername, spotifyAccessScope, spotifyClientID, spotifyClientSecret,
spotifyRedirectURI)
while True:
main()
time.sleep(0.1)3
u/MathGuy15243 Apr 30 '20
This is so awesome. I'm new to python so always looking for cool ways to use it especially on the data analytics side. Does spotipy also allow you to access your listening data? Would love to design some data collection of my own listening habits.
1
u/FattySuperCute Apr 29 '20
How come you can't afford a Spotify subscription in these times where coding skills are worth gold?
1
1
u/knight7s8man Apr 29 '20
can you explain how to use this script, like do i have to run it every time a ad plays
1
u/BananeHD01 Apr 29 '20
That wouldnt make sense. Its a script which can be run in the background
1
u/IMFAILINGENGLISH Apr 29 '20
You run the application once and it runs in the background while your listening.However when the token expires you have to verify again by copying a link.
1
Apr 29 '20
Does it recognize when an add is playing or do you have to initiate with every ad?
1
u/IMFAILINGENGLISH Apr 29 '20
The program runs in the background and recognises ads
→ More replies (1)
1
Apr 29 '20
Why not hop on some Gig work site, like Fiverr, sell these impressive skills, and acquire money to purchase the things you would enjoy.
2
4
u/theAllSeeingBeaver Apr 29 '20
Instead of muted silence during the ad, it should be a recording of yourself telling you how awesome you are.
→ More replies (2)
2
2
u/Furyat Apr 29 '20
For mobile users : there's already an android app called "ad-free" which does exactly the same thing.
2
u/ouellp Apr 29 '20
Could participate to this one instead of creating a new one all by yourself.
Edit: just realized it's not coded in python. Maybe you could "translate it" to your python project.
2
u/PuzzleheadedSector2 Apr 29 '20
Could somebody tell me how to use this?
3
2
2
u/chexagon Apr 29 '20
Posts like this break me. Cool idea. Good job making something happen. I’m happy that you got to take an idea to completion. The idea that anyone out there gets to do a project at all while my career is over for the medium term so I can take care is my kids makes me cry. The world is really fucked up right now and a large percentage of people went from being allowed to think about things on a daily basis to basically indentured manual labor.
The other day I was thinking about how it would be cool to make an app where parents could sign up to timeshare public playgrounds for day a half hour at a time, so they instead of being closed, they could be used a little bit. But fuck me if I’d have the energy to code that up at the end of the day.
2
u/BootError99 Apr 29 '20
Hi5 I had to make one too!
This is in bash btw https://gist.github.com/Boot-Error/fc73f41bb59afd9b6ff23b07e63b4692
2
2
2
u/Jar70 Apr 29 '20
Can you help me code tic tac toe? I’m not good with coding and I just want to pass this class.
2
2
2
u/WesPeros Apr 29 '20
Looks pretty cool. Now, without me actually trying it out: how does it work? Does it integrate into Spotify desktop app, or does it play from the Python shell? Or is it independent from Spotify player app, and just controls the volume from a different socket?
2
u/Breavyn Apr 29 '20
On linux I had a script that would listen to Spotify events over dbus. The song titles for ads would literally say they were an ad. So the script then sends through the skip command. Worked perfectly.
Now I just use premium.
2
2
u/MooseKnuckleBoxer Apr 29 '20
This is really cool. Thanks for posting.
I was able to get it mostly working, but can't get it to identify an ad. Likely an issue on my end, but projects like this are always fun to play with.
2
2
2
2
2
1
1
u/MacDreBestRapperDead Apr 29 '20
This is awesome. I've wanted to do this for TVs for so long but I can't figure out how. I was thinking using a raspberry pi to detect volume it something... Idk
1
1
1
u/YogahBear Apr 29 '20
Nice idea!
Python question: coming from other languages, it feels like the while loop is suboptimal. What is the Pythonic way to do that nicer? Is there a timer with callback function or such? Or does it not matter in Python?
1
u/wwwrp Apr 29 '20
Nice! Any idea how to do something similar to mute Ads in Youtube when playned from Chromecast?
1
1
1
u/jajca_i_krompira Apr 29 '20
Really cool idea, I love it! But I personally find it easier to close it and reopen when the ad starts playing as that takes far less than waiting for it to finish. This also wouldn't require much changes to your program :)
1
u/Piloux99 Apr 29 '20
Nice
2
u/nice-scores Apr 30 '20
𝓷𝓲𝓬𝓮 ☜(゚ヮ゚☜)
Nice Leaderboard
1.
u/RepliesNiceat 6629 nices2.
u/spiro29at 5296 nices3.
u/DOCTORDICK8at 4085 nices...
275066.
u/Piloux99at 1 niceI AM A BOT | REPLY !IGNORE AND I WILL STOP REPLYING TO YOUR COMMENTS
1
1
u/JamesDotPictures Apr 29 '20 edited May 03 '20
Now add smooth lerping for your volume instead of hard set.
1
1
u/Dbgamerstarz Apr 29 '20
Make a program that downloads your spotify playlist so you dont even need premium
1
1
u/suudo Apr 29 '20
You can replace the if mute: else block with volume.SetMute(int(mute), None), booleans are directly equivalent to 0 and 1
1
1
u/DaWatermeloone Apr 29 '20
Have you heard of jailbreaking (iOS)? r/jailbreak There’s a tweak called spotilife which basically spoofs your account to premium so you have all premium features except for downloads. (Not universal only for the device with the tweak installed)
1
1
u/NewToPython69 Apr 29 '20
whoa that's cool. i'm pretty new to python, how does this work in real life application? do you run the python and it works with spotify?
1
Apr 29 '20
This is awesome but.....if you want a solution without programming you could block the ads on the host file. There is a reddit post somewhere with the domains to block.
1
u/onEstusFlask Apr 29 '20
Very cool and good job. I wonder if the same can be achieve using their open APIs.
1
u/RighteousParanoia Apr 29 '20
This is great! They jack the volume on those ads. they're super annoying and cause hearing damage. I'm surprised they havent caught a class action lawsuit by now. Make one that converts the songs to usable files without spotify and then lets storm the gates of Canada.
1
1
1
482
u/grnngr Apr 29 '20
This functionality getting removed from the API in 3… 2…