#Microadventure by bike in Brandenburg, Germany

Posted on 30. Juni 2016 Comments

Brandenburg is German state in the east at the Polish border. Just as it’s neigbouring state Mecklenburg-Vorpommern, it doesn’t have a lot of inhabitants compared to the size. But a lot of lakes and one bis plus: in about the middle lays Berlin and their border is easily reachable by the public transportation („S-Bahn“) of Berlin. So 3 weeks ago on a Friday after work I packed my panniers, booked a Flixbus ticket for me and my bike and drove to Berlin to visit my friend Martin. We then started (reasonably) early Saturday morning in Oranienburg, north or Berlin, and drove until the early evening: through forests and fields, on normal paved roads but also gravel. With lots of lakes  and nobody around we swam in every other lake at 30°C+ and sunshine. We then camped at a lake and ate our instant noodles. Next day we started with some nice pancakes and a bit later, a small lunch at a restaurant and made it just in time to a café, before a thunderstorm broke loose. After that we sprinted 30km back to the train station. In total we drove about 170km. I took the bus back to Hamburg that very night and went to work on Monday again. Awesome weekend, we will definitely do this again. Thanks for the idea, Alastair.

2016-06-05 10.06.52 2016-06-05 15.04.58 2016-06-05 09.40.46 2016-06-04 18.54.39 2016-06-04 18.54.30 NOVATEK CAMERA

brandenburg

MySQL Workbench Error: line contains NULL byte

Posted on 21. April 2016 Comments

When importing CSV (or other) files into the database, scripts (especially PHP or C-related languages), will stop if there is – for whatever reason – a NULL byte in your file because it signals end of file/string, see Null bytes related issues.

So when importing a file like that with MySQL Workbench you will get this error:

line contains NULL byte

You can solve this by using the commandline tool tr (from coreutils):

tr < file-with-nulls -d '\000' > file-without-nulls

To check if there are any null bytes in your file, use the python IDE and type in:

open('filename.ext').read().index('\0')

Thanks to Pointy from Stackoverflow

ThinkPad Compact Bluetooth Keyboard with TrackPoint Einfügen beim Scrollen Fix Ubuntu 14.04

Posted on 16. Dezember 2015 Comments

Die ThinkPad Bluetooth Tastatur von Lenovo verursacht unter Ubuntu 14.04 einen Fehler, so dass beim Scrolling mit TrackPoint und mittlerer Maustaste gleichzeitig die Einfügen Operation ausgelöst wird. Ich will aber auf keinen Fall weder auf die Paste Funktion der mittleren Maustaste noch auf das Scrolling verzichte und habe lange gesucht bis ich die folgende Lösung gefunden habe: tp-compact-keyboard-backport.

  1. git clone https://github.com/mithro/tp-compact-keyboard-backport.git
  2. make
  3. make install
  4. hid-lenovo.ko in /lib/modules/3.19-*version*-generic/kernel/drivers/hid/ austauschen.
  5. (leider muss man das auch nach jedem Kernel Update machen, da sonst die mittlere Taste gar nicht mehr funktioniert)
  6. Neustart

Außerdem war noch tp-compact-keyboard hilfreich (daher habe ich auch das -backport repository). Ggf. ist dafür ein Kernel Update nötig bzw. einfacher als ein Kernel-Patch zu installieren.

Simple wget crawler for list of files

Posted on 6. August 2015 Comments

This script could be helpful to download a set of files from a webserver, that you don’t have (S)FTP access to. The input file consists of a list of filenames, one name each line.


#!/bin/bash
file=$1
WEBSERVER="http://webserver.tld/folder/"
while IFS= read -r line; do
FULLURL="$WEBSERVER$line"
wget -nc -R --spider $FULLURL
done < "$file"

At first, the first command line argument is saved into the variable file. Then the Webserver address is saved to the WEBSERVER variable. IFS stands for Internal Field Separator. It’s used to read line by line through the file in the while loop that ends in the last line. Inside of the loop, the read line is concatenated with the webserver address into FULLURL. Then, wget is used with the parameters -nc for checking if the file is not already present in the current folder, -R for downloading and –spider for checking the existence on the webserver.

You can find the script on GitHub.

Collect currency exchange rates in a MySQL database with PHP and fixer.io API

Posted on 4. August 2015 Comments

If you work in a company that buys and sells goods in many different currencies, it might be a good idea, to use the latest exchange rates. Also, it might be useful, to store old exchange rates to clarify/verify old business decisions. If once a day is enough for you, fixer.io offers a free simple Rest API. A lot of the code at my work is written in PHP but I usually use the request library in JavaScript and Python, so I’m using it in this example too. A common PHP solution would be guzzle. But first, get composer (the PHP counterpart to npm or pip):

$ curl -sS https://getcomposer.org/installer | php

$ php composer.phar require rmccue/requests

The mysql_ commands are deprecated (and removed in PHP 7), use mysqli or PDO. Also you should use some sort of framework for the database access, like medoo or a proper ORM. This is just proof of concept.


$base = 'EUR';
$request = Requests::get('https://api.fixer.io/latest?base=' . $base, array('Accept' => 'application/json'));
if ($request->status_code == 200) {
$response = json_decode($request->body);
$GBP = $response->rates->GBP;
$CAD = $response->rates->CAD;
$USD = $response->rates->USD;
$NOK = $response->rates->NOK;
$CNY = $response->rates->CNY;
$rBase = mysql_real_escape_string($response->base);
$date = mysql_real_escape_string($response->date);
$currencies = mysql_real_escape_string("1.0, $USD, $GBP, $NOK, $CNY, $CAD");
$qry = "INSERT INTO `exchange_rates_fixerio`(date, base, eur, usd, gbp, nok, cny, cad) VALUES ('$date', '$rBase', $currencies);";
$insert = mysql_query($qry, $mysqlConnection) or print mysql_error();
}

I assume the database connection is defined earlier, there’s lot’s of documentation for that. Because we are from Europe, I chose Euro (EUR) as the base currency. Apart from the get() method, you need nothing else, to send a request. If the request returns an OK(200), the response is read and saved into different variables, e.g. for British Pounds, US Dollar, Canadian Dollar, Chinese Renminbi and Norwegian Krone. Just to make sure we have the right base, it’s also parsed. From there it’s only a simple INSERT INTO (as said before, use a framework for that)

The table could look like this:

CREATE TABLE `echange_rates_fixerio` (
`date` date NOT NULL,
`base` varchar(3) NOT NULL,
`eur` double NOT NULL,
`usd` double NOT NULL,
`gbp` double NOT NULL,
`nok` double NOT NULL,
`cny` double NOT NULL,
`cad` double NOT NULL
)

 

You can also find this code on GitHub.

Using nanoc for podcast feeds

Posted on 23. Juli 2015 Comments

Nanoc is a static site generator much like jekyll or octopress, but with a more minimalistic approach. These generators are not necessarily the most suitable choice for a podcast website, but it’s possible and you might save up on webspace and traffic when you use GitHub or Neocities.

Creating the podcast feed is basically like writing a normal Atom feed for the blog, since podcast feeds ARE indeed feeds with an enclosure tag in which the URL to the audio or video file is placed. This guide does not include the itunes tags. I might add it one day. Follow the instructions and use the documentation for the Helper Blogging. Tag your podcast episodes as kind:article.

The media files are placed in content/mp3 and content/opus, which is where the links in the feeds will point to later.

I invented the fields mp3 and opus, since these are the file formats I want to use. The values are the filenames. The header of a new episode/post would look like

---
title: 001 - Podcast Episode Title
created_at: 2015-03-14 09:00:00 +0000
kind: article
tags: [podcast,topic]
mp3: 001-podcast-episode-title.mp3
opus: 001-podcast-episode-title.opus
---

This has to be filled manually everytime, so make sure you have the exact filename, as some podcast clients won’t allow correction of the URL.

The next step is to write different feeds for the formats. For that, I’m using the new field format, which will be interpreted by the Helper class later on. For example, I called my normal feed blogfeed and the podcast feeds mp3feed and opusfeed. Create the file blogfeed.erb in the content folder and fill it with the following:

<%= atom_feed :title => 'repats podcast blog', :author_name => 'repat',
:author_uri => 'http://repat.de', :limit => 10, :format => 'blog' %>

The mp3feed.erb and opusfeed.erb are filled accordingly:

<%= atom_feed :title => 'repats podcast mp3', :author_name => 'repat',
:author_uri => 'http://repat.de', :limit => 10, :format => 'mp3' %>

<%= atom_feed :title => 'repats podcast opus', :author_name => 'repat',
:author_uri => 'http://repat.de', :limit => 10, :format => 'opus' %>

The next step is to use the Blogging locally in your nanoc installation. To do that you need to copy it from the gems folder into your lib folder. For me, that was

$ cp /var/lib/gems/1.9.1/gems/nanoc-3.7.5/lib/nanoc/helpers/blogging.rb lib/

It should be included like this in the lib/default.rb

include Nanoc3::Helpers::Blogging

Add the following attribute to the AtomFeedBuilder class

attr_accessor :format

If you don’t trust yourself to always remember the files you might want to  add this exception to the validate_feed_item function

if format.nil?
raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: no format(mp3,opus,blog) in params, item or site config')
end

After the # Add link comment is a good place to insert the  enclosure tag. File.size() will only work if the files are there and the exact same name. This code could probably be written a bit more safely, but I’m not a ruby developer and since I will have an mp3 file and and opus file in every post it’s not a problem this way.

# Add podcast enclosure
if format == 'mp3'
xml.link(href:"http://yourpodcast.com/mp3/" + a[:mp3],length:File.size("content/mp3/" + a[:mp3]), type:"audio/mpeg", rel:"enclosure")
elsif format == 'opus'
xml.link(href:"http://yourpodcast.com/opus/" + a[:opus],length:File.size("content/opus/" + a[:opus]), type:"audio/mpeg", rel:"enclosure")
end

To interpret the mp3 and opus attribute from earlier in the actual post, the last step is to add this line to the atom_feed function:

      builder.format            = params[:format]

You might need to install builder to let this run

$ sudo gem install builder

The only thing left to do is to edit the Rules file:

compile '/blogfeed' do
filter :erb
end
compile '/mp3feed' do
filter :erb
end
compile '/opusfeed' do
filter :erb
end
[...]
route '/blogfeed' do
'/blogfeed.xml'
end
route '/mp3feed' do
'/mp3feed.xml'
end
route '/opusfeed' do
'/opusfeed.xml'
end

 

You can find the blogging.rb and the Rules file on GitHub.

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":".-"}