Get Geo Location

Here’s, let’s call it what it is, a hack to get geographical location (longitude & latitude).

Code:

#!/bin/env perl
use warnings;
use strict;
use LWP::Simple qw ($ua get);
$ua->timeout(15);

my $geo_url  = "https://geoiptool.com";
my $junk     = get($geo_url) || die "Problems connecting to URL!";
my $lon_line = 0;
my $lat_line = 0;
my $lon      = 0;
my $lat      = 0;
if ($junk) {
    my @lines = split( /\n/, $junk );
    my $i = 1;
    foreach my $line (@lines) {
        chomp($line);
        if ( $line =~ /Longitude/ ) {
            if ( $lon_line == 0 ) {
                $lon_line = ($i);
            }
        } elsif ( $line =~ /Latitude/ ) {
            if ( $lat_line == 0 ) {
                $lat_line = ($i);
            }
        }
        $i++;
    }
    $lon = $lines[$lon_line];
    $lon =~ s/\s+//;
    $lon =~ s/\<span\>//;
    $lon =~ s/\<\/span\>//;
    $lat = $lines[$lat_line];
    $lat =~ s/\s+//;
    $lat =~ s/\<span\>//;
    $lat =~ s/\<\/span\>//;

    if ( $lon && $lat ) {
        print "longitude: " . $lon . ", latitude: " . $lat . "\n";
    }
}

Output:

./geoloc.pl 
longitude: -80.135, latitude: 26.937

And that’s all I have to say about that.

Monitor Hot-plug Script

Background

I recently installed Pop!_OS from system76 (maker of my laptop) which happens to use a customized implementation of the Unity desktop. I’ve never been a fan of Ubuntu and certainly never cared for the Unity desktop. But I figured I would give it a try for a period of time before switching to something that is, in my opinion, more user-friendly and configurable (such as XFCE, Openbox, etc.). This isn’t about my opinion of Ubuntu, Unity or all efforts in Linux that go out of their way to mimic Microsoft Windows and Apple.

While it is a laptop that I use, I do, for the most part have an external monitor connected to it via an A/B switch for my work laptop and my personal one. One the functionalities lacking in Gnome3 (as well as other desktop environments) in general is the detection of when an external monitor is connected. Sure, you could use something like arandr the will generate a script for you and you can map it to a keyboard shortcut (binding), but that requires some manual effort that, frankly, shouldn’t be necessary. So, what I did was create a utility (chreli-monitor-hotplug) that simulates detecting when an external monitor is connected or disconnected, leveraging xrandr. The script is written in Perl and loops continuously (sleeping for 5 seconds) parsing the output of xrandr to see what’s connected, primary, etc. It’s not elegant, as I’m sure there are other like scripts or applications out there that can do the same thing better, but this gets the job done (for me any way).

Requirements

The following are required for chreli-monitor-hotplug:

  • x11-xserver-utils (needed for xrandr)
  • perl

Installation

To install, download chreli-monitor-hotplug-1.0.20171103.deb and execute the following command:

Disclaimer: This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

sudo /usr/bin/dpkg -i chreli-monitor-hotplug-1.0.20171103.deb

Usage

Using GNOME Tweaks, navigate to Startup Applications, click the + and in dialog, search for ChrEli.

Select ChrEli Monitor Hotplug and click the Add button.

Alternatively, you could just copy /usr/share/applications/chreli-monitor-hotplug.desktop to ~/.config/autostart.

The above will result in chreli-monitor-hotplug auto-starting on next restart (or re-login).

Configuration

Upon initial execution of chreli-monitor-hotplug, it will create and set defaults in ~/.config/chreli-monitor-hotplug/chreli-monitor-hotplug:

# chreli-monitor-hotplug 1.0.20171104
#
# configuration file for chreli-monitor-hotplug

# debug
# default: 0
chreli-monitor-hotplug.debug=0

# interval (in seconds) to check monitor changes
# default: 5
chreli-monitor-hotplug.interval=5

# dual
#  o flag to use both connected monitors
# default: 0
chreli-monitor-hotplug.usedual=0

# external.primary
#  o flag to make external primary monitor
# default: 1
chreli-monitor-hotplug.external.primary=1

Conclusion

That’s it. Nothing complex, but gets the job done and meets my needs (at least on my system). Given that every system and distro is different in how they behave, name devices, etc., chreli-monitor-hotplug may not work out-of-the-box on all system, but should for most.

And that’s all I have to say about that.

Yet Another Random Wallpaper Changer

Here’s yet another utility that, yep, you guessed it, is written in perl called chreli-randomwall. As the name implies, it’s random (keyword) wallpaper changer for desktop environments such as Openbox.

Requirements

The following are required for chreli-randomwall:

  • x11-xserver-utils (needed for xrandr)
  • nitrogen

The following are required for chreli-randomwall-hsetroot:

  • x11-xserver-utils (needed for xrandr)
  • hsetroot

Installation

To install, download chreli-randomwall_1.1.20170522.deb or chreli-randomwall-hsetroot_1.0.20170527.deb and execute the following command:

Disclaimer: This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

sudo /usr/bin/dpkg -i chreli-randomwall_1.1.20170527.deb

Or

sudo /usr/bin/dpkg -i chreli-randomwall-hsetroot_1.0.20170527.deb

Usage

For Openbox, add the following, for example in ~/.config/openbox/autostart:

sleep 2 && /usr/bin/chreli-randomwall &

Or

sleep 2 && /usr/bin/chreli-randomwall-hsetroot &

The above will start chreli-randomwall upon login to Openbox.

Configuration

Upon initial execution of chreli-randomwall, it will create and set defaults in ~/.config/chreli-randomwall/chreli-randomwall.conf:

# chreli-randomwall 1.1.20170527
#
# configuration file for chreli-randomwall

# interval (in seconds) to change wallpaper
# default: 300
chreli-randomwall.interval=300

# directories to search for images
#  o comma separate for multiple directories
# default: /home/cygnus/Pictures
chreli-randomwall.image.dirs=/home/cygnus/Pictures,/home/cygnus/pics

The default interval (when to change the wallpaper) is 300 seconds (5 minutes). The default directory to look for images is ~/Pictures. If there is a desire to use multiple directories, that can be accomplished by a comma separated list. Any new images that are added while chreli-randomwall is running will be used the next time it re-reads the image dir(s), which it does after it cycles though showing all the images. This could take a while depending on how many images are involved. For the impatient, the process id for chreli-randomwall can be killed off and started again; be sure to send it background.

As stated earlier, chreli-randomwall randomly change the wallpaper. Meaning that if you have 100 unique images, once you see the first one, you should not see it again until the other 99 are displayed; unless, of course, you have the same image multiple times in the directory with a different file name.

If you have multiple monitors (who doesn’t, right), chreli-randomwall will detect (via xrandr)that too and update ~/.config/nitrogen/bg-saved.cfg as shown in the example below:

[xin_-1]
file=/home/cygnus/Pictures/c62314c087379a9f2306412ca4268925.jpg
mode=0
bgcolor=#242424
[:0.0]
file=/home/cygnus/Pictures/c62314c087379a9f2306412ca4268925.jpg
mode=0
bgcolor=#242424

The current version of chreli-randomwall will show the same image on all monitors that may be configured. A future release may allow for a different image on each screen.

And that’s all I have to say about that.

Yet Another Openbox Pipe-Menu Generator

If you ever used the openbox-menu command and it doesn’t work, chreli-ob-menu is a reasonable alternative (I could have a biased opinion on that though). It is yet another simple perl script (perl is your friend, embrace it) that programmatically reads .desktop files in /usr/share/applications and ~/.local/share/applications and generates <openbox_pipe_menu> entries when called from ~/.config/openbox/menu.xml. It is also cognizant of new applications that are installed.

Installation

Download chreli-ob-menu_1.0.20160923.deb and execute the following:

Disclaimer: This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

sudo /usr/bin/dpkg -i chreli-ob-menu_1.0.20160923.deb

Usage

chreli-ob-menu [options]

Options:
    -a|--all                        generate pipe-menu for all categories
    -c|--category <category>        category to generate pipe-menu for
    -l|--list                       lists categories

    -h|--help                       display this help message
    -v|--version                    display version

Listing Supported Categories

chreli-ob-menu -l
Valid Categories:
 o Accessories
 o Development
 o Emulator
 o Game
 o Graphics
 o Internet
 o Multimedia
 o Office
 o Other
 o Preferences
 o System

Example Usage: Specific Category

<menu execute="/usr/bin/chreli-ob-menu -c System" id="openbox-System" label="System" icon="" />

The above will create a sub-menu called System with those applications using the category of System. If openbox has been compiled to show icons with menus, you can include the icon parameter that points to the image you would like to use.

Example Usage: All Categories

If you want to take the lazy approach and just have all the categories generate, you can do so by adding the following to ~/.config/openbox/menu.xml:

<menu execute="/usr/bin/chreli-ob-menu -a" id="desktop-all-menu" label="Menu" icon=""/>

Again, if openbox was compiled to include an icon with the menu items, you can give the icon parameter a value.

If you want just a static menu, you can call /usr/bin/chreli-ob-menu from the command line and copy-n-paste the output into ~/.config/openbox/menu.xml. You will, however, lose the dynamic capability when new applications are installed.

The chreli-ob-menu utility will do its level best to display icons to menu entries, including sub-menus, based on the gtk-icon-theme-name setting in ~/.gtkrc-2.0.

And that’s all I have to say about that.

Yet Another Simple Update Notifier

If you’re using a debian-based distro (such as Ubuntu or Linux Mint; both of which I view as Linux for Apple for Microsoft GUI babies either too lazy or afraid to use a terminal) or some derivative of RedHat (such as Fedora or CentOS) you may or may not have a systray applet that lets you know package updates are available. Below is a simple script, for debian-based installations, that will do just that.

The utility, chreli-update-check, written in Perl, leverages aptitude and yad (yet another dialog).

The aptitude command is as follows:

/usr/bin/aptitude search "~U" | /usr/bin/wc -l
3

The above instructs aptitude to search for upgradable packages (~U is shorthand) and is piped to the wc -l command to get the number of lines returned. You can read more about aptitude search terms here.

If you want to see what’s available to be updated, simply remove the | /usr/bin/wc -l from the above command:

/usr/bin/aptitude search "~U" 
i A libmenu-cache-bin    - LXDE implementation of the freedesktop Menu's cache (libexec)              
i A libmenu-cache3       - LXDE implementation of the freedesktop Menu's cache                        
i   pcmanfm              - extremely fast and lightweight file manager

If chreli-update-check detects updates (greater than 0 [zero]), it will construct the yad command to either display an in-your-face dialog: or a more subtle systray icon (displays details when moused over): The determination of either behavior is controlled by the chreli-update-check.nofification setting in ~/.config/chreli-update-check/chreli-update-check.conf; default is dialog.

To install chreli-update-check simply download chreli-update-check_1.0.20170520.deb and execute the following:

Disclaimer: This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

sudo /usr/bin/dpkg -i chreli-update-check_1.0.20170520.deb

Once installed, simply execute:

/usr/bin/chreli-update-check

To schedule its execution, add the following to your crontab:

15 */4 * * * DISPLAY=:0 /usr/bin/chreli-update-check > /dev/null 2>&1

The above will execute 15 minutes past, every 4 hours; you can adjust as you deem necessary.

The initial time chreli-update-check executes, it will create a chreli-update-check.conf in ~/.config/chreli-update-check with default settings for which icon to use (chreli-update-check.icon) and what type of notification (chreli-update-check.nofification):

# chreli-update-check 1.0.20170520
#
# configuration file for chreli-update-check

# icon to use for dialog or systray
# default: /usr/share/icons/Adwaita/16x16/apps/system-software-update.png
chreli-update-check.icon=/usr/share/icons/Adwaita/16x16/apps/system-software-update.png

# type of notification
# legal values:
#  o systray: displays icon in systray
#  o dialog : displays dialog
# default: dialog
chreli-update-check.nofification=systray

Adjust to your needs.

And that’s all I have to say about that.