Difference between revisions of "Conky/Tips and tricks"

From ArchWiki
Jump to: navigation, search
(added style note)
(migrated mail and gmail information over)
Line 38: Line 38:
  
 
This can be achieved by using the in python written extension named [http://evilshit.wordpress.com/2013/04/20/conkypress-a-wordpress-stats-visualization-tool-for-your-desktop/ ConkyPress].
 
This can be achieved by using the in python written extension named [http://evilshit.wordpress.com/2013/04/20/conkypress-a-wordpress-stats-visualization-tool-for-your-desktop/ ConkyPress].
 +
 +
== Display number of new emails ==
 +
 +
Conky has built in support for IMAP and POP3, but does not have support for access over ssl. Conky's FAQ recommends using {{Pkg|stunnel}} for this and has an example configuration [http://conky.sourceforge.net/faq.html#Conky's-built-in-IMAP-and-POP3-doesn't-support-SSL-or-TLS/ here].
 +
 +
Modify {{ic|/etc/stunnel/stunnel.conf}} as follows, and then [[start]] {{ic|stunnel.service}}:
 +
 +
# Service-level configuration for TLS server
 +
[imap]
 +
client = yes
 +
accept  = 143
 +
connect = imap.gmail.com:143
 +
protocol = imap
 +
sslVersion = TLSv1
 +
# Service-level configuration for SSL server
 +
[imaps]
 +
client = yes
 +
accept  = 993
 +
connect = imap.gmail.com:993
 +
 +
Then add the following to {{ic|conky.conf}}:
 +
 +
conky.config = {
 +
    imap = "localhost username password [-i 120] [-f 'inbox'] [-p 993]",
 +
}
 +
 +
conky.text {
 +
    Inbox: ${imap_unseen}/${imap_messages}
 +
}
 +
 +
=== Gmail ===
 +
If you use 2-factor authentication, you need to use an [https://myaccount.google.com/apppasswords App Password].
 +
 +
For method 1, 2 and 3:
 +
 +
Create one of the following files in a convenient location (for example in {{ic|~/.scripts/}}).
 +
 +
Then add the following string to your {{ic|conky.conf}} in order the check your Gmail account for new email every five minutes (300 seconds) and display:
 +
${execi 300 python ~/.scripts/gmail.py}
 +
 +
==== method 1 ====
 +
This script uses retrieves the number of new email via Gmail's Atom API.
 +
{{hc|gmail.py|<nowiki>
 +
#!/usr/bin/env python3
 +
 +
import urllib.request
 +
 +
email = 'your email'
 +
password = 'your password'
 +
 +
# Set up authentication for gmail
 +
auth_handler = urllib.request.HTTPBasicAuthHandler()
 +
auth_handler.add_password(realm='mail.google.com',
 +
                          uri='https://mail.google.com/',
 +
                          user=email,
 +
                          passwd=password)
 +
opener = urllib.request.build_opener(auth_handler)
 +
# ...and install it globally so it can be used with urlopen.
 +
urllib.request.install_opener(opener)
 +
 +
gmailurl = 'https://mail.google.com/gmail/feed/atom'
 +
with urllib.request.urlopen(gmailurl) as page:
 +
    contents = page.read().decode('utf-8')
 +
 +
ifrom = contents.index('<fullcount>') + 11
 +
ito  = contents.index('</fullcount>')
 +
 +
fullcount = contents[ifrom:ito]
 +
 +
print('{} new emails'.format(fullcount))
 +
 +
</nowiki>}}
 +
 +
==== method 2 ====
 +
Same as method 1, but does proper XML parsing.
 +
 +
{{hc|gmail.py|<nowiki>
 +
#!/usr/bin/env python3
 +
 +
import urllib.request
 +
from xml.etree import ElementTree as etree
 +
 +
email = 'your email'
 +
password = 'your password'
 +
 +
# Set up authentication for gmail
 +
auth_handler = urllib.request.HTTPBasicAuthHandler()
 +
auth_handler.add_password(realm='mail.google.com',
 +
                          uri='https://mail.google.com/',
 +
                          user=email,
 +
                          passwd=password)
 +
opener = urllib.request.build_opener(auth_handler)
 +
# ...and install it globally so it can be used with urlopen.
 +
urllib.request.install_opener(opener)
 +
 +
gmailurl = 'https://mail.google.com/gmail/feed/atom'
 +
NS = '{http://purl.org/atom/ns#}'
 +
with urllib.request.urlopen(gmailurl) as source:
 +
    tree = etree.parse(source)
 +
fullcount = tree.find(NS + 'fullcount').text
 +
 +
print('{} new emails'.format(fullcount))
 +
</nowiki>}}
 +
 +
==== method 3 ====
 +
 +
The same way, but with using {{ic|curl}}, {{ic|grep}} and {{ic|sed}}:
 +
 +
{{hc|gmail.sh|
 +
#!/usr/bin/sh
 +
 +
curl -s -u '''email''':'''password''' <nowiki>https://mail.google.com/mail/feed/atom | grep fullcount | sed 's/<[^0-9]*>//g'</nowiki>
 +
}}
 +
 +
replace ''email'' and ''password'' with your data.
 +
 +
==== IMAP + SSL using Perl ====
 +
 +
''Conky'' has built in support for IMAP accounts but does not support SSL. This can be provided using this script from [http://www.unix.com/shell-programming-scripting/115322-perl-conky-gmail-imap-unread-message-count.html this forum post]. This requires the Perl/CPAN Modules Mail::IMAPClient and IO::Socket::SSL which are in the {{AUR|perl-mail-imapclient}} and {{Pkg|perl-io-socket-ssl}} packages
 +
 +
Create a file named {{ic|imap.pl}} in a location to be read by ''conky'' (for example in {{ic|~/.scripts/}}). In this file, add (with the appropriate changes):
 +
{{hc|imap.pl|<nowiki>
 +
#!/usr/bin/perl
 +
 +
# by gxmsgx
 +
# description: get the count of unread messages on imap
 +
 +
use strict;
 +
use Mail::IMAPClient;
 +
use IO::Socket::SSL;
 +
 +
my $username = 'example.username';
 +
my $password = 'password123';
 +
 +
my $socket = IO::Socket::SSL->new(
 +
  PeerAddr => 'imap.server',
 +
  PeerPort => 993
 +
)
 +
or die "socket(): $@";
 +
 +
my $client = Mail::IMAPClient->new(
 +
  Socket  => $socket,
 +
  User    => $username,
 +
  Password => $password,
 +
)
 +
or die "new(): $@";
 +
 +
if ($client->IsAuthenticated()) {
 +
  my $msgct;
 +
 +
  $client->select("INBOX");
 +
  $msgct = $client->unseen_count||'0';
 +
  print "$msgct\n";
 +
}
 +
 +
$client->logout();
 +
</nowiki>}}
 +
 +
Add to {{ic|conky.conf}}:
 +
${execi 300 ~/.scripts/imap.pl}
 +
or wherever you saved the file.
 +
 +
If you use Gmail you might need to [http://www.google.com/accounts/IssuedAuthSubTokens?hide_authsub=1 generate] an application specific password.
 +
 +
Alternatively, you can use stunnel as shown above: [[#Gmail]]
 +
 +
==== IMAP using PHP ====
 +
Another alternative using PHP. PHP needs to be installed and {{ic|1=extension=imap.so}} must be uncommented in {{ic|/etc/php/php.ini}}.
 +
 +
Then create a file named {{ic|imap.php}} in a location to be read by ''conky'' (for example in {{ic|~/.scripts/}}). Make the file executable:
 +
$ chmod +x imap.php
 +
 +
In this file, add (with the appropriate changes):
 +
 +
{{hc|imap.php|<nowiki>
 +
#!/usr/bin/php
 +
<?php
 +
// See http://php.net/manual/function.imap-open.php for more information about
 +
// the mailbox string in the first parameter of imap_open.
 +
// This example is ready to use with Office 365 Exchange Mails,
 +
// just replace your username (=email address) and the password.
 +
$mbox = imap_open("{outlook.office365.com:993/imap/ssl/novalidate-cert}", "username", "password");
 +
 +
// Total number of emails
 +
$nrTotal = imap_num_msg($mbox);
 +
 +
// Number of unseen emails. There are other ways using imap_status to count
 +
// unseen messages, but they don't work with Office 365 Exchange. This one does.
 +
$unseen = imap_search($mbox, 'UNSEEN');
 +
$nrUnseen = $unseen ? count($unseen) : 0;
 +
 +
// Display the result, format as you like.
 +
echo $nrUnseen.'/'.$nrTotal;
 +
 +
// Not needed, because the connection is closed after the script end.
 +
// For the sake of clean public available scripts, we are nice to
 +
// the imap server and close the connection manually.
 +
imap_close($mbox);
 +
</nowiki>}}
 +
 +
Add to {{ic|conky.conf}}:
 +
 +
${execi 300 ~/.scripts/imap.php}
 +
 +
or wherever you saved the file.
 +
 +
This script displays A/B where A is the number of unseen emails and B is the total number of mails in the mailbox. There are a lot of other informations available through a lot of PHP functions like with imap_Status (http://php.net/manual/function.imap-status.php). Just see the PHP docs about IMAP: http://php.net/manual/ref.imap.php.

Revision as of 22:06, 14 March 2018


Tango-edit-clear.pngThis article or section needs language, wiki syntax or style improvements.Tango-edit-clear.png

Reason: Conky's configuration changed and it is unknown if all of these tips migrated from the main article are still working/relevant. (Discuss in Talk:Conky/Tips and tricks#)

Go back to Conky.

Display package update information

Pacman provides its own script called checkupdates which displays package updates from the official repos. Use ${execi 3600 checkupdates | wc -l} to display the total number of packages.

Display weather forecast

See this thread.

Display a countdown timer

ConkyTimer is a simple countdown timer that displays the remaining time of a defined task.

Start the timer using conkytimer "<task description>" <min>.

Display RSS feeds

Conky has the ability to display RSS feeds natively without the need for an outside script to run and output into Conky. For example, to display the titles of the ten most recent Planet Arch updates and refresh the feed every minute, you would put this into your conky.conf in the TEXT section:

${rss https://planet.archlinux.org/rss20.xml 1 item_titles 10 }

If you want to display Arch Forum rss feed, add this line:

${rss https://bbs.archlinux.org/extern.php?action=feed&type=rss 1 item_titles 4}

where 1 is in minutes the refresh interval (15 mn is default),4 the number of items you wish to show.

Display rTorrent stats

See this thread.

Display your WordPress blog stats

This can be achieved by using the in python written extension named ConkyPress.

Display number of new emails

Conky has built in support for IMAP and POP3, but does not have support for access over ssl. Conky's FAQ recommends using stunnel for this and has an example configuration here.

Modify /etc/stunnel/stunnel.conf as follows, and then start stunnel.service:

  1. Service-level configuration for TLS server
[imap]
client = yes
accept  = 143
connect = imap.gmail.com:143
protocol = imap
sslVersion = TLSv1
# Service-level configuration for SSL server
[imaps]
client = yes
accept  = 993
connect = imap.gmail.com:993

Then add the following to conky.conf:

conky.config = {
    imap = "localhost username password [-i 120] [-f 'inbox'] [-p 993]",
}
conky.text {
    Inbox: ${imap_unseen}/${imap_messages}
}

Gmail

If you use 2-factor authentication, you need to use an App Password.

For method 1, 2 and 3:

Create one of the following files in a convenient location (for example in ~/.scripts/).

Then add the following string to your conky.conf in order the check your Gmail account for new email every five minutes (300 seconds) and display:

${execi 300 python ~/.scripts/gmail.py}

method 1

This script uses retrieves the number of new email via Gmail's Atom API.

gmail.py
#!/usr/bin/env python3

import urllib.request

email = 'your email'
password = 'your password'

# Set up authentication for gmail
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm='mail.google.com',
                          uri='https://mail.google.com/',
                          user=email,
                          passwd=password)
opener = urllib.request.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib.request.install_opener(opener)

gmailurl = 'https://mail.google.com/gmail/feed/atom'
with urllib.request.urlopen(gmailurl) as page:
    contents = page.read().decode('utf-8')

ifrom = contents.index('<fullcount>') + 11
ito   = contents.index('</fullcount>')

fullcount = contents[ifrom:ito]

print('{} new emails'.format(fullcount))

method 2

Same as method 1, but does proper XML parsing.

gmail.py
#!/usr/bin/env python3

import urllib.request
from xml.etree import ElementTree as etree

email = 'your email'
password = 'your password'

# Set up authentication for gmail
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm='mail.google.com',
                          uri='https://mail.google.com/',
                          user=email,
                          passwd=password)
opener = urllib.request.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib.request.install_opener(opener)

gmailurl = 'https://mail.google.com/gmail/feed/atom'
NS = '{http://purl.org/atom/ns#}'
with urllib.request.urlopen(gmailurl) as source:
    tree = etree.parse(source)
fullcount = tree.find(NS + 'fullcount').text

print('{} new emails'.format(fullcount))

method 3

The same way, but with using curl, grep and sed:

gmail.sh
#!/usr/bin/sh

curl -s -u email:password https://mail.google.com/mail/feed/atom | grep fullcount | sed 's/<[^0-9]*>//g'

replace email and password with your data.

IMAP + SSL using Perl

Conky has built in support for IMAP accounts but does not support SSL. This can be provided using this script from this forum post. This requires the Perl/CPAN Modules Mail::IMAPClient and IO::Socket::SSL which are in the perl-mail-imapclientAUR and perl-io-socket-ssl packages

Create a file named imap.pl in a location to be read by conky (for example in ~/.scripts/). In this file, add (with the appropriate changes):

imap.pl
#!/usr/bin/perl
 
# by gxmsgx
# description: get the count of unread messages on imap

use strict;
use Mail::IMAPClient;
use IO::Socket::SSL;

my $username = 'example.username'; 
my $password = 'password123'; 

my $socket = IO::Socket::SSL->new(
  PeerAddr => 'imap.server',
  PeerPort => 993
 )
 or die "socket(): $@";
 
my $client = Mail::IMAPClient->new(
  Socket   => $socket,
  User     => $username,
  Password => $password,
 )
 or die "new(): $@";
 
if ($client->IsAuthenticated()) {
  my $msgct;

  $client->select("INBOX");
  $msgct = $client->unseen_count||'0';
  print "$msgct\n";
}

$client->logout();

Add to conky.conf:

${execi 300 ~/.scripts/imap.pl} 

or wherever you saved the file.

If you use Gmail you might need to generate an application specific password.

Alternatively, you can use stunnel as shown above: #Gmail

IMAP using PHP

Another alternative using PHP. PHP needs to be installed and extension=imap.so must be uncommented in /etc/php/php.ini.

Then create a file named imap.php in a location to be read by conky (for example in ~/.scripts/). Make the file executable:

$ chmod +x imap.php

In this file, add (with the appropriate changes):

imap.php
#!/usr/bin/php
<?php
// See http://php.net/manual/function.imap-open.php for more information about
// the mailbox string in the first parameter of imap_open.
// This example is ready to use with Office 365 Exchange Mails,
// just replace your username (=email address) and the password.
$mbox = imap_open("{outlook.office365.com:993/imap/ssl/novalidate-cert}", "username", "password");

// Total number of emails
$nrTotal = imap_num_msg($mbox);
 
// Number of unseen emails. There are other ways using imap_status to count
// unseen messages, but they don't work with Office 365 Exchange. This one does.
$unseen = imap_search($mbox, 'UNSEEN');
$nrUnseen = $unseen ? count($unseen) : 0;
 
// Display the result, format as you like.
echo $nrUnseen.'/'.$nrTotal;
 
// Not needed, because the connection is closed after the script end.
// For the sake of clean public available scripts, we are nice to
// the imap server and close the connection manually.
imap_close($mbox);

Add to conky.conf:

${execi 300 ~/.scripts/imap.php} 

or wherever you saved the file.

This script displays A/B where A is the number of unseen emails and B is the total number of mails in the mailbox. There are a lot of other informations available through a lot of PHP functions like with imap_Status (http://php.net/manual/function.imap-status.php). Just see the PHP docs about IMAP: http://php.net/manual/ref.imap.php.