Archive
Tags
android (3)
ant (2)
beautifulsoup (1)
debian (1)
decorators (1)
django (9)
dovecot (1)
encryption (1)
fix (4)
gotcha (2)
hobo (1)
htmlparser (1)
imaplib (2)
java (1)
json (2)
kerberos (2)
linux (7)
lxml (5)
markdown (4)
mechanize (6)
multiprocessing (1)
mysql (2)
nagios (2)
new_features (3)
open_source (5)
optparse (2)
parsing (1)
perl (2)
postgres (1)
preseed (1)
pxe (4)
pyqt4 (1)
python (41)
raid (1)
rails (1)
red_hat (1)
reportlab (4)
request_tracker (2)
rt (2)
ruby (1)
scala (1)
screen_scraping (7)
shell_scripting (8)
soap (1)
solaris (3)
sql (2)
sqlalchemy (2)
tips_and_tricks (1)
twitter (2)
ubuntu (1)
vmware (2)
windows (1)
zimbra (2)

I have moved the code repository to Google Code. In addition, the latest version respects youtube's URL get values, like hd=1.

Grab a clone like so:

hg clone https://python-markdown-video.googlecode.com/hg/ python-markdown-video

This is only the version compatible with python markdown 2.0. The version for earlier versions of python markdown is now deprecated and will not be maintained.

Posted by Tyler Lesmann on October 20, 2009 at 9:33
Tagged as: markdown open_source python

Waylan Limberg tells me python-markdown will be releasing version 2.0 at the end of this week. This release breaks my extension and all others written for 1.7. My extension obsoleted within a less than a week of release. That would not do, so I have updated the code to work on 2.0! It's still under LGPL.

You can get it here or clone the Mercurial repository like so:

hg clone http://code.tylerlesmann.com/mdx_video2

Use and install are exactly the same as with the earlier version. Adding new services has changed and I will get to documenting that soon.

Posted by Tyler Lesmann on April 2, 2009 at 15:20 and commented on 6 times
Tagged as: django markdown open_source python

I'm a big fan of markdown, especially the fact that it can be extended so easily. I wanted to give the users of DeathCat Inc. the ability to embed video from popular services in their posts with only an URL, so I wrote up a new extension. To see it in action, go here. This code is licensed under LGPL.

You can get it here.

It is installable as many other python modules...

python setup.py install

..., but mdx_video.py only has to be in your PYTHON_PATH. With Django, for instance, you can place it in the same directory as settings.py.

Using my extension is like using any other extension for markdown.

>>> import markdown
>>> s = "http://www.youtube.com/watch?v=F8qwxzQar2g"
>>> markdown.markdown(s, ['video'], safe_mode='escape')

Remember to set safe_mode to escape if you are passing untrusted users' input through markdown, extension or not. This extension supports arguments for setting the dimension of the video. By default, the dimensions are what the specific service gives for their example embed. If you don't like this size, then you can change it like so:

>>> markdown.markdown(s, ['video(youtube_width=720, youtube_height=400)'], safe_mode='escape')

If you want to integrate markdown and this extension with Django, then I recommend looking at this post.

This extension supports the following services:

  • Blip.tv
  • Dailymotion
  • Metacafe
  • Veoh
  • Vimeo
  • Yahoo! video
  • Youtube

NOTE: Blip.tv works a little differently than the others because there is no way to construct a working object with the player URL. Instead of the URL to the Blip.tv page, you will use the URL to the flv file, like http://blip.tv/file/get/Pycon-DjangoOnJython531.flv for example. This is located in Files and Links section of Blip.tv.

Adding extra services is easy. Note: This portion is relevant to the extension for python-markdown 1.7. The first part is defining what URL for the video service should look like. You do this in the extendMarkdown method of VideoExtension.

# This regular expression looks for a youtube URL that do not start with parenthesis.
# It does this to avoid eating regular markdown links.
YOUTUBE_RE = r'([^(]|^)http://www\.youtube\.com/watch\?\S*v=(?P<youtubeid>[A-Za-z0-9_-]+)\S*'
# Here we plug our expression into the bit that builds the video embed html.
# We define this shortly.
YOUTUBE_PATTERN = Youtube(YOUTUBE_RE)
# The next two lines allow control of markdown through instances of this class.
YOUTUBE_PATTERN.md = md
YOUTUBE_PATTERN.ext = self
# This registers everything with markdown, so the code will be executed.
md.inlinePatterns.append(YOUTUBE_PATTERN)

Next, we get to build the HTML. We need to add a new subclass of markdown.BasePattern to the module.

class Youtube(markdown.BasePattern):
    def handleMatch(self, m, doc):
        url = 'http://www.youtube.com/v/%s' % m.group('youtubeid')
        width = self.ext.getConfig('youtube_width')
        height = self.ext.getConfig('youtube_height')
        return FlashObject(doc, url, width, height)

For the most part, building the HTML is easy. I have defined a flash_object function that builds an object element that work in most cases. You only need to feed it your minidom instance, doc, an url, and width/height, both as strings. You will notice that I am using self.ext.getConfig to assign my width and height. These are the extension arguments. You will want to use these too. To do so, add a new key to the self.config dictionary of __init__ in VideoExtension.

'youtube_width': ['640', 'Width for Youtube videos'],
'youtube_height': ['385', 'Height for Youtube videos'],

The first part is the default value and the second bit is a description of the argument. If you need to define more HTML, then I suggest taking look at the Yahoo class.

Posted by Tyler Lesmann on April 2, 2009 at 8:16 and commented on 10 times
Tagged as: django markdown open_source python

There is this really nice plugin for WordPress called sociable that makes it easy to make links to various social bookmarking sites. There did not seem to be anything similar in Django, so I made a clone. It does not do everything that the sociable plugin does yet, but it is incredibly flexible. This code is licensed under LGPL.

You can get the latest version here or you can get the whole repository with mercurial:

hg clone http://code.tylerlesmann.com/django-sociable

You will probably want the sociable images also. You can get them from here.

Install is like many other python packages:

python setup.py install

Using django-sociable is easy. After install, edit your settings.py. You will need to specify a MEDIA_URL, if you have not already. Now add SOCIABLE_IMAGE_PATH, which is the path relative to the MEDIA_URL to find the sociable images. The last thing to add is put sociable into your INSTALLED_APPS.

The next step is defining a Site because django-sociable uses this to construct your submission link. You do not want it pointing to example.com. The easiest way to do this is from the Django admin site.

You are ready to use the django-sociable templatetags in your templates. Here's an example of its use from this very blog:

<div>
{% load sociable_tags %}
{% get_sociable Reddit Digg as sociable_links with url=object.get_absolute_url title=object.headline %}
{% for link in sociable_links %}
    <a href="{{ link.link }}">
        <img alt="{{ link.site }}" title="{{ link.site }}" src="{{ link.image }}" />
    </a>
{% endfor %}
</div>

Load sociable_tags. Use the get_sociable tag to get the information you need to build the links. Specify the sites to target, then the variable to map the information to, and finally the arguments. The only required argument is url, which should be a context variable containing link to submit. You can also submit title, excerpt, and source. Use the list of information from get_sociable in whatever way you like. Each element of the list has three attributes. The site attribute is the name of the target site. The link contains the full submission link. The image is the absolute path to the image corresponding to the site.

Here is a full list of the acceptable target sites:

BarraPunto blinkbits BlinkList BlogMemes blogmarks Blogsvine blogtercimlap Faves Book.mark.hu Bumpzee co.mments connotea del.icio.us De.lirio.us Digg DotNetKicks DZone eKudos email Facebook Fark feedmelinks Furl Fleck GeenRedactie Google Gwar Haohao HealthRanker Hemidemi IndianPad Internetmedia kick.ie Kirtsy laaik.it Leonaut LinkArena LinkaGoGo LinkedIn Linkter Live Ma.gnolia Meneame MisterWong MisterWong.DE Mixx muti MyShare MySpace N4G Netvibes NewsVine Netvouz NuJIJ Ping.fm PlugIM Pownce ppnow Print Propeller Ratimarks Rec6 Reddit SalesMarks Scoopeo scuttle Segnalo Shadows Simpy Slashdot Smarking Socialogs Spurl SphereIt Sphinn StumbleUpon Symbaloo Taggly Technorati TailRank ThisNext Tipd Tumblr TwitThis Upnews Webnews.de Webride Wikio Wists Wykop Xerpi YahooBuzz YahooMyWeb YCombinator Yigg

Posted by Tyler Lesmann on March 9, 2009 at 18:16 and commented on 9 times
Tagged as: django new_features open_source python

I am releasing my package for accessing Authorize.net's CIM API under the LGPL license.

It requires lxml to function.

You can download pyauthdotnet here or you can clone the repository with this command, assuming you have mercurial installed:

hg clone http://code.tylerlesmann.com/pyauthdotnet

You will install it like many other packages:

python setup.py install

This package is not well documented as of yet, though it is written to coincide with Authorize.net's documentation. Here is a quick example of its use:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#!/usr/bin/env python
from pyauthdotnet import cim
from copy import deepcopy
from lxml import etree

login = 'Your_API_login'
key = 'Your_API_key'

def printxml(xml):
    print(etree.tostring(xml, pretty_print=True, encoding='UTF-8',
        xml_declaration=True))

billTo = cim.constructAddress('billTo',
    firstName='John', lastName='Doe',
    company='Superhappy testing', address='123 Fake Street', city='Aurora',
    state='CO', zip='80014', country='USA', phoneNumber='(303)555-5555',
    faxNumber='(303)666-6666')
printxml(billTo)

ba = cim.constructBankAccount('011000015', '123123123', 'John Doe',
    'checking', 'CCD', 'Some Bank')
printxml(ba)

cc = cim.constructCreditCard('4007000000027', '2010-03', '123')
printxml(cc)

pp1 = cim.constructPaymentProfiles(customerType='individual',
    billTo=deepcopy(billTo), bankAccount=ba)
printxml(pp1)
pp2 = cim.constructPaymentProfiles(customerType='business',
    billTo=deepcopy(billTo), creditCard=cc)
printxml(pp2)

stl = deepcopy(billTo)
stl.tag = 'shipToList'
stl2 = deepcopy(stl)
newprof = cim.createCustomerProfileRequest(login, key,
    merchantCustomerId='1235', description='test1235', email='not2@valid.email',
    paymentProfiles=[pp2], shipToList=[stl, stl2], validationMode='liveMode')
printxml(newprof)
custid = newprof.customerProfileId

prof = cim.getCustomerProfileRequest(login, key, custid)
printxml(prof)

ppid = prof.profile.paymentProfiles.customerPaymentProfileId.text
said = prof.profile.shipToList.customerAddressId.text

tax = cim.constructAmount('tax', amount='1.00', name='tax',
    description='mininature american flags tax')

shipping = cim.constructAmount('shipping', amount='3.00',
    name='Fedex Express Saver')

duty = cim.constructAmount('duty', amount='5.00')

li1 = cim.constructLineItem('123F12', name='widget',
    description='Used with whatsits', quantity='5', unitPrice='5.00',
    taxable='true')

li2 = cim.constructLineItem('123F13', name='whatsit',
    description='Used with widgets', quantity='5', unitPrice='5.00',
    taxable='false')

trans = cim.createCustomerProfileTransactionRequest(login, key,
    '59.00', custid, ppid, refId='123', tax=tax, shipping=shipping, duty=duty,
    lineItems=[li1, li2], customerShippingAddressId=said,
    invoiceNumber='20090211', description='widget/whatsit purchase',
    purchaseOrderNumber='20090211123', taxExempt='false',
    recurringBilling='false', cardCode='123',
    extraOptions='x_customer_ip=100.0.0.1')
printxml(trans)

cc2 = cim.constructCreditCard('XXXX0002', '2013-12', '789')
pp3 = cim.constructPaymentProfile(
    customerType='individual', billTo=deepcopy(billTo), creditCard=cc2,
    customerPaymentProfileId=ppid)
ppr = cim.updateCustomerPaymentProfileRequest(login, key, custid, pp3,
    refId='123', validationMode='liveMode')
printxml(ppr)

address = cim.constructAddress('address',
    firstName='John', lastName='Doe',
    company='Superhappy testing', address='123 Fake Street', city='Aurora',
    state='CO', zip='80014', country='USA', phoneNumber='(303)555-5555',
    faxNumber='(303)666-6666', customerAddressId=said)
csa = cim.updateCustomerShippingAddressRequest(login, key, custid,
    address, refId='6')
printxml(csa)

profids = cim.getCustomerProfileIdsRequest(login, key)
printxml(profids)

valid = cim.validateCustomerPaymentProfileRequest(login, key,
    custid, ppid, customerShippingAddressId=said, cardCode='123',
    validationMode='liveMode')
printxml(valid)

up = cim.updateCustomerProfileRequest(login, key, custid,
    merchantCustomerId='whoa', description='man', email='no@no.no')
printxml(up)
Posted by Tyler Lesmann on February 25, 2009 at 13:02 and commented on 3 times
Tagged as: open_source python