Pidgin and batch rename in python

I remembered that I can use pidgin! I don’t know what I haven’t been using it for a while. But now that I’ve installed it again, I went and downloaded yahoo smilies (from here) and the according to this page I had to copy the unzipped file to ~/.purple/smileys folder. Everything worked fine, expect since the file I downloaded had a bunch of files that were all named like yahoo\26.gif and that made pidgin not to recognize them! So I had to change them, but it seems like also shell doesn’t like \ in file names either. So I asked python to help me. Here is the code:

import os
directory = '/home/k1/.purple/smileys/yahoo/'
for filename in os.listdir(directory):
    if filename.startswith('yahoo'):
            print('renaming', filename)               
            os.rename(directory+filename, directory+filename[6:])

This deletes yahoo\ part of each file that has it. In the if part I couldn’t write if filename.startswith('yahoo\'): and if I wanted to be precise I had to write if filename.startswith('yahoo\\'):. Anyways, going to pidgin, tools, preferences, themes, I chose the yahoo smilies from the dropdown list.
The following is the info from the mentioned page above:

Smiley Themes

Installing

One important thing to remember is that these smileys only show up on your side. Unless the person you’re talking to has the same theme as you installed in the same client, it’s not guaranteed that they will see the same pictures as you for the smileys.

The way smiley themes work is quite simple. We’ll use an example to explain how it works. The name of this theme will be “Hello World!”.

If we wanted to install Hello World! into pidgin, we would have to do one of three things:

  • Copy a folder, whose contents are described below, into ~/.purple/smileys (See the FAQ for the location of ~/.purple on Windows)
  • Drag a tar.gz file (it must be a gzipped tarball), whose contents are described below, onto the list of smiley themes in Tools -> Preferences
  • Click the “Add” button in the “Smiley Themes” tab in the preferences, navigate to your <theme>.tar.gz file, and click “Open”.

Creating

The first thing you need to know is the contents of the tarball you drag into the smiley themes list. It contains the folder that is described in option 1 above. In our case, this folder would probably be called “Hello_World”.

Now to describe the contents of Hello_World. This folder contains all the information Pidgin needs to use your theme. It has a lot of picture files that will be how your smileys show up (to you) and a text file named “theme”, which tells pidgin what to do with all those pictures. For this example, lets let our list of picture files be:

  • happy.png
  • sad.png
  • grin.png
  • hidden.png
  • MSN_only.gif
  • GoogleTalk.gif

Then let our theme file look like so:

Name=Hello World!
Description=Smiley Theme How-To
Icon=happy.png
Author=Ankit Singla

[default]
! hidden.png			C:-) C:)
sad.png				😦 😦
happy.png			πŸ™‚ πŸ™‚
grin.png		πŸ˜€ :-d

#These show up only in MSN
[MSN]
sad.png				😦 😦
happy.png			πŸ™‚ πŸ™‚
grin.png		πŸ˜€ :-d
MSN_only.gif	:-M :-$

#These show up only in XMPP
[XMPP]
sad.png				😦 😦
happy.png			πŸ™‚ πŸ™‚
grin.png		πŸ˜€ :-d
GoogleTalk		:-G :T

Blank lines and lines that start with # are ignored. The # means this line is a comment. Lines starting with a ! means to hide that particular emoticon from the emoticon selector dialog.

The first four lines are part of any theme. The first line is the name of our theme, in our case “Hello World!”. This shows up in the theme selector in bold. The second line is the description of our theme. This shows up underneath the name and author in the theme selector. The third line is the icon to show in the theme selector, which is a quick way for the user to see which theme they’re selecting. The last line is the author of the theme. This shows up next to the name of the theme in the theme selector.

Next come the protocol specific sets of smileys. A set of smileys is defined in the following format:

[<protocol>]
<picture to use>	<text to replace>
.
.
.

For example:

[MSN]
sad.png				😦 😦
happy.png			πŸ™‚ πŸ™‚
grin.png		πŸ˜€ :-d
MSN_only.gif	:-M :-$

This means, the first line defines which protocol this set defines. In our example, this set will define all of the MSN pictures. Each line after that specifies how to handle each picture you want to use in that protocol. The first element on this line is the name of the picture, so for our first picture, we have sad.png. This must be followed by some whitespace and then a list of strings, separated by whitespace, that will be replaced by the picture sad.png in conversation. So in our case, we’re telling pidgin that if it ever sees “:(” or “:-(“, it shouln’t show us “:(” or “:-(“. Instead, it should replace it with sad.png so we see that picture instead.

The first set defined in a theme file is usually the default set. This is the set of smileys that pidgin uses if it doesn’t find a protocol specific set for that protocol in the rest of the file. So in our file, it will use the default set for AIM, ICQ, IRC, etc. If you notice, in the default set there’s a line that starts with an exclamation point. This means the smiley is hidden. If someone types the text associated with that picture, you’ll see the picture, but you won’t be able to pick that smiley from the smiley selector that pops up from the formatting toolbar.

That’s it! Congratulations, you know how to make a theme file.

Advertisements

Merge PDF files

Try the good ghostscript:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=merged.pdf mine1.pdf mine2.pdf

or even this way for an improved version for low resolution PDFs (thanks to Adriano for pointing this out):

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=merged.pdf mine1.pdf mine2.pdf

In both cases the ouput resolution is much higher and better than this way using convert:

convert -density 300x300 -quality 100 mine1.pdf mine2.pdf merged.pdf

In this way you wouldn’t need to install anything else, just work with what you already have installed in your system (at least both come by default in my rhel).

Source: Stack Overflow

What to do with a long list?

I mean a list in SAGE. The answer is to

save(mylist, 'mylist')

and then later

mynewlist = load('mylist')

This will save your list into a file called mylist.sobj and then loads it. The story is I calculated a list through a long calculation to do some other calculations with it. So I saved the list in a .txt file and in the file it looks like this:

[[[-1/10, 1.02421477975960],
  [-99/1000, 1.02369404664481],
  [-49/500, 1.02317986236459],
  [-97/1000, 1.02267219319285],
  ...

But then that I needed to read it, I had to open the text file and copy and paste it into SAGE. If the list is short, I think it’s an easy way to do it, but if the list is long (a few gigabytes maybe?) you don’t want to open that text file, forget about copying it! So, one way to read it was to strip all the \n‘s and get rid of the brackets and all the other things in a nice way and then read it as a csv file, which I could save it as a csv in the first place with:

import csv 
with open('mylist.csv', 'w') as f1:
    writefile = csv.writer(f1)
    writefile.writerows(mylist)

And then read it with

import csv 
with open('mylist.csv','rU') as f1:
    mynewlist =load('myfile')

The problem is I still had to go through the list and change strings to numbers. Something I really don’t want to do. So, the save and load file is the best thing.

save(mylist, 'mylist')
mynewlist = load('mylist')

Resource: AskSageMath community.

sftp stories

When I was at WIU I had this website on their server that I had to maintain through their sftp server. It’s been a while I have forgotten how to connect to do it. So, I am rediscovering it step by step, and here is a map for later!

  1. Connect to their VPN:
    1. Currently they use a cisco vpn client (Cisco AnyConnect Security Mobility Client Version 3.1.12020 Copyright (c) 2004 – 2015 Cisco Systems).
    2. Connect to vpn.wiu.edu with ECom username and password
  2. Connect to sftp server:
    1. In a file browser type: sftp://sftp.wiu.edu
      Here I get the following error:

      Oops! Something went wrong.
      Unhandled error message: SSH program unexpectedly exited
    2. A google search tells me that it might be due my IP not being in a whitelist that the server uses. It is a little weird if that is the case, sice I’m connected to their vpn and I should be assigned a local IP address. I’ve contacted them to see what the case is.
    3. It turns out that they had restricted my user since I’m not a faculty member any more. But I got access for a week to update my page to link to my current page. The above command worked.

converting time to color

So, Danny pointed out this website and asked if one can write a script to do this on a computer. Here is what I came up with:

#!/bin/bash
while true
do
	#read time
	TIME=$(date +"%T") 

	#delete colons and add a pound sign to the begining
	COLOR=$(echo "$(echo $TIME | sed 's/://g')" | sed 's/^/#/') 

	#to change the background color I need to change it in the profile, so first I find the ID, here I'm acutally assuming there's only 1 ID
	MYID=$(dconf list /org/gnome/terminal/legacy/profiles:/ | sed '1d' | sed 's/\///g' ) 

	#this is where things need to be changed
	BAK=/org/gnome/terminal/legacy/profiles:/$MYID/background-color 

	#clear the terminal screen
	clear 
	#change the color of background
	dconf write $BAK "'$COLOR'" 

	#this is to center the text
	MyX=$(stty size | awk '{print $1}') #get the number of rows of terminal
	MyY=$(stty size | awk '{print $2}') #get the number of columns of terminal

	HalfX=$((($MyX)/2)) #divide the x by two
	HalfY=$((($MyY-5)/2)) #divide the y by 2. Here -5 is accounting for the half of the length of the stirng to be printed
	
	#puch the cursor to the middle of the screen
	tput cup $HalfX $HalfY
	#write time
	echo ${TIME}
	#push the cursor to the last line
	tput cup $MyX 0
	#wait for a second
	sleep 1
done

I bet one can make it look nicer πŸ™‚

Spotify on linux

Here I’m going to collect my attempts at installing and using spotify client on fedora 22.

Install one of the latest versions (currently 1.01something):

The installation process is easy since the negotivo17 team has repackaged the ubuntu client for fedora. Here is how to install it:

sudo dnf config-manager --add-repo=http://negativo17.org/repos/fedora-spotify.repo
sudo dnf install spotify-client

The bugs are discussed here: http://negativo17.org/spotify-client

Install version 0.9.17.1:

Why would anyone want to install an old version? ads. I’ve found the following method the installs from the source, and it actually doesn’t need a root access.

wget https://github.com/leamas/spotify-make/tarball/master/spotify-make.tar.gz
tar xzf spotify-make.tar.gz
cd leamas-spotify-make-*
./configure --user
make download
make install
make register
~/bin/my-spotify

So, that does the job. For more info read the full readme file with the links:

## Spotify installer README

This is an attempt to create something which installs the spotify
debian package[1] in a distro independent way. The goal is to support
three usecases:

- User-only install without root privileges.
- System-wide FHS-compliant installs in e. g., /usr/local or /usr.
- Temporary installs used in a packaging context.

## News

2013-05-03: Updated to upstream 0.9.0.133 release.

## Installation

Either use git:
```
$ git cloneΒ  https://github.com/leamas/spotify-make.git
$ cd spotify-make
```
or download a tarball and use that:
```
$ wget https://github.com/leamas/spotify-make/tarball/master/spotify-make.tar.gz
$ tar xzf spotify-make.tar.gz
$ cd leamas-spotify-make-*
```

## Usage

Although not based on automake in any way it's run in the same way
using configure, make and make install. Some examples:

Making a user install without root privileges installs in
~/.local/share with a binary in ~/bin, downloading sources
as required:
```
$ ./configure --user
$ make download
$ make install
$ make register
$ ~/bin/my-spotify
```
Installing in system directories e. g., installing in /opt/local/lib and
/opt/local/share with a binary in /usr/bin. (yes, bad example, I know):
```
$ ./configureΒ  --prefix=/opt/local --bindir=/usr/bin
$ make download
# make install
# make register
$ spotify
```
Installing in a temporary dir e. g. in a packaging context with all
sources already in place and the % substitutions available. The
--package flag disables downloading of any data from spotify.
```
$ ./configure --prefix=/usr --libdir=%{_libdir} --package=%{SOURCE1}
$ make install DESTDIR=/tmp/spotify
```
Other variants are possible using e. g. the --bindir or --libdir
arguments to configure. Use configure -h to find out. The register
target notifies system about new icons etc. To uninstall the thing:
```
$ cd "The $(datadir) used when installing"
$ make uninstall
```
As of now, this is just an experiment although there are successful usecases
from Fedora, Ubuntu, Mageia and SUSE. There are certainly fixes needed to
stabilize things though.

## Dependencies

This is an installer, not a package. It has no automatic dependencies.
That said, the configure script tries to check the buildtime dependencies
and reports them as ERROR e. g.,
```
$ ./configure --user
Checking build and support dependencies
...
make: ERROR: Not found
```
You are on your own here: you must find out the package which provides
make and install it.

The Makefile tracks the runtime dependencies from spotify using ldd. It
tries to symlink to existing system libraries in simple cases. This is
reported as INFO: lines. Unresolved runtime dependencies in spotify is
reported as WARNING: lines e. g.:
```
$ make install
...
WARNING; cant resolve spotify dependency: libQtGui.so.4
```
Likewise, here you have to find the package providing libQtGui.so.4
and install it.

## User installs

User installs have some caveats:

- The binary is called my-spotify, to allow parallel install with a
system-wide 'spotify' installation.
- The binary is by default installed in ~/bin.
- The desktop fileΒ  and icons are installed under ~/.local/share. This is
according to freedesktop specs, and most tools will find them there.
- Some systems (notably Ubuntu/unity) requires a logout-login sequence to
pickup the installation in the menus.
- Manpage goes to ~/.local/share/man/man1. Your MANPATH will probably need
an update to includeΒ  $HOME/.local/share/man. Change dir with --mandir=
to ./configure. Manpage isn't that useful anyway.

## License

These files are in public domain, you can do whatever you like with them.
Remember that Spotify's own terms are unclear but ATM said to be
"non-redistributable, no changes permitted"

## System notes

### Ubuntu
- Works occasionally. 12.10 32- and 64-bits minimally tested.
13.10 success stories reported.
- Bundles the correct Debian libssl0.9.8.0.9.8o-4squeeze13.
- Desktop (unity) does not pick up the newly installed package until
after logout-login.

### Fedora
- Solves the libssl0.9.8 problem by bundling.
- Works, both 64 and 32-bit.

### SUSE
- Current spec actually Conflicts libssl0.9.8, linking to 1.0.0 seems
to work OK.
- Have some discussion about possible merging of spotify-make into current spec
and installer, a fork is available [7].
- 32-bit release 12.2 and upcoming 12.3 (Factory) minimally tested.

### Mageia
Minimally tested on version 2, 32-bit desktop release. Package bundles
libqtdbus4-4.8.1 required by spotify; system version is 4.8.2

## Debian
- Success stories reported for Wheezy 7.0, bug fixed for 7.1 (issue #13)

## CentOS
- Partial success story on CentOS 6.4 in [6]

### Linux Mint
Handled the same way as Ubuntu. Version 14 32-bit minimally tested.

## References

[1] http://community.spotify.com/t5/Help-Desktop-Linux-Mac-and/bd-p/001

[2] https://help.ubuntu.com/community/SwitchingToUbuntu/FromLinux/RedHatEnterpriseLinuxAndFedora

[3] https://wiki.archlinux.org/index.php/Pacman_Rosetta

[4] https://bugs.launchpad.net/ubuntu/+source/desktop-file-utils/+bug/592671

[5] https://bugs.launchpad.net/ubuntu/+source/meta-gnome2/+bug/572918

[6] http://community.spotify.com/t5/Help-Desktop-Linux-Mac-and/Linux-Installer-script-Ubuntu-SUSE-Mageia-Mint/m-p/240886/highlight/true#M8789

[7] https://github.com/leamas/opensuse-spotify-installer

Now it’s time for muting the annoying ads. Follow this github thread, and this one.

Making quick notes and searching through them

There are several times that I just want to jot down something and I don’t have time to open a note, or I don’t want it to get lost somewhere, or I don’t want to decide where to save the file and what to call it. So I have the following little script does the job for me. I called it quicknote and assigned a short alias to it in my .bashrc: alias n='~/opt/quicknote'.

#!/bin/sh
## date format ##
NOW=$(date +"%F")
NOWT=$(date +"%T")

## path and file ##
BAK="~/Dropbox/Drafts"
FILE="$BAK/qnote-$NOW-$NOWT"

input=$*
echo "$input" >> "$FILE"

So, what this does is it takes whatever one line input that I have and puts it in a file in a folder in my dropbox folder with names qnote-CURRENT_DATE-DURENT_TIME.

But sometimes I actally want to write a real note with multiple lines, but still I don’t want to decide where to put it and what to call it. So I have another script that makes a file named note-CURRENT_DATE-CURRENT_TIME, and puts it in that folder in my dropbox, then opens a text editor for me to edit it. Here it is:

#!/bin/sh

## date format ##
DATE=$(date +"%F")
TIME=$(date +"%T")
 
## Backup path ##
BAK="~/Dropbox/Drafts"
FILE="$BAK/note$DATE-$TIME"

gedit $FILE

So far so good, but the problem is, now I don’t know what’s in what file. So I just use grep to search for keywords. It might not be very fun to set up all the things for the grep command each time you want to run it for this specific task. So, here goes another little script, which I save it in a file called drafts.

#!/bin/bash

if [[ $(echo $*) ]]; then
    searchterm="$*"
else
    read -p "Enter your search term: " searchterm
fi

searchterm=$(echo $searchterm | sed -e 's/\ /+/g')

egrep -r -I -i -H "$searchterm" ~/Dropbox/Drafts/* --color

The good things about using this routine than just input=$* is that I can just run drafts, and then it asks me to enter my search words, in this case, if I want to search for either of two words I can use word1 | word2. Of course I could just run drafts word1 \| word2 and it would do the same thing. Any ways, this will get my work done.

ToDo: I should sit and read Sed and Awk once. Maybe on the flights this January πŸ˜€