Main Scripts
Dropshipping Script
14min
this script is used to automate the dropshipping process for dropshippers working with clients concept is to automate the process of dropshipping from linnworks account to the dropshipper the script will automatically search for orders on linnworks, export them to a dropbox folder, and then automatically send email of the orders to the dropshipper the dropshipper will then send the tracking number back to the script, which will then update the tracking number on linnworks all scripts for this are similar, for the sake of documentation we will use complete automotive dropship installation clone the repository and install the dependencies git clone https //github com/e mps co uk/completeautomotivedropship cd completeautomotivedropship python3 m venv venv source venv/bin/activate pip install r requirements txt create a new file called env and add the following variables clientid = # dropbox client id clientsecret = # dropbox client secret refreshtoken = # dropbox refresh token hostname = #host email = # client email address emailpassword = # client email password sending email = # dropshipper email address run the script python3 main py code structure script structure ├── readme md ├── app │ ├── init py │ ├── config py │ ├── db config py │ ├── downloadmail py │ └── sendmail py ├── config ini ├── main py ├── pyproject toml ├── requirements txt ├── tests │ ├── init py │ ├── test sendmail py │ └── test shipsmail py └── tox ini documentation configuration module this module contains the configuration variables and file paths used in the script for every droshipper, you must add new configuration variables dropbox configuration dropbox connect() this function is used to establish a connection with dropbox it returns a dropbox object that can be used for further operations def dropbox connect() > dropbox try endpoint = os environ\["endpoint"] query params = { "grant type" "refresh token", "client id" os getenv("client id"), "client secret" os getenv("client secret"), "refresh token" os getenv("refresh token") } response = requests post(endpoint, params=query params, timeout=60) json() dbx = dropbox dropbox(response\['access token']) except autherror as e print('error connecting to dropbox with access token ' + str(e)) return dbx send email module downloadfromdbx this function uses the dropbox object to download files from dropbox to a local path dbx = dropbox connect() dbx files download to file(local path, download path) send to db this function is used to save new oprders from a csv file to a sqlite database send email this function is used to send emails to the dropshipper if there are any new orders code snippet send from = username send to = recipient email msg = mimemultipart() msg\['from'] = send from msg\['to'] = send to msg\['date'] = formatdate(localtime=true) msg\['subject'] = 'open orders' with open(file path, 'rb') as fp part = mimebase('application', 'text/csv') part set payload(fp read()) encoders encode base64(part) part add header('content disposition', 'attachment', filename='orders(aap) csv') msg attach(part) with smtplib smtp ssl('send one com', 465) as smtp smtp login(username, password) smtp sendmail(username, send to, msg as string()) download mail module download from mail this function is used to download attachments from unread emails it fetches all unread messages and marks them as read mail = imbox(host, username=username, password=password) messages = mail messages(unread=true) for (uid, message) in messages mail mark seen(uid) for idx, attachment in enumerate(message attachments) try att fn = attachment get('filename') download path = f"{download folder}/{att fn}" with open(download path, "wb") as fp fp write(attachment get('content') read()) except exception as download error logging info('error downloading attachment %s', download error) mail logout() upload to dropbox this is uses the dropbox object to upload the downloaded files to a dropbox with open(file path, 'rb') as f dbx files upload(f read(), dest path, mode=dropbox files writemode overwrite, mute=true) this is a general overview of the dropshipping script with the information provided you will be able to run the script seamlessly