Skip to content

Instantly share code, notes, and snippets.

View openrijal's full-sized avatar
🇳🇵
<code />

Nitesh Rijal openrijal

🇳🇵
<code />
View GitHub Profile
@openrijal
openrijal / ip2geo.sh
Created April 3, 2013 18:33
Bash Script to find Geolocation from IP Addresses in apache's access log. You need to have "geoiplookup" package from GeoLite package. The excerpts are taken from http://danielmiessler.com/blog/a-simple-script-for-harvesting-dns-country-state-and-city-information-from-a-list-of-ip-addresses
#!/usr/bin/env bash
cat /var/log/apache2/access_log | awk '{print $1}' > ips.txt
uniq ips.txt > uniqips.txt
IPS=`cat uniqips.txt`
for i in $IPS
do
echo "$i,`geoiplookup $i | cut -d "," -f2 | sed -e 's/^[\t]//'`" >> ipinfo.csv
done
@openrijal
openrijal / rockpaperscissors.js
Last active December 16, 2015 01:08
Rock,Paper, Scissors GAME
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice <= (1/3)) {
computerChoice = "rock";
} else if(computerChoice <= (2/3)) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
compare(userChoice,computerChoice);
@openrijal
openrijal / media_to_uri.java
Last active April 21, 2021 03:26
Get Path from URI in Android.
/*
Input: URI -- something like content://com.example.app.provider/table2/dataset1
Output: PATH -- something like /sdcard/DCIM/123242-image.jpg
*/
public String convertMediaUriToPath(Uri uri) {
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(column_index);
@openrijal
openrijal / string_to_md5.java
Created May 1, 2013 12:17
This method converts any String to MD5 encrypted Strings.
/*
Input: STRING -- something like "myplainpassword"
Output: MD5 STRING -- something like "79054025255fb1a26e4bc422aef54eb4"
*/
public static String convertToMd5(final String inputString) throws UnsupportedEncodingException {
StringBuffer sb = new StringBuffer();
try {
final java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
final byte[] array = md.digest(md5.getBytes("UTF-8"));
for (int i = 0; i < array.length; ++i) {
@openrijal
openrijal / copy_of_range.java
Created May 1, 2013 12:21
Implementation of Arrays.copyOfRange() in terms of System.arraycopy() for compatibility in older Android versions.
public byte[] copyOfRange(byte[] from, int start, int end){
int length = end - start;
byte[] result = new byte[length];
System.arraycopy(from, start, result, 0, length);
return result;
}
@openrijal
openrijal / PasswordValidator.java
Created May 1, 2013 12:24
Java Class for verifying minimum password standard.
public class PasswordValidator {
// For character determinations
private static final int CHAR_LOWER_A = 'a';
private static final int CHAR_LOWER_Z = 'z';
private static final int CHAR_UPPER_A = 'A';
private static final int CHAR_UPPER_Z = 'Z';
private static final int CHAR_NUMERIC_ZERO = '0';
private static final int CHAR_NUMERIC_NINE = '9';
@openrijal
openrijal / gist:8120686
Created December 25, 2013 06:30
BASE_URL for Django from A BASE_URL Template Variable in Django http://www.micahcarrick.com/base-url-in-django.html via @MicahCarrick
context_processors.py
def baseurl(request):
"""
Return a BASE_URL template context for the current request.
"""
if request.is_secure():
scheme = 'https://'
else:
scheme = 'http://'
@openrijal
openrijal / webhost_bkup
Created February 8, 2014 15:22
This bash script is to be run from local machine and it copies remote web files and database to local folder.
# Assumptions
#########################
# remote_host: example.com
# remote_user: johndoe
# db_user: mydbuser
# db_pass: mydbpass
# db_name: mydbname
# to dump mysql database in the remote system from local system
ssh johndoe@example.com 'mysqldump -u mydbuser --password=mydbpass mydbname > /destination/path/dump_filename.sql'
@openrijal
openrijal / pgsql specific
Created July 7, 2015 13:08
Django Related
# to import data from csv to table
\copy <table_name> from '/path/to/csv/filename.csv' DELIMITERS ',' CSV;
# to increase the auto_increment after import
ALTER SEQUENCE tblName_id_seq RESTART WITH <number>;
@openrijal
openrijal / get_prev_next_items_iterable.py
Last active August 29, 2015 14:26
Get Previous and Next items in a forloop in Python
'''
source: http://stackoverflow.com/a/1012089/1777024
'''
from itertools import tee, islice, chain, izip # official documentation https://docs.python.org/2/library/itertools.html
def prev_and_next(my_iterable):
prevs, items, nexts = tee(my_iterable, 3)
prevs = chain([None], prevs)
nexts = chain(islice(nexts, 1, None), [None])