GeniusOfEvil’s Blog

It is a sin to believe evil of others, but it is seldom a mistake.

Archive for the ‘Programing’ Category

Why complexity matters

leave a comment »

I needed a simple queue class – so  I wrote one based on java.util.Queue interface

**
 * Queue Template Class
 * @author: Jarek Przygódzki <jarek.przygodzki@gmail.com>
 * @version 0.1
**/
class Queue(E) {
	this() {

	}
	bool isEmpty() {
		return length is 0 ;
	}

	/**
	 * Retrieves and removes the head of this queue
	 */
	E poll(){
		auto e = elements[$-1];
		elements = elements[0..$-1];
		return e;
	}

	/**
	 * Retrieves, but does not remove, the head of this queue
	 */
	E peek() {
		return elements[$-1];
	}
	/**
	 * Inserts the specified element into this queue
	 */
	Queue!(E) offer(E e) {
		elements ~= e;
		return this;
	}
	/**
	 * Number of elements in queue
	 */
	uint length() {
		return elements.length;
	}

	int opApply(int delegate(ref E e) dg) {
		foreach(e;elements) dg(e);
		return 0;
	}
	private E[] elements;
}

Of course it totally screwed up Edmonds-Karp algorithm complexity. But it worked ;D

Written by Jarek Przygódzki

June 23, 2009 at 11:19 am

Posted in D, Geek, Programing

Tagged with , ,

Howto Get Direct YouTube Video (FLV) URL

with 7 comments

This post is based on paulg’s description of  Youtube video url protocol . Time is precious, so let’s get straight to the point – here’s the Python implementation

import httplib,urllib

video_id = 'PB8pWjMlxxE'
eurl = 'https://geniusofevil.wordpress.com/' #
params = urllib.urlencode({'video_id':video_id, 'eurl':eurl})
conn = httplib.HTTPConnection("www.youtube.com")
conn.request("GET","/get_video_info?&%s"%params)
response = conn.getresponse()
status,reason = response.status, response.reason
data = response.read()
video_info = dict((k,urllib.unquote_plus(v)) for k,v in
                               (nvp.split('=') for nvp in data.split('&')))

Now video_info is a dictionary containing a lot of information about video ( author, length,title, etc) – but we still don’t have direct video URL.

conn.request('GET','/get_video?video_id=%s&t=%s' %
                 ( video_info['video_id'],video_info['token']))
response = conn.getresponse()

YouTube will respond with HTTP 303 See Other

direct_url = response.getheader('location')

Final Solution

# #!/usr/bin/env python  
# # -*- coding: utf-8 -*-

import httplib,urllib
__author__ = 'Jarosław Przygódzki'
__copyright__ = 'Copyright (c) 2009 Jarosław Przygódzki'
__date__ = '30.04.2009'
__license__ = 'GPL'
__version__ = '0.1.1'

def GetYoutubeVideoInfo(videoID,eurl=None):
	'''
	Return direct URL to video and dictionary containing additional info
	>> url,info = GetYoutubeVideoInfo("tmFbteHdiSw")
	>>
	'''
	if not eurl:
		params = urllib.urlencode({'video_id':videoID})
	else :
		params = urllib.urlencode({'video_id':videoID, 'eurl':eurl})
	conn = httplib.HTTPConnection("www.youtube.com")
	conn.request("GET","/get_video_info?&%s"%params)
	response = conn.getresponse()
	data = response.read()
	video_info = dict((k,urllib.unquote_plus(v)) for k,v in 
                               (nvp.split('=') for nvp in data.split('&')))
	conn.request('GET','/get_video?video_id=%s&t=%s' %
                         ( video_info['video_id'],video_info['token']))  
	response = conn.getresponse()
	direct_url = response.getheader('location')
	return direct_url,video_info 

Written by Jarek Przygódzki

April 30, 2009 at 12:07 pm

Posted in Programing, Python

Exception handling in constructor’s initialization list

There are some aspects of C++ syntax that still amaze me… The syntax for catching an exception in an initialization list is one of them.It’s totally awkward

class MyClass {
public:
     MyClass()
     try : <initialization list>
     { }
     catch ( ... ) { 
     }
};

But even if you catch the exception, you are pretty much screw because it cannot be guaranteed that object being created is in a valid state.There’s nothing much you can do besides rethrowing the exception.

Written by Jarek Przygódzki

April 1, 2009 at 2:43 pm

Posted in C++, Cpp, Programing

Tagged with , ,

Mixing stdio with iostream

leave a comment »

When mixing C stdio and C++ iostream synchronization problems can occur because the two libraries use different buffering strategies. This seems a major drawback since mixing C++ code with C libraries (that uses stdio) is a very common practice. Luckily, all we have to do to avoid these problems is to call function

ios::sync_with_stdio()

Written by Jarek Przygódzki

March 26, 2009 at 9:43 am

Posted in C++, Cpp, Programing

Tagged with ,