Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS 

yn.rb

// Ruby Yubnub launcher

#!/usr/bin/env ruby

# Andrew Pennebaker (andrew.pennebaker@gmail.com)
# 7 Aug 2007
# Copyright 2007 Andrew Pennebaker
# License: GPL
# URL: http://snippets.dzone.com/posts/show/4400

parser="http://yubnub.org/parser/parse?command="

command=ARGV.join("+")

system "open "+parser+command

yubnub.py

#!/usr/bin/env python

__author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"
__date__="9 Dec 2006 - 10 Dec 2006"
__copyright__="Copyright 2006 Andrew Pennebaker"
__license__="GPL"
__version__="0.0.1"
__credits__="Based on Yubnub for Windows (http://www.opbarnes.com/blog/Programming/OPB/Utilities/yubnub.html)"
__URL__="http://snippets.dzone.com/posts/show/3120"

from html2txt import html2txt

import webbrowser
from urllib import urlopen
import re

import sys
from getopt import getopt

PARSER="http://yubnub.org/parser/parse?command="

BROWSER_MODE="BROWSER"
PLAIN_MODE="PLAIN"

def space2plus(s):
	return "+".join(s.split())

def yubnub(command=""):
	global PARSER

	return PARSER+space2plus(command)

def yubnubBrowser(command):
	return webbrowser.open(yubnub(command))

def cleanHTML(html):
	h=html2txt()
	h.feed(html)
	h.close()

	return h.output()

def yubnubPlain(command, clean=True):
	command=yubnub(command)

	try:
		url=urlopen(command)
		lines=url.readlines()
		url.close()

		lines="".join(lines)

		if clean:
			return cleanHTML(lines)

		return lines

	except IOError, e:
		return "Error connecting to "+command

def usage():
	print "Usage: "+sys.argv[0]+" [options] <command>"
	print "-b --browser"
	print "\n--plain (default)"
	print "\t-c --clean (default)"
	print "\t-d --dirty"
	print "\n--parser <parser> (experimental)"
	print "\n-h --help"

	sys.exit()

def main():
	global PARSER

	global BROWSER_MODE
	global PLAIN_MODE

	mode=PLAIN_MODE
	parser=PARSER
	clean=True

	systemArgs=sys.argv[1:]
	optlist, args=[], []
	try:
		optlist, args=getopt(systemArgs, "bhcd", ["browser", "plain", "clean", "dirty", "parser=", "help"])
	except:
		usage()

	for option, value in optlist:
		if option=="-h" or option=="--help":
			usage()

		elif option=="-b" or option=="--browser":
			mode=BROWSER_MODE
		elif option=="--plain":
			mode=PLAIN_MODE
		elif option=="-c" or option=="--clean":
			clean=True
		elif option=="-d" or option=="--dirty":
			clean=False
		elif option=="--parser":
			parser=value

	command=" ".join(args)

	if mode==BROWSER_MODE:
		yubnubBrowser(command)
	elif mode==PLAIN_MODE:
		for line in yubnubPlain(command, clean):
			sys.stdout.write(line)
		print ""

if __name__=="__main__":
	main()

YubNub Bookmarklet

// description of your code here
This works with the site yubnub.org. First highlight some text, then click the bookmarklet. A dialogue box pops up to ask the YubNub command to be used. It then performs that command on the highlighted text.
javascript:q = "" + (window.getSelection ? window.getSelection() : document.getSelection ? document.getSelection() : document.selection.createRange().text); p = prompt("Yubnub search type? ... ", ""); if (q!=null) location="http://yubnub.org/parser/parse?command=" + escape(p).replace(/ /g, "+") + " " + escape(q).replace(/ /g, "+"); void 0

Yubnub "echoshortcut" to display text stored for alias

<?php

if (!$link = mysql_connect('', '', '')) {
echo 'Could not connect to mysql';
exit;
}

if (!mysql_select_db('yub', $link)) {
echo 'Could not select database';
exit;
}
$sendto1 = mysql_query("select email from yubmail where mail_alias = '$path'");
$sendto2 = mysql_result($sendto1, 0);

if (!$sendto2) {
echo "The alias does not exist! Use the command shortcut to create one.";
exit;
}
echo "$sendto2";
?>

Yubnub "checkshortcut" command to check alias

<?php

if (!$link = mysql_connect('', '', '')) {
   echo 'Could not connect to mysql';
   exit;
}

if (!mysql_select_db('yub', $link)) {
   echo 'Could not select database';
   exit;
}
$sendto1 = mysql_query("select email from yubmail where mail_alias = '$path'");
$sendto2 = mysql_result($sendto1, 0);

if (!$sendto2) {
   echo "The alias does not exist! You can try to create one or send the message to mailinator ailas.";
exit;
}

echo "The alias $path does exist!";
?>

Yubnub "dbsearch" command to search MySQL data

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=
<?php
$compare1 = substr($path, 0, 7);
if ($compare1 == "http://"){
echo $path.'db_search.php?search_option=1&submit_search=Go&db='.$db.'&search_str='.$search;
}

if (!$link = mysql_connect('', '', '')) {
   echo 'Could not connect to mysql';
   exit;
}

if (!mysql_select_db('yub', $link)) {
   echo 'Could not select database';
   exit;
}
$sendto1 = mysql_query("select email from yubmail where mail_alias = '$path'");
$sendto2 = mysql_result($sendto1, 0);
echo $sendto2.'db_search.php?search_option=1&submit_search=Go&db='.$db.'&search_str='.$search;
?>
">

Yubnub "data" command to display MySQL data

<?php
// Yubnub.org command "data" access this show.php file on the server
      
//    $dbname = "";
//    $loginname = "";
//    $loginpass = "";
//    $dbhost = "";
    
    echo('<html><body bgcolor="#FFFFFF">');
    echo('<font face="arial" size="+4"><center>');
    echo(" $dbname");
    
    $id_link = @mysql_connect($dbhost, $login, $password);
    
    $tables = mysql_list_tables($dbname, $id_link);
    
    $num_tables = mysql_num_rows($tables);

    // store table names in an array
    $arr_tablenames[] = '';
    
    // store number of fields per table(index 0,1,2..) in an array
    $arr_num_fields[] = '';
    for ($i=0; $i < $num_tables; $i++) {
        $arr_tablenames[$i] = mysql_tablename($tables, $i);
        $arr_num_fields[$i] = mysql_num_fields(mysql_db_query($dbname, "select * from $arr_tablenames[$i]", $id_link));
    }
    
    // store field names in a multidimensional array:
    // [i] == table number, [ii] == field number for that table
    for ($i=0; $i < $num_tables; $i++) {
        for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
            $result = mysql_db_query($dbname, "select * from $arr_tablenames[$i]", $id_link);
            $hash_field_names[$i][$ii] = mysql_field_name($result, $ii);
        }    
    }
    
    for ($i=0; $i < $num_tables; $i++) {
        echo("<center><h2> $arr_tablenames[$i] </h2></center>");
        echo('<table align="center" border="1"><tr>');
        $result = mysql_db_query($dbname, "select * from $arr_tablenames[$i]", $id_link);
        for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
            echo("<th>");
            echo $hash_field_names[$i][$ii];
            echo("</th>");
        }
        echo("</tr><tr>");
        $number_of_rows = @mysql_num_rows($result);
        for ($iii = 0; $iii < $number_of_rows; $iii++) {
            $record = @mysql_fetch_row($result);
            for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
                echo("<td>");
                // echo $record[$ii];
                $mytext = nl2br($record[$ii]);
                echo $mytext;
                echo("</td>");
            }
        echo("</tr>");
        }
        echo("</table>");
    }
    


    echo('</body></html>');

?>


Yubnub "shortcut" command source

<?php

// CREATE TABLE `yubmail` (
// `id` int(11) NOT NULL auto_increment,
// `mail_alias` varchar(250) NOT NULL default '',
// `email` varchar(250) NOT NULL default '',
// `modifed1` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
// `paid` enum('Y','N') default 'N',
// PRIMARY KEY (`id`),
// UNIQUE KEY `mail_alias` (`mail_alias`)
// ) ENGINE=MyISAM


 $this = ' ';
   function after ($this, $alias)
   {
       if (!is_bool(strpos($alias, $this)))
       return substr($alias, strpos($alias,$this)+strlen($this));
   };

   function before ($this, $alias)
   {
       return substr($alias, 0, strpos($alias, $this));
   };
$mail_alias = before ($this, $alias);
$email = after ($this, $alias); 

if (!$link = mysql_connect('mysql', 'username', 'password')) {
   echo 'Could not connect to mysql';
   exit;
}

if (!mysql_select_db('yub', $link)) {
   echo 'Could not select database';
   exit;
}

$sql_query = mysql_query("INSERT INTO yubmail(mail_alias, email) VALUES ('$mail_alias', '$email')") ;

if (!$sql_query) {
   die('Try again with different shortcut');
}
echo "alias created called <b><i>$mail_alias</i></b> directing to <b>$email</b>";
?> 

« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS