Perl script monitors payments and feedback in eBay sales
Watchful Seller
We show how to use a screen-scraper and an application for the official eBay API to trigger an alarm on incoming eBay customer feedback and detect errors in the monthly billing statement.
If I ever get around to writing my memoirs, I am going to include a lengthy chapter about my life's motto, which is: "Anything you don't constantly monitor is guaranteed to go belly up when you least expect it." True to this motto, I wrote a script this month to receive an email immediately when one of my eBay customers has left feedback about a transaction.
Simply Scraping
As a quick and dirty solution, and to first avoid having to register as a developer with eBay, I wrote a screen-scraper that extracts the current feedback score from the slew of HTML on the feedback page, before saving the counter and raising the alarm during subsequent runs if the value has increased.
When I click on my username on eBay.com, the browser lands on the feedback page in Figure 1. Selecting view-source
in the browser shows the HTML code from Figure 2, and a text search for the string 362
(the current feedback score) shows that this number is found in a markup tag of the mbg-l
class:
( 362<img src="...
An XPath processor such as HTML::TreeBuilder::XPath can easily retrieve the content of this tag. The query
/html/body//span[@class="mbg-l"]
locates all span
elements in the HTML body that have a class
attribute with a value of mbg-l
. The double slash in the expression indicates that the requested elements can exist at an arbitrary nesting depth beneath the HTML body tag.
The XPath query then spits out a string such as ( 362) (...
. From there, with a regular expression in Perl, extracting the score is child's play. Listing 1 does exactly this in the feedback_fetch
function in line 70, and then saves the score that was found in a cache file. Then, on the next run, it compares the value with the one obtained at that point, and fires off an email if the number has increased [1].
Listing 1
ebay-feedback
Email at Your Command
Writing and reading the cache data, which saves the feedback score confirmed during the last run as its only value, is handled by the slurp
and blurt
functions. These are from the CPAN Sysadm::Install module, which exports them in line 3. To retrieve the eBay page, the script uses the slimmed-down LWP::Simple module, whose get
method simply runs an HTTP request for the specified URL and, if successful, returns the content of the page that was found.
On Linux, it is often very easy to send mail with the /usr/bin/mail
utility; the CPAN Mail::DWIM module is recommended for those wanting more flexibility. Because I already replaced /usr/bin/mail
on my Linux box at home with a Perl script that can cope with my ISP's requirements, I left things as is. Figure 3 shows the email that landed in my Gmail inbox.
Before firing it up, the parameters in the script header still need to be tuned to match the local conditions. In line 8, the $nick
variable receives the name assigned to the eBay user, whose feedback counter will be monitored by ebay-feedback
. The URL is valid for US eBay accounts, whereas UK accounts run on ebay.co.uk instead. Finally, in the $mail_to
variable, line 23 requires the email address of the user to whom the alerts should be sent.
Monitoring the Monitor
How can you monitor whether the script is still functioning, or whether eBay has changed its page layout? Listing 1 records all the operations for that purpose via Log::Log4perl in the log file ~/logs/ebay-feedback.log
. Additionally, for instance, a daily Nagios script could trawl through the data that show up there based on a pattern such as "OK" under the current date, and sound the alarm if it is not found."
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.