/* Copying and distribution of this file, with or without modification,
 * are permitted in any medium without royalty provided the copyright
 * notice and this notice are preserved.  This file is offered as-is,
 * without any warranty.
 */

import amalthea.net;
import amalthea.encoding;

import std.encoding;
import std.stdio;


void main() {
    string utfText;

    string linkToKOI8R = "https://www.opennet.ru/opennews/art.shtml?num=52171";
    auto koi8rSample = cast(KOI8RString) amalthea.net.get(linkToKOI8R);
    writeln("KOI8R-encoded page was received.");
    amalthea.encoding.transcode(koi8rSample, utfText);
    File("koi8r_to_utf8.html", "w").write(utfText);
    writeln("koi8r_to_utf8.html was written.");
    writeln("-");

    // alternative method to get encoded html page
    UniString page = amalthea.net.getText(linkToKOI8R);
    writeln(page.getEncodingName());  // koi8-r
    // we can get the same string in new encoding
    auto win1251Page = page.toEncodedString!Windows1251String;
    std.encoding.transcode(win1251Page, utfText);
    File("win1251_to_utf8.html", "w").write(utfText);
    writeln("win1251_to_utf8.html was written.");
}
