CopyTo & MoveTo

Attribute Value Templates

An attribute value template is an XPath expression enclosed in curly braces that's placed in the attribute value in the stylesheet.

<xsl:template match="name">
  <name first="{first_name}"
        initial="{middle_initial}"
        last="{last_name}" />
</xsl:template>

Miscellaneous

MySQL Cheat Sheet

mysql> SET AUTOCOMMIT=1; # For quick recreation of table
mysql> LOAD DATA LOCAL INFILE "infile.txt" INTO TABLE table_name;

sysctl

"MediaUploader" Project

Uploading sounds for every item can be extremely tedious. So I put together a utility script to make my life easier. I spent some time on the smart.fm API and found it just a waste of time. First of all, it's buggy; Secondly it lacks important features like "edit item" and "attach sound".

curl 'http://api.smart.fm/items/1729944/sounds'
-F api_key=wzxhft8cnb25b5c3we33qt63 -F 'sound[file]=@apple.mp3;type=audio/mpeg' -u lemontr2:PeAsw6cH --trace-ascii -

Notes:
  1. You have to use -F here for "multipart/form-data"; "-d (application/x-www-form-urlencoded)" will not work.
  2. If you want to use "sound[file]", you need to specify the type of the file, which is "audio/mpeg". And notice that you cannot specify two "sound[...]" parameters otherwise you will get a strange error message. Also "author" parameter doesn't work with "sound[file]".
  3. This API will not attach your newly uploaded sound to that item; you still need to set the new sound to be the current sound manually.

"MediaUploader" Project II

...Then I turned to the ajax API used by smart.fm's web interface. Kind of a hack but it works.

curl "http://smart.fm/my_sounds/upload.js" -F
"sound_attachment=@apple.mp3;type=audio/mpeg" --trace-ascii - -c
newcookies.txt -H 'Cookie:
__utma=213639243.3007978433092454400.1250429551.1256908869.1256913311.17;
__utmz=213639243.1256916224.17.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=%20http%3A//api.smart.fm/sounds;
__utmv=213639243.user%3Aen;
csid2=3e120e46bf3072296045d20fa85f5c51fvug;
csid=9cbc8bb09ef1012c029400163e60a6bb;
_smartid=BAh7DDoSbXlfbGlzdHNfbW9kZSIMY3JlYXRlZDoMdXNlcl9pZGkD0P8HOhBvcGVuaWRfc
HJvZnsIIg1uaWNrbmFtZTAiK2h0dHA6Ly9zY2hlbWEub3BlbmlkLm5ldC9jb250YWN0L2VtYWlsWwY
iHWxlbW9udHJlZS5jb29sQGdtYWlsLmNvbSIKZW1haWxACjoPb3BlbmlkX3VybCJVaHR0cHM6Ly93d
3cuZ29vZ2xlLmNvbS9hY2NvdW50cy9vOC9pZD9pZD1BSXRPYXdrSFp6YjBfdXFtMGtpalRKUk1PV1k
wQ3NCTTBXSW4zb3M6D3Nlc3Npb25faWQiJTUxMGYxODAxYTI3ZmVmNzhkZmY3Yjk3YmY0NTQ0NDU4O
hJsYW5ndWFnZV9jb2RlIgdlbjoIdWxtIgYx--a274213360cf0766fba2ac9ba54711b65c1a90d6;
language_code=en; timezone_offset=-480; __utmc=213639243;
__utmb=213639243.57.10.1256913311; ListBuilder.lastAddedType=text'

In this case you need to provide your cookies after logged into smart.fm. And the upload.js API only uploads the sound file to smart.fm; you still need to attach that sound to your item.

Here is the corresponding perl code:
 my $r = POST $url,
   Content_Type => 'form-data',
   Cookie => $cookies,
   Content => [ sound_attachment =>
                 [ $path, $file, Content_Type => 'audio/mpeg' ]];

The only thing we need in the response can be captured using the following regex:
 my $html = $resp->decoded_content;
 if ($html =~ m{add_user_sound\(\\"(.+?)\\", (\d+?)\)}) {
   my ($url,$sid) = ($1,$2);
   printf "%s,%s,%s\n", $sid, $file, $url;
 }

Now let's attach the sound to the item:
 my $url = "http://smart.fm/lists/$lid/items/$iid/attach_sound?context=item&row_id=row_$iid";
 my $r = POST $url,
   Cookie => $cookies,
   'X-Requested-With' => 'XMLHttpRequest',
   'X-Prototype-Version' => '1.6.0.2',
   Content => [
     id          => $iid,
     class       => 'Item',
     commit      => 'attach sound',
     language_id => 1819,
     sound_id    => $sid,
     url         => '',
   ];
Two things to note here: 'X-Requested-With' and 'X-Prototype-Version'. If you forget to include these two header parameters, you will get a 500 error.


This topic: Sandbox > UserShjiang
Topic revision: 12 Feb 2011, AlexJiang
The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. see CopyrightStatement. Creative Commons LicenseGet Foswiki at sourceforge.net. Fast, secure and Free Open Source software downloads