Showing posts with label api. Show all posts
Showing posts with label api. Show all posts

WolframAlpha CLI interface

I've reworked one of my old shell scripts: the WolframAlpha commandline interface. This client uses the free WolframAlpha API. You can get your own API key at https://developer.wolframalpha.com/portal/apisignup.html (you need it in order to use this script as I'm not giving away my own key). After you've registered your key, put it into script's config section. Here goes a little screenshot to show you the looks of this script interface:
Hope it will be of some use to somebody ;). As always, the sources are in my Github repo.

ThePirateBay.se search script

This ThePirateBay.se torrent search script is based on the quite modular Torrentz.eu one I've made a while ago. You can find it on my GitHub as will be usual for all of my future scripts and stuff, so don't forget to add me to your watch list :).

This deprecates the old TPB search script.

I'll greatly appreciate any feedback, so don't hesitate to leave a comment if you'd like to. Improvement ideas are also welcome ;).

You can also modify and reuse all my scripts listed in the repo if you feel like it as they're licensed under GPLv3.

Torrentz.eu search shell script

Torrentz.eu is a great meta-search engine collecting info from dozens of torrent hosting sites, such as ThePirateBay.se. You can read through the complete list of scraped sites right here (almost 7M of live torrents indexed as of now, ThePirateBay alone has "only" 3.1M).

I've written this /bin/sh compatible script mainly because my previous torrent search script for ThePirateBay stopped to function properly and I was just too lazy to rewrite the code to work with TPB's new way of offering torrent downloads -- via magnet links, which are great by the way ;).


Oh, and just in case you've missed it: you can download an offline copy of TPB's torrent database in format suitable for making and SQLite database and searching torrents completely anonymously. Hell, it's just 90 megs! However, pr0n is not included, so it's only 1,643,194 torrents :P.

OK, so let's go to the point. Here's a screenshot:

As you can see, it has a nice colorized output (um... well, it can be disabled with the -C cmdline switch) and shows first 15 results by default (a max. of 50 due to pagination, sometimes less due to DMCA takedown notices sent to torrentz.eu -- it's quite funny how they just let you know which torrents exactly are they watching :P).

And t3h s0urc3, ofc: see this pastebin, it's more readable and copypastable than listing it here. Be sure to edit the torrent download command at the beginning of the script if you're using client other than Transmission. You can also change the default number of displayed results and output colors if you'd like. Don't forget to leave a comment or drop some spare coin into my PayPal account if you like my works ;).

My shell scripts #5 - Google dictionary API (word definitions)

This two-file google dictionary API, which returns a full definition of a given word in all its forms (noun, adjective, verb, abbreviation, ...), along with a direct link to an mp3 file containing the word spoken by a google text-to-speech api (and a lot of other useful informations).
This script is divided into two separate scripts (shell and PHP) only because it is quite hard to parse JSON strings with line-oriented text processors (sed, grep, ...you get the idea). First, the shell script gets the actual data from google, does some filtering and substitution to make it easier for the following PHP script, which then iterates through the parsed JSON string and outputs the data you want (and also adds some colour).
Don't forget to change the names and paths accordingly to suit your environment and needs.

The shell script:
#!/bin/ash
lang="en"
if [ "$1" = "-" ]
then
  read word
else
  word="$*"
  word=`echo $word | sed 's/\ /\+/g'`
fi
if [ ! -n "$word" ]
then
  echo Specify a word to lookup in glossary.
  echo Usage:
  echo "  dict word"
  echo "use - (dash) if reading word from stdin"
  exit 1
else
  wget -qO - "http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q=$word&sl=$lang&tl=$lang&restrict=pr,de&client=te" \
  | sed 's/dict_api\.callbacks.id100.//' \
  | sed 's/\,\"webDefinitions.*//' \
  | sed 's/\\x3cem\\x3e//g' \
  | sed 's/\\x3c\/em\\x3e//g' \
  | sed "s/\\\x27/\'/g" \
  | php-cli /opt/scripts/gloss-json.php
fi
exit 0
And the PHP one:
    $fd = fopen("php://stdin", "r");
    $json = "";
    while (!feof($fd)) {
        $json .= fread($fd, 1024);
    }
    $json = $json.'}';
    //var_dump(json_decode($json));
    $json = json_decode($json);
    $word = $json->query;
    $primaries = count($json->primaries);
    if ($primaries == 0) echo "Sorry, no match.\n";
    for ($i = 0; $i < $primaries; $i++) {
        echo "\033[1;36m" , $json->primaries[$i]->terms[0]->text , " | ", $json->primaries[$i]->terms[0]->labels[0]->text ,
             "  ";
        if ($json->primaries[$i]->terms[1]->type == "phonetic") echo $json->primaries[$i]->terms[1]->text;
        echo "\033[0m\n";
        $cnt = 1;
        for ($j = 0; $j < count($json->primaries[$i]->entries); $j++) {
            if ($json->primaries[$i]->entries[$j]->type == "related") {
                for ($l = 0; $l < count($json->primaries[$i]->entries[$j]->terms); $l++) {
                    echo "\033[0;36m" , $json->primaries[$i]->entries[$j]->terms[$l]->text , "\033[0m ";
                    echo $json->primaries[$i]->entries[$j]->terms[$l]->labels[0]->text , "; ";
                }
                echo "\n";
            }
            if ($json->primaries[$i]->entries[$j]->type == "meaning") {
                echo "  " , $cnt , ") " , $json->primaries[$i]->entries[$j]->terms[0]->text , "\n";
                for ($k = 0; $k < count($json->primaries[$i]->entries[$j]->entries); $k++) {
                    echo "    * " , $json->primaries[$i]->entries[$j]->entries[$k]->terms[0]->text , "\n";
                }
                $cnt++;
            }
        }
    }
?>