Get any GitHub user’s email (Python)

Here’s a silly little Python script ( I wrote to get a GitHub user’s email address based on their public event history.

It’s not bulletproof, but generally works:

from __future__ import print_function

import requests
import sys

from optparse import OptionParser

parser = OptionParser()
parser.add_option('-u', '--username', action='store', type='string', dest='username')
(options, args) = parser.parse_args()

gh_api_url = '{username}/events/public'.format(username=options.username)

r = requests.get(gh_api_url)

gh_public_events = r.json()

if isinstance(gh_public_events, dict):
    if gh_public_events.get('message') and gh_public_events.get('message') == 'Not Found':
        print('User was not found!')

fullname = 'N/A'
email = 'N/A'

for event in gh_public_events:
    if event.get('payload').get('commits'):
        commits = event.get('payload').get('commits')
        for commit in commits:
            if commit.get('author'):
                fullname = commit.get('author').get('name')
                email = commit.get('author').get('email')

print('Full Name: {name}'.format(name=fullname))
print('email: {email}'.format(email=email))

You can simply save this script as (for example).

Also, do note, it uses Python 3, a deprecated standard library (optparse has been replaced by argparse) and one third party library, requests.

Usage Output

Usage: [options]

  -h, --help            show this help message and exit
  -u USERNAME, --username=USERNAME

Expected Results

If the user exists and has public events:

$ -u jbarnette
Full Name: John Barnette

If the user exists, but has no public events, you’ll get:

$ -u test
Full Name: N/A
email: N/A

If the user does not exist, you’ll get:

$ -u iou18y23123
User was not found!

And that’s how you get (almost) any GitHub user’s email!
It’s useful if you want to reach out to another contributor via email.


The script is inspired from this blog post: How to Find Almost Any GitHub User’s Email Address

If you rather do this process manually, follow the instructions in the link right above.


A few piped Linux commands and short scripts

Here’s a couple of bash and Perl scripts, and piped Linux commands that I’ve found to be useful. Let me know if you are having trouble with any of these. Your mileage may vary depending on your version of bash and Perl.

Current CPU Usage in percent (bash):

You can get the same info from other tools like htop and mpstat, but they might not be installed by default.

# Linux
top -bn 10 -d 0.01 | grep '^Cpu.s.' | tail -n 1 | awk '{cpu_use = $2 + $4 + $6 ; printf("%.2f\n", cpu_use)}'
# Solaris
top -bn 10 -d 0.01 | grep 'CPU states' | awk -F ' |%' '{cpu_use = $7 + $11 ; printf("%.2f\n", cpu_use)}'

Current RAM Usage in percent (bash):

# Linux
free -m | awk 'FNR == 2 { ratio = $3 / $2 ; ram_use = ratio * 100 ; printf("%.2f\n", ram_use)}'
# Solaris
top -bn 10 -d 0.01 | grep 'Memory' | awk -F \" |G\" '{used = $2 - $6 ; ram_use = (used / $2) * 100 ; printf("%.2f\n", ram_use)}'

System Usage (perl):

my $linuxCPUpct = "top -bn 10 -d 0.01 | grep '^Cpu.s.' | tail -n 1" ;  # CPU usage
my $linuxRAMpct = "free -m | grep 'Mem:'" ;  	# RAM usage
my $linuxROOTpct = "df -hl / | grep '/'" ;  	# / disk usage
my $linuxHOMEpct = "df -hl /home | grep 'home'" ;  # /home disk usage

my $cpu_res = "" ; my $ram_res = "" ; my $root_res = "" ; my $home_res = "" ;

my @CPU_parse = split ( ',', `$linuxCPUpct` ) ;
my @RAM_parse = split ( ' ', `$linuxRAMpct` );
my $linuxROOT = (`$linuxROOTpct` =~ /(\d+)%/img)[0] ;
my $linuxHOME = (`$linuxHOMEpct` =~ /(\d+)%/img)[0] ;

$ram_res = sprintf("%.2f", $RAM_parse[2] / $RAM_parse[1] * 100.0 ) ;
$cpu_res = sprintf("%.2f", substr($CPU_parse[3], 0, -3)) ;
$root_res = sprintf("%.2f", $linuxROOT) ;
$home_res = sprintf("%.2f", $linuxHOME) ;

print ("CPU Usage: $cpu_res\n") ;
print ("RAM Usage: $ram_res\n") ;
print ("/ Usage: $root_res\n") ;
print ("/home Usage: $home_res\n") ;

Reset Password expiry script (bash):

Perhaps not the best idea security wise, but it’s handy when you’re lazy.

pwd_age=$(grep "username:" /etc/shadow | cut -d: -f 3)
echo $pwd_age;
now=$(( $(date +%s) / 3600 / 24 ))
echo $now;
age_at_expiry_date=$(( $now + 90 - $pwd_age))
echo $age_at_expiry_date;
chage username -M $age_at_expiry_date

Multi-Ping script (bash):

Self-explanatory. Pass the IPs as arguments to mping.

# mping: Multiple ping
# Script to ping multiple units. Each unit is pinged 4 times.


for args
        ping -c 4 ${arrArgs[$i]}
        echo -e "\n"

And a few words of wisdom…


Getting the System Time in C (Function)

It has been a while since I’ve posted a tutorial, so I figured I would go over a simple example on how to write a function to return the system time in C.

The code below has only been tested on a handful of UNIX systems (RHEL 6.5, SLES 10 and Solaris 10), therefore I can’t guarantee the output on Windows systems (but it really should, considering time.h is a part of the Standard C library).

Overall, it is really straightforward and mostly self-explanatory if you’re at all familiar with C, and my solution is partially borrowed from a post on StackOverflow (with a few enhancements).

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

const char * getSystemTime()
    struct timeval tp;
    struct tm *t;
    char *current_time;
    time_t curtime;    
    gettimeofday(&tp, 0); //Pass tp to library function gettimeofday()
    curtime = tp.tv_sec;
    t = localtime(&curtime);
    current_time = (char *) malloc(sizeof(char) * 18);
                         t->tm_hour, t->tm_min, t->tm_sec, tp.tv_usec/1000);
    return current_time;

int main (void)
    printf("%s\n", getSystemTime());


The time format is important to note here:

"%02d:%02d:%02d:%04d", t->tm_hour, t->tm_min, t->tm_sec, tp.tv_usec/1000

This essentially translates to the following time format:

12:51:12:0491 | | | |-> Milliseconds (4 digits) | | |----> Seconds (2 digits) | |-------> Minutes (2 digits) |----------> Hours (2 digits)

For more info the time.h library in C, check out the links below:
GNU: Elapsed Time C Library time.h C Library struct tm


Java Swing: Auto-Scrolling JScrollPane (i.e. Chat Window)

This is just a short tutorial on how to make an auto scrolling pane in Java Swing. I was doing a chat client not too long ago, so I figured I’d assemble this short Swing tutorial for those interested in this particular behavior. This uses a JTextArea and a JScrollPane. I commented most of the lines, but I invite you to refer to the Java API for methods you don’t fully understand. Feel free to ask questions or suggest improvements in the comments section! Click here for the code.

Auto-Scrolling JTextArea/JScrollPane

Read more…Java Swing: Auto-Scrolling JScrollPane (i.e. Chat Window)