Entries posted by repat

Auftragsstatus bei Rakuten

Posted on 22. Juli 2015 Comments

Rakuten.de hat eine REST API, über die z.B. Aufträge abgefragt werden können. Die Doku gibt dazu folgende Auskunft:

pending = Bestellung ist neu eingegangen
editable = Bestellung ist zur Bearbeitung freigegeben
shipped = Bestellung ist versendet
payout = Bestellung ist ausbezahlt
cancelled = Bestellung ist storniert

Eine ausgiebiere Erklärung könnt so aussehen:

pending = Bestellung ist neu eingegangen. Wenn der Kunde über PayPal oder Kreditkarte bezahlt hat, wird dieser Zustand eigentlich sofort wieder verlassen. Eigentlich braucht man sich Aufträge mit diesem Status nicht angucken, weil im Normalfall Ware erst versandt wird, wenn sie auch bezahlt ist. Vorkasse Aufträge können z.B. länger in diesem Status verbleiben, eben so lange bis das Geld bei Rakuten eingegangen ist.
editable = Bestellung ist zur Bearbeitung freigegeben. Das heißt im Normalfall, dass der Kunde die Ware auch bezahlt hat. Diese Aufträge sollte man sich ins System holen, sie intern bearbeiten und kann dann in den nächsten Status springen
shipped = Bestellung ist versendet. Dies wird z.B. erreicht, indem eine Trackingnummer für das Paket eingegeben wird. Dies muss durch den Händler passieren, Rakuten wird diesen Status nie von alleine setzen.
payout = Bestellung ist ausbezahlt. Rakuten hat nach einer bestimmten Frist das aufsummierte Geld der Bestellungen (in der die aktuelle Bestellung enthalten ist) an den Händler überwiesen.
cancelled = Bestellung ist storniert. Der Kunde hat sich umentschieden oder der Händler hat die Bestellung storniert (etwa durch Fehlbestand)

 

upload mit phpsec library und php

Posted on 22. Juli 2015 Comments

In der Doku, die gerade nicht verfügbar ist, da sie auf sourceforge liegt, ist ein Fehler. Der Upload via SFTP mit der phpseclib funktioniert auf 2 verschiedene Weisen folgendermaßen:

$sftp->put($destinationFilename, file_get_contents($sourceFullFilePath));

oder


$sftp->put($destinationFilename, $sourceFullFilePath, NET_SFTP_LOCAL_FILE);

 

Domain von Domain Offensive bei Heroku nutzen

Posted on 24. Januar 2015 Comments

Ich habe neulich die Domain morsecode-api.de bei Domain Offensive erworben und wollte nun diese Domain mit meiner Heroku App für Morsecode As A Service nutzen. Allerdings bietet Heroku nicht die Möglichkeit an, A Resource Records oder AAAA Resource Records (für IPv6) zu benutzen. Stattdessen empfehlen sie einen CNAME Record. Nun ist es von der IETF aber nicht empfohlen bzw. gegen den Standard CNAME Records für root Domain Namen zu verwenden und viele Hoster unterstützen dies auch nicht. Wie der Name schon andeutet sind diese Einträge für weitere anerkannte Namen der selben Domain gedacht (z.B. .net und .com für denselben Namen).

Der Workaround funktioniert bei do.de also folgendermaßen:

  1. In den do.de Domaineinstellungen wird eine Weiterleitung eingerichtet auf www.example.com/
  2. Nach einer Weile ist es möglich unter DNS (Zonen Details), die A bzw. AAAA Resource Records für *.example.com zu entfernen
  3. Außerdem muss man einen CNAME Eintrag für www.example.com anlegen, der auf example.herokuapp.com zeigt.

Der Ablauf ist dann folgender:

  1. Eine DNS Anfrage auf example.com wird gestellt
  2. Der DNS Server liefert einen A bzw. AAAA Resource Record für einen lighttpd Server von Domain Offensive zurück
  3. Dieser liefert dem Client eine HTTP Redirect 301 Message auf www.example.com/ zurück (s. 1. weiter oben)
  4. Eine DNS Anfrage auf www.example.com gestellt
  5. Auf diese antwortet ebenfalls do.de mit einem CNAME Eintrag auf example.herokuapp.com
  6. Die HTTP Anfrage wird an example.herokuapp.com gestellt.

Löscht man auch die A bzw. AAAA Resource Records, funktioniert die Domain nur über die Subdomain www.example.com.

Dies funktioniert nur für Webservices unter Port 80 via HTTP. Für andere Bereiche bleibt wohl nur übrig, auch die A bzw. AAAA Records auf example.com zu löschen und in den Anfragen nur die Subdomain www.example.com zu benutzen.

Ausprobieren kann man das nach ein bisschen Wartezeit mit z.B. cURL:

$ curl -vL example.com
$ curl -vL www.example.com

Bei meinem Beispiel sieht das wie folgt aus:

 

$ curl -vL morsecode-api.de/encode/A
* Hostname was NOT found in DNS cache
*   Trying 78.47.59.169...
* Connected to morsecode-api.de (78.47.59.169) port 80 (#0)
> GET /encode/A HTTP/1.1
> User-Agent: curl/7.35.0
> Host: morsecode-api.de
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Location: http://www.morsecode-api.de/encode/A
< Content-Length: 0
< Date: Sat, 24 Jan 2015 20:09:09 GMT
* Server lighttpd/1.4.28 is not blacklisted
< Server: lighttpd/1.4.28
< 
* Connection #0 to host morsecode-api.de left intact
* Issue another request to this URL: 'http://www.morsecode-api.de/encode/A'
* Hostname was NOT found in DNS cache
*   Trying 23.21.123.184...
* Connected to www.morsecode-api.de (23.21.123.184) port 80 (#1)
> GET /encode/A HTTP/1.1
> User-Agent: curl/7.35.0
> Host: www.morsecode-api.de
> Accept: */*
> 
< HTTP/1.1 200 OK
* Server Cowboy is not blacklisted
< Server: Cowboy
< Connection: keep-alive
< Content-Type: application/json
< Content-Length: 34
< Date: Sat, 24 Jan 2015 20:09:09 GMT
< Via: 1.1 vegur
< 
* Connection #1 to host www.morsecode-api.de left intact
{"plaintext":"A","morsecode":".-"}

Morsecode As A Service: node.js app with restify and Heroku

Posted on 22. Januar 2015 Comments

I wanted to play around with node.js and REST APIs. Heroku is widely used for deploying node.js app, last but not least because they give you one free instance to test your code and the possibility to use your own domain name (via CNAME).

Until now the code is rather trivial. This for example is the encode function (plaintext->morsecode). It uses the npm module morse to encode the given string in the request parameters and returns it with the plaintext in an array. The requests are handled by restify.
function encode(req, res, next) {
var answer = {}
answer.plaintext = req.params.string.toUpperString();
answer.morsecode = morse.encode(req.params.string);
res.send(answer);
next();
}

This function gets called later here.
server.get('/encode/:string', app.encode);

This starts the server on the port from the Heroku instance.
var port = process.env.PORT || 8080;
server.listen(port, function() {
console.log('%s listening at %s', server.name, server.url);
});

The decode function is equivalent, except that I test if there are only „.“, „-“ and white spaces in the request.

I wrote the documentation with the automatic page generator from GitHub Pages in the repository and so with the following code the user is redirected there when entering the root „/“.

 

function redirectToDocumentation(req,res,next) {
res.send(302, null, {
Location: API_DOKU
});
next();
}

It’s reachable under morsecode-api.de or morsecodeapi.herokuapp.com.

Skript mit cat, unzip, grep und awk für eBays XML Responses

Posted on 7. Januar 2015 Comments

Wenn man bei eBay per API z.B. ein FixedPriceItem hinzufügt bekommt man die folgende Antwort nachdem der Prozess erst Scheduled und dann InProcess ist:

Status is Completed
Downloading fixed price item responses...Done
File downloaded to /tmp/add-fixed-price-item-responses-ABC123.zip
Unzip this file to obtain the fixed price item responses.

Den folgenden Code eine Datei schrieben, dann in /usr/local/bin verschieben und mit chmod +x Ausführrechte geben.

cat `unzip -o $1 | grep inflating |awk '{print $2}'`

Mit diesem Skript bekommt man zum debuggen eine schnelle Ausgabe der XML aus der Konsole bewirken:

debugebayxml /tmp/add-fixed-price-item-responses-ABC123.zip

Using a Perl script because refactoring of a project with Android tools didn’t work

Posted on 20. Dezember 2014 Comments

I wanted to rename my project but I guess since it has a lot of dependencies that caused an error somewhere and I got the error message:

Refactoring
A fatal error occurred while performing the refactoring.
An unexpected exception occurred while creating a change object. See the error log for more details.

So I just the normal refactoring feature of Eclipse which not surprisingly also caused an error. After editing the AndroidManifest package entry, the import of the resources in the sources files didn’t work. It still said

import com.example.oldpackage.R

Only a couple of resource files needed manual editing but the Java files were a problem. What did the trick for me was this one-liner

perl -pi -w -e 's/import com.example.oldpackage.R/import com.example.newpackage.R/g;' `grep -r -l "import com.example.oldpackage.R"`

I realise this is rather quick&dirty (do a backup 😉 ) but it did work for this project. A short explanation:

perl

  • -pi puts the code in a loop (like -n, but sed-style)
  • -w gives you warnings
  • -e is one line of programm and since -pi
  • /g global, for all lines in the file

grep

  • -r recursive
  • -l give out files that matches the following search string

How to change nano syntax highlighting for arbitrary filename extensions

Posted on 17. Dezember 2014 Comments

The text editor nano manages syntax highlighting through .nanorc files. Say, we have files with the ending .phpx, but would like PHP syntax highlighting.

$ sudo nano /usr/share/nano/php.nanorc

Change this line

syntax "php" "\.php[2345s~]?$"

to this regex:

syntax "php" "\.php[x2345s~]?$".

Alternativly, e.g. if you don’t have sudo rights for /usr/share/nano/php.nanorc, you can do the following

$cp /usr/share/nano/php.nanorc ~/.phpx.nanorc

Then enter the following line in ~/.nanorc:

include ~/.phpx.nanorc

Then, change the line as mentioned above and syntax „php“ into syntax „phpx“

Automate selling at LaRedoute #2: Parse order files

Posted on 16. Dezember 2014 Comments

This blog post is part of the series Automize selling at LaRedoute.

  • Part 1: Get new orders
  • Part 2: Parse order files
  • Part 3: Upload response files
  • Part 4: update quantity and price feed

Update 2016: La Redoute is going to stop using CSV and moves everything to SOAP Webservices. Tutorials will follow


In part 1 the files containing orders are downloaded into a folder called OrdersFromLaRedoute. The next script is going to go through that folder, parse the file and insert it into a table.

These are the values the table must have because we’re just going to insert everything that’s in the CSV files.


$dbValues = ['MarketplaceID', 'OrderID', 'StorefrontOrderID', 'OrderDate', 'BuyerEmailAddress', 'BuyerName', 'BuyerPhoneNumber', 'OrderItemCode', 'ItemStatus', 'SKU', 'Title', 'Quantity', 'ItemPrice', 'ItemTax', 'ShippingCharge', 'ShippingTax','ItemFee', 'Currency', 'ShippingOption', 'PaymentInfo', 'ShippingAddressName', 'ShippingAddressFieldOne', 'ShippingAddressFieldTwo', 'ShippingAddressFieldThree', 'ShippingCity', 'ShippingStateOrRegion', 'ShippingPostalCode', 'ShippingCountryCode', 'ShippingPhoneNumber', 'BillingAddressName', 'BillingAddressFieldOne', 'BillingAddressFieldTwo', 'BillingAddressFieldThree', 'BillingCity', 'BillingStateOrRegion', 'BillingPostalCode', 'BillingCountryCode', 'BillingPhoneNumber'];

 

Therefore I created the table more or less like this:

CREATE TABLE `ORDERS-HISTORY` (
`MarketplaceID` int(11) DEFAULT NULL,
`OrderID` varchar(50) DEFAULT NULL,
`StorefrontOrderID` varchar(50) DEFAULT NULL,
`OrderDate` varchar(50) DEFAULT NULL,
`BuyerEmailAddress` varchar(50) DEFAULT NULL,
`BuyerName` varchar(50) DEFAULT NULL,
`BuyerPhoneNumber` varchar(50) DEFAULT NULL,
`OrderItemCode` varchar(50) NOT NULL DEFAULT '',
`ItemStatus` varchar(10) NOT NULL DEFAULT '',
`SKU` int(11) DEFAULT NULL,
`Title` varchar(50) DEFAULT NULL,
`Quantity` int(11) DEFAULT NULL,
`ItemPrice` decimal(8,2) DEFAULT NULL,
`ItemTax` decimal(8,2) DEFAULT NULL,
`ShippingCharge` decimal(8,2) DEFAULT NULL,
`ShippingTax` decimal(8,2) DEFAULT NULL,
`ItemFee` decimal(8,2) DEFAULT NULL,
`Currency` varchar(3) DEFAULT NULL,
`ShippingOption` varchar(10) DEFAULT NULL,
`PaymentInfo` varchar(50) DEFAULT NULL,
`ShippingAddressName` varchar(70) DEFAULT NULL,
`ShippingAddressFieldOne` varchar(70) DEFAULT NULL,
`ShippingAddressFieldTwo` varchar(70) DEFAULT NULL,
`ShippingAddressFieldThree` varchar(70) DEFAULT NULL,
`ShippingCity` varchar(70) DEFAULT NULL,
`ShippingStateOrRegion` varchar(70) DEFAULT NULL,
`ShippingPostalCode` int(11) DEFAULT NULL,
`ShippingCountryCode` varchar(3) DEFAULT NULL,
`ShippingPhoneNumber` varchar(50) DEFAULT NULL,
`BillingAddressName` varchar(70) DEFAULT NULL,
`BillingAddressFieldOne` varchar(70) DEFAULT NULL,
`BillingAddressFieldTwo` varchar(70) DEFAULT NULL,
`BillingAddressFieldThree` varchar(70) DEFAULT NULL,
`BillingCity` varchar(70) DEFAULT NULL,
`BillingStateOrRegion` varchar(70) DEFAULT NULL,
`BillingPostalCode` int(11) DEFAULT NULL,
`BillingCountryCode` varchar(3) DEFAULT NULL,
`BillingPhoneNumber` varchar(50) DEFAULT NULL,
PRIMARY KEY (`OrderID`,`OrderItemCode`,`ItemStatus`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

As you can see, the primary key consists of OrderID, OrderItemCode and ItemStatus. An OrderID is the unique identifier of one order, consisting of possibly many but at least one OrderItemCode. An OrderItemCode is representing an SKU + Quantity. For newly created orders, the ItemStatus will appear as „Created“. Once an item is accepted, LaRedoute will put a file into the ToSupplier folder with exactly the same OrderID, OrderItemCode but the ItemStatus „ToShip“. This will be important in Step 3.

Then, use a CSV library like league/csv and insert it with e.g. medoo.


$dirAsArray = scandir("OrdersFromLaRedoute");

foreach($dirAsArray as $file) {
// parse and INSERT
}

Automate selling at LaRedoute #1: Get new orders

Posted on 16. Dezember 2014 Comments

This blog post is part of the series Automize selling at LaRedoute.


The french marketplace LaRedoute unfortunately doesn’t have a real API, but they do have ways to automize some processes. A lot of smaller marketplaces have this concept as well. You will get credentials for an SFTP server. On this server you will find the folders ToSupplier and FromSupplier, where the „supplier“ (aka you) can up- and download a range files documented by Merchantry in their blog. The processing of the uploaded files can take up to 6 hours, but is sometimes done in only a couple of minutes, so I’m going to assume the worst case of 6 hours in this post.

While programming a couple of scripts I found the following problems:

  • the server is incredibly slow sometimes (better at nights), so sometimes the connections just time out
  • sometimes the listing for the ToSupplier folder times out because there are too many files (according to support…huh?), so they have to be deleted regularly
  • not only the connection to LaRedoute but also the connection to my local MySQL server times out
  • I have to reserve a purchased item once I accepted it on LaRedoute immediately, because it could be sold elsewhere in the 6 hours LaRedoute might take to give me the shipping address

New orders can be found in the ToSupplier folder in tab seperated CSV files (but .txt ending) with the format OrdersYYYY-MM-DD-hh-mm-ss.txt.

Since PHP is the companies main language I will show a couple of scripts which automize downloading and processing those files. The code is of course simplified for better understanding. We’re using SFTP instead of FTP and I found using the phpseclib to be the most usable library.

I will propose the use of 2 Tables in the MySQL database: TEMP-FILENAMES and FILENAMES-HISTORY. Both have a the unique column filename. FILENAMES-HISTORY will contain the name of every file ever processed by the following script, TEMP-FILENAMES is a helper table that will be truncated after every run.

First, we need to establish a connection


$sftp = new Net_SFTP(SFTP_LAREDOUTE_HOST);
if (!$sftp-&gt;login(SFTP_LAREDOUTE_USER, SFTP_LAREDOUTE_PASS)) {
exit('Login Failed');
}

Then we change directory. This is a command that usually involves listing the directory changed into, but since this is not a graphical client, the real timeout might come on line below. $nlist will just be null if the listing fails, and I will assume it didn’t work if it takes more than 30 seconds.

$sftp->chdir('/ToSupplier');

$beforetime = time();
$nlist = $sftp->nlist();
$aftertime = time();
if(($aftertime-$beforetime) > 30 ) {
exit('Timeout while Listing directory');
}

The next piece of code is only executed if the listing worked. Every filename that includes the word „Order“ is now inserted into the temporary table:

foreach($nlist as $filename) {
if (strpos($filename, 'Order') !== false) {
$qry = "INSERT INTO `TEMP-FILENAMES`(`filename`) VALUES ('". $filename . "')";
$insert = mysql_query($qry,MYSQLCONNECTION) or print mysql_error();
}
}

You can look at the difference between the filenames in your HISTORY table and the possibly new ones in the temporary table.

$tmpCmpFilenames = array();
$qry = "SELECT `filename` FROM `TEMP-FILENAMES` WHERE `filename` NOT IN (SELECT `filename` FROM `FILENAMES-HISTORY`)";
$select = mysql_query($qry, MYSQLCONNECTION) or print mysql_error();
while ($row = mysql_fetch_assoc($select)) {
$tmpCmpFilenames[] = $row['filename-id'];
}

Now we have all the new files in the array $tmpCmpFilenames. The correct way would be make sure the downloaded files are correct with hashes. Instead we decided to misuse the filesize, since it’s a good indicator something didn’t work properly;) The files not downloaded correctly are deleted from the array. They will appear next time the script is run.

foreach($tmpCmpFilenames as $filename) {
$remotefilesize = $sftp->size($filename);
$sftp->get($filename, 'OrdersFromLaRedoute/' . $filename);
$localfilesize = filesize('OrdersFromLaRedoute/' . $filename);
if ($remotefilesize != $localfilesize) {
unset($tmpCmpFilenames[$filename]);
}
}

We can now insert the filenames into the HISTORY table.

foreach($tmpCmpFilenames as $filename) {
$qry = "INSERT INTO `FILENAMES-HISTORY`(`filename`) VALUES ('". $filename . "')";
$insert = mysql_query($qry, MYSQLCONNECTION) or print mysql_error();
}

Last but not least, the temporary table needs to be truncated for the next run.

$truncate=mysql_query("TRUNCATE TABLE `TEMP-FILENAMES`",MYSQLCONNECTION) or print mysql_error();

The next step is described in part 2 of this series.

Minimal Mensa Plan

Posted on 9. Dezember 2014 Comments

We’ve been doing a lot of website scraping for a university project latel so I decided a little app to scrape the universitys‘ cantine website for the lunch menu.

minimalmensaplan-screenshot

A network connection in the main Activity is not allowed so I’m using a private class for that. Once the doInBackground method is over onPostExecute is automatically called.

Luckily the jSoup library which is used for parsing also brings a way download a website for parsing.

Document doc = Jsoup.connect("http://speiseplan.studierendenwerk-hamburg.de/de/520/2014/0/").get();

There is only one category class and it contains the date. The dish-description class contains what you will later see in the app. The size is needed for a for-loop later on.

date = doc.select(".category").text();
int maxDishes = doc.getElementsByClass("dish-description").size();

With the next piece of code all the dish descriptions are extracted. The original website contains details about the food (made with alcohol, pork or if it’s vegeterian etc). For blending this out I use a regular expression which filters out:

1 non-word character(\W = an opening bracket), then possibly multiple digits (\d) and non-word characters (\W = commas) and then another non-word character (\W = a closing bracket)

String dish = doc.getElementsByClass("dish-description").get(i).text().replaceAll("(\\W[\\d\\W]*\\W)", " ");

These dishes are then saved together with every second price (the one for students, the other one’s for employees) in a HashMap, which is then added to a list. For recognizing later, the dishes get the key „dish“ and the prices the key „price“.

Once the data extraction is done, onPostExecute is automatically called. The date is set to a TextView above the ListView and a SimpleAdapter is populating the list of HashMaps into the layout

dateTextView.setText(date);
simpleAdapter = new SimpleAdapter(MainActivity.this,
dishList, R.layout.list, new String[] { "dish",
"price" }, new int[] { R.id.text1, R.id.text2 });
setListAdapter(simpleAdapter);

Since i’s called Minimal Mensa Plan, no other features (such as caching or selecting a cantine) are available. The app in the Play Store is used for scraping the cantine at the campus Berliner Tor but it might as well be used for others, just by changing the URL. It’s released under the MIT License and available at GitHub.