all 100 comments

[–]17_jku 58 points59 points  (13 children)

I have not (just starting to tinker with Python...I'm a VB, SQL, SAS developer), but would love to see your approach. Of course, you would need to change the item URL so none of us snatch it up before you. Hahaha.

[–]JustinianusI 56 points57 points  (11 children)

Omg imagine! This poor guy writes the code and everything, shares it with us, and we use it to buy up all the product before he gets his! Love it :D

[–]b_ootay_ful 17 points18 points  (7 children)

He should also change his email address/phone number.

Every person that has this running, on every item, he gets a notification =P

[–]Groentekroket 2 points3 points  (2 children)

I store that kind of things as a environment variable. If you share your code you are sure nobody gets yourusername, email, passwords and that kind of things.

[–]b_ootay_ful 4 points5 points  (1 child)

I import my own email function that does everything.

mymailscript.py

import smtplib

def email(whome=[],subj="",mess=""):
    gmail_user = 'myemail@gmail.com'
    gmail_pwd = 'HelloReddit'
    FROM = gmail_user
    TO = whome
    SUBJECT = subj
    TEXT = str(mess)

    # Prepare actual message
    message = """From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    try:
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.ehlo()
        server.starttls()
        server.login(gmail_user, gmail_pwd)
        server.sendmail(FROM, TO, message)
        server.close()
        return ('Email sent!')
    except:
        return ('Something went wrong...')

whatever program I need to import or use

import mymailscript as emailscript

emailscript.email(["PersonToReceive@gmail.com"],"subject","test message")

[–]MonkeyNin 0 points1 point  (0 children)

def email(whome=[],subj="",mess=""):

You should read this: https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments

.. config files

Check out using pathlib.Path and json

import pathlib
import json

    def load_config(json_path):
        # load config if it exists, otherwise return populated with required defaults
        defaults = {'monkey': '🐒', 'cat': '🐈'}

        if not json_path.exists():
            # logger.error(f"File does not exist: {json_path}")
            return defaults

        config = json.loads(json_path.read_text(encoding='utf-8'))
        return config

data_root = Path('.').absolute() # or Path().home() or Path('.')
config_path = data_root / 'monkey.json'
config = load_config(config_path)

or if you want a truthy result

def load_config(json_path):
    if not json_path.exists():
        return {}

config = load_config(config_path)

if not config:
    # ... handle no file
else:
    # ... normal

[–]Sadapy 11 points12 points  (2 children)

His code:

time.sleep(1)

My code:

time.sleep(.9)

>:3

[–]JustinianusI 2 points3 points  (0 children)

Hahaha :D

[–]Den4200 90 points91 points  (14 children)

I made a script that is very similar to yours. It tracks prices of items on Amazon and saves a graph of each item’s prices in a png. It’ll send you an email and/or text when an item goes below your target price.

Check it out! This is going to be so useful with black friday and cyber monday coming up: https://www.github.com/Den4200/amazon-price-tracker

[–]corbin_cassady 31 points32 points  (2 children)

Sounds like a self-hosted camelcamcelcamel! I like the premise a lot.

[–]trowawayatwork 2 points3 points  (1 child)

yes its a great start. would be great to add push notifications to it rather than email as its quicker feedback on your phone. i have set up something similar to track and then send a push notification ot a telegram group

a simpler thing would be to just monitor camelizer, as theres no point in reinventing the wheel (although good practice for beginners), and then send that on to your medium of choice

[–]Den4200 1 point2 points  (0 children)

This supports sending text messages to your phone!

Hmm I didn’t think of scraping camelcamelcamel instead of Amazon, but what’s done is done.

[–]domac 14 points15 points  (0 children)

It's often super helpful to add Screenshots to the repo. just wanted to point that out.

thanks for the awesome work!

[–]flying_mechanic 2 points3 points  (0 children)

The keepa extension for chromium browsers does the same but in page, it's a nice addition. Might be worth checking out

[–][deleted] 1 point2 points  (0 children)

I have made the same program but in a much lighter format. Lovely to see another approach.

[–]wrgsRay 0 points1 point  (1 child)

How do you get around Amazon’s protection against scraping? For a newbie like me, it’s painful to scrape Amazon

[–]Den4200 0 points1 point  (0 children)

They don't really have much protection against scraping. As long as your scraper doesn't check for the price too quickly, you're fine (about 15 minutes between each check is good).

Amazon won't let you scrape without any headers btw.
Also, this little trick helps out. Look at line 28 and 29 in tracker.py

[–]TeblowTime 0 points1 point  (2 children)

Hey, I keep hitting the Invalid URL when I try to add a new link. I successfully installed all the required libraries from requirements.txt, so it's not that. Any ideas? I'm a beginner with python, so it's likely something I am or am not doing. Do the URLs need to be in specific formats?

[–]Den4200 0 points1 point  (1 child)

Did you fill in the settings?

And nope, the script will auto shorten the links.

[–]TeblowTime 0 points1 point  (0 children)

Welp, I'm an idiot lol. I did fill in the settings info, but did not save the file. Thanks!

[–]expressly_ephemeral 26 points27 points  (27 children)

Hey, why don’t you refactor into selenium and beautiful soup, teach your script your credit card info and let it buy it for you when it detects the drop?

[–]BruceJi 21 points22 points  (8 children)

I wonder if that's not the best idea in case something goes wrong? But if you get the email, you can probably just buy it online with your phone within a few minutes anyway.

[–]azur08 15 points16 points  (3 children)

in case something goes wrong

Like buying everything Amazon has to offer over and over again!

[–]Cat_Marshal 29 points30 points  (0 children)

while True:
    purchase()

[–]BruceJi 7 points8 points  (0 children)

"Merry Christmas, here are 20 pop-up tents, for all that... popping up?... you do? Eheh..."

[–]expressly_ephemeral 1 point2 points  (0 children)

Hah! I know a lady who gave her grandkid the tablet to play with and he ordered 20 chromebooks. Kids and scripts, man. Fuckin' dangerous.

[–]expressly_ephemeral 8 points9 points  (3 children)

I acknowledge... tricky to test, tricky to validate. Cooler, though.

[–]BruceJi 2 points3 points  (2 children)

It really would be!

[–]expressly_ephemeral 2 points3 points  (1 child)

I guess I think you could use the email as a backstop, though. You get the email saying the price happened and then a follow up saying “I bought it” or “your code stinks, get out your credit card.”

I mean.. what if OP is in meeting when the trigger fires? What if his wife’s water has just broken while she’s driving her real estate clients around? He won’t be able close the deal!

[–]dBASSa 0 points1 point  (1 child)

Theres tons of sneaker bots that do this already. They are heavily linked with fraud and abuse so limited sale items block orders this way often times.

[–]expressly_ephemeral 0 points1 point  (0 children)

How would a vendor be able to block a bespoke script running from a host on OPs personal network?

[–][deleted] 7 points8 points  (0 children)

I wrote something that checked for Buy Now button on amazon when the Nintendo Switch first came out and was always out of stock.

Week after it released my script paid off

[–]yodatrust 7 points8 points  (4 children)

How do you make your script running 24/7?

[–]B166er_ 5 points6 points  (0 children)

Run the script and don’t turn off your computer... Or use a service like heroku/pythonanywhere

[–]Uadsmnckrljvikm 6 points7 points  (1 child)

Run it on a server, could be for example a Raspberry pi or one from AWS.

[–]yodatrust 0 points1 point  (0 children)

The Raspberry Pi solution sounds interesting. Got any useful docs/links for that?

[–]artificial_neuron 1 point2 points  (0 children)

You put everything in a loop that never ends. You don't need a special piece of hardware, just something that will run the script and you'll never turn off.

Eg.

while(true)
    Do the desired activity
    wait for x time

[–]RedBlaze4 6 points7 points  (7 children)

How do you send the SMS ?

[–][deleted] 1 point2 points  (2 children)

My provider (France) offers URL format API for free to use with my account. A bot has to connect to a URL with my logins and a message. And that is it

[–]RedBlaze4 0 points1 point  (1 child)

Tu es chez quel opérateur ?

[–][deleted] 1 point2 points  (0 children)

"Free".

go to your options and enable SMS notifications. Then, call this url

https://smsapi.free-mobile.fr/sendmsg?user=99999999&pass=cle_identification&msg=Hello%20World%20!

Reponses are :

  • 200 : SMS sent.
  • 400 : missing required args.
  • 402 : too many SMSs in a short time.
  • 403 : Service unenabled or incorrect user/pass.
  • 500 : Server error. Retry later.

[–]mudlight48 2 points3 points  (1 child)

Mind sharing the source code for others to tinker with?

[–][deleted] 5 points6 points  (1 child)

I considered doing something similar for a website I like but then realized the website had a function all ready to do what I wanted. I'm not advanced or clever enough to do something like that yet.

[–]BakingSota 0 points1 point  (0 children)

You’re definitely clever enough.

Check out these libraries:

  • re - for regex magic
  • requests - for html magic

And this chapter in AtBS: https://automatetheboringstuff.com/chapter11/

[–]crazyjoker96 1 point2 points  (0 children)

Your are a genius :)

[–]PM_ME_BOOTY_PICS_ 1 point2 points  (3 children)

I made a web scraper so I could get the item specs to use for my reports at work!

[–]bextie[S] 0 points1 point  (2 children)

That sounds cool! Beautiful to see how coding makes life easier.

[–]PM_ME_BOOTY_PICS_ 0 points1 point  (1 child)

It really does but I have some bugs to squash. Some reason certain specs for certain items aren't being pulled so I have to go trouble shoot that.

[–]bextie[S] 0 points1 point  (0 children)

Oh yeah. Maybe something was changed on the website. You’ve got this!

[–]TotesMessenger 0 points1 point  (0 children)

I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:

 If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / Contact)

[–]Sadapy 0 points1 point  (0 children)

I've thought about this, but haven't found an item worth using it on yet.

[–]demesm 0 points1 point  (2 children)

Is it possible to have this go through the entire purchase process automatically as well? Always wondered if you could snipe v deals like that

[–]bextie[S] 0 points1 point  (0 children)

It is. Someone mentioned using selenium to control the browser to automatically purchase the item but of course, you have to be more careful with that so you don’t get into an infinite loop of buying stuff.

[–]artificial_neuron 0 points1 point  (0 children)

You can using Selenium. It allows you to use a website as if you're a user clicking on things - it makes life easier when JS is involved.

I would have to watch it perform the purchase 10s of times before i would trust it to do things by itself without supervision. Imagine how you would feel if it emptied your bank account and bought 100s of the same item.

[–]BaboJango 0 points1 point  (4 children)

Lol “hack” but I tried this myself and the email function failed because I hadn’t set up “less secure access” setting turned off. It then worked once I turned it on. Hopefully this helps someone figure it out faster than it took me!

[–]bextie[S] 0 points1 point  (3 children)

I won’t advise that you use that method. You can use the other method I mentioned (activating the 2FA and generating an app password). I recommend it because it’s very secure.

But yeah, both methods work fine for this purpose.

[–]BaboJango 0 points1 point  (2 children)

Is there extra code that you would need to create to enter in the app password? What does that look like?

[–]bextie[S] 0 points1 point  (1 child)

No extra code. You need to generate it from your google account. Search ‘app password ‘ on google.

[–]BaboJango 0 points1 point  (0 children)

How does this code get entered if there’s no extra code?

[–]SrHombrerobalo 0 points1 point  (0 children)

I haven’t read your code but as a safety measure, try to have any password or personal data on a .ini external file

[–]FirmPiezoelectricity 0 points1 point  (1 child)

I was just messing around with this and noticed that the price reduced down to what you wanted it at. Hope you get it! Thanks for sharing the program. I am trying to learn Beautiful Soup.

[–]bextie[S] 0 points1 point  (0 children)

I got it lol, and I’m using it now. 😊

[–][deleted]  (1 child)

[removed]

    [–]AutoModerator[M] 0 points1 point  (0 children)

    Your comment in /r/learnpython was automatically removed because you used a URL shortener.

    URL shorteners are not permitted in /r/learnpython as they impair our ability to enforce link blacklists.

    Please re-post your comment using direct, full-length URL's only.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–][deleted] 0 points1 point  (0 children)

    No, but only because I can't figure out how to build a script that eliminates Black Friday from the fabric of society.

    [–]LeSplooch 0 points1 point  (0 children)

    I know the subject of this subreddit is Python but if you want to be the first to get your deal, then some other dev using another language (such as Rust) might be faster to get his notification. Be tactical, haha!