Python monitor shopify

Vibz91

Newbie
Joined
Aug 15, 2018
Messages
23
Reaction score
3
Hello,

I have python skills and I'm looking to create a bot that will monitor websites, and as soon as there are updates I need to be notified on my group discord. But I don't know which libraries to use for that.

For example, on a shoe site there is no stock for a pair of shoes, the purpose of the bot would be that as soon as there is new stock I would be directly notified on discord.

If anyone can help me, that would be cool, thanks !
 
The answer is: mechanize + BeautifulSoup. First one enables you to visit any webpage (its only weakness is Javascript) or even login to a page. The second one will help you parse HTML output.
 
The answer is: mechanize + BeautifulSoup. First one enables you to visit any webpage (its only weakness is Javascript) or even login to a page. The second one will help you parse HTML output.
Thanks for you answer, Is that all it takes? I saw that it was also necessary to import json
 
Thanks for you answer, Is that all it takes? I saw that it was also necessary to import json
These two plugins are enought to visit a page and scrape its contents. You also have to find a library that will send messages to your discord. It's a start, you'll see what's missing as you go :)
 
If your target website has sitemap, you can try to find sitemap. Especially if wordpress, the sitemap will update when you create post.
Then just use request to that url sitemap and parse with Beautiful Soup, is there is change with the latest post. You can run the request maybe for every 5 minutes.

you can also do the same with rss feed.
 
i have some experience helping the sneaker kids scrape shopify sites and make discord hooks.. mechanize is a pretty old library for this task, not even sure its still maintained. if you're in to python, you can crib some of my old notes for reference if you like.

[PYTHON]

Code:
import requests
from requests_html import HTMLSession
import pandas as pd
import json
from pandas.io.json import json_normalize

'''
    lets start by getting the mobile stock json...
    apparently this file represents the total catalog
'''

# r = requests.get("https://www.supremenewyork.com/mobile_stock.json")
# print(r.content)

## output: ( long AF )

# r = requests.get("https://www.supremenewyork.com/shop.json")
# print(r.content)
## output: ( looks good; still long af )

'''
    we should save a copy of the 'all products' desktop page and make use of it...
'''

# r = requests.get("https://www.supremenewyork.com/shop/all")
# print(r.content)
## output: ( bitchen sauce )

'''
    i wonder if we get more clues with a weak attempt to render content hydrated by xhr
    pb: requests-html  =^_^=
'''
session = HTMLSession()
# no magic here, start a requests-html HTMLSesssion() and call it...
# .......
# .......
# .......
# session...

rr_html = session.get("https://www.supremenewyork.com/shop/all")
# print(rr.content)
# output: ( totally identical, i didn't copy pasta here. just take my word for it.
# however, we will continue forward with requests-html ( cause i want to )

'''
    now that we can get some basic product information via the product_catalog.json
    "let's get dangerous" -Darkwing Duck
'''

# jump off with basic features of requests-html

# we can get absolute product links
#print(rr.html.absolute_links)

# output: ( hit the ground running )

'''
    kung-fu pandas
'''

# lets go back for that mobile stock.json from the first few lines..
json = json.loads(session.get("https://www.supremenewyork.com/mobile_stock.json").content)

fake_link_list = []

for link in rr_html.html.absolute_links:
    fake_link_list.append(link)

product_info = []

for l in fake_link_list:
    product_info.append(session.get(l + ".json").content)

print(product_info[0])


# for category in data['products_and_categories']:
#     for product in data['products_and_categories'][category]:
#         print(product)
#
# # and the html from the first fiew lines
# for link in rr_html.html.absolute_links:
 
and another example from yeezy supply with the discord hooks added.... you'll have to do your own bot registration plenty of docs on this.

[PYTHON]
Code:
import json
from bs4 import BeautifulSoup
from requests_html import HTMLSession

import time

import pickle

from DiscordHooks import Hook, Embed, EmbedAuthor, Color

# webhook
webhook = 'CHANGE.ME.discord.webhook.url'

# set up a session
session = HTMLSession()

# name a base url
baseUrl = 'https://yeezysupply.com'
#products = '/products'
#collections = '/collections'

# some ancillary properties
# variant_by_name = boost350black

# url to session object
r = session.get("{}".format(baseUrl))

#page as soup object
soup = BeautifulSoup(r.content, "html.parser")

# now lets start working back through the main page
pageBody = soup.body
feature = pageBody.find('script').text
jsonFeature = json.loads(feature)

# count of products
products_count = jsonFeature['products_count']

# products..
jsonProducts = jsonFeature['products']

#print(jsonProducts)

safePickle = pickle.dumps(jsonFeature['products'])

pickleObject = pickle.loads(safePickle)

while True:
    if jsonProducts == pickleObject and products_count == 8:
        for pp in jsonProducts:

            embed = Embed(
                title="{}{}".format(baseUrl,pp['url']), description=pp['description'])

            Hook(
                hook_url=webhook,
                username="CHANGE.ME.discord.bot.user.name",
                content="id:{}, handle:{}, tags:{}".format(str(pp['id']), pp['handle'], str(pp['tags'])),
                embeds=[embed]).execute()
            time.sleep(10)
    else:
      for pp in jsonProducts:

            embed = Embed(
                title="{}{}".format(baseUrl,pp['url']), description="some items may have changed")
            
            Hook(
                hook_url=webhook,
                username="CHANGE.ME.discord.bot.user.name",
                content="id:{}, handle:{}, tags:{}".format(str(pp['id']), pp['handle'], str(pp['tags'])),
                embeds=[embed]).execute()
            time.sleep(10)
    
    time.sleep(120)
 
and another example from yeezy supply with the discord hooks added.... you'll have to do your own bot registration plenty of docs on this.

[PYTHON]
Code:
import json
from bs4 import BeautifulSoup
from requests_html import HTMLSession

import time

import pickle

from DiscordHooks import Hook, Embed, EmbedAuthor, Color

# webhook
webhook = 'CHANGE.ME.discord.webhook.url'

# set up a session
session = HTMLSession()

# name a base url
baseUrl = 'https://yeezysupply.com'
#products = '/products'
#collections = '/collections'

# some ancillary properties
# variant_by_name = boost350black

# url to session object
r = session.get("{}".format(baseUrl))

#page as soup object
soup = BeautifulSoup(r.content, "html.parser")

# now lets start working back through the main page
pageBody = soup.body
feature = pageBody.find('script').text
jsonFeature = json.loads(feature)

# count of products
products_count = jsonFeature['products_count']

# products..
jsonProducts = jsonFeature['products']

#print(jsonProducts)

safePickle = pickle.dumps(jsonFeature['products'])

pickleObject = pickle.loads(safePickle)

while True:
    if jsonProducts == pickleObject and products_count == 8:
        for pp in jsonProducts:

            embed = Embed(
                title="{}{}".format(baseUrl,pp['url']), description=pp['description'])

            Hook(
                hook_url=webhook,
                username="CHANGE.ME.discord.bot.user.name",
                content="id:{}, handle:{}, tags:{}".format(str(pp['id']), pp['handle'], str(pp['tags'])),
                embeds=[embed]).execute()
            time.sleep(10)
    else:
      for pp in jsonProducts:

            embed = Embed(
                title="{}{}".format(baseUrl,pp['url']), description="some items may have changed")
           
            Hook(
                hook_url=webhook,
                username="CHANGE.ME.discord.bot.user.name",
                content="id:{}, handle:{}, tags:{}".format(str(pp['id']), pp['handle'], str(pp['tags'])),
                embeds=[embed]).execute()
            time.sleep(10)
   
    time.sleep(120)

Thank's you very much,I will try all this, and improve if necessary. I'll send you an mp if I need it!
 
Back
Top