RSS
 

Archive for the ‘工作’ Category

Summary of Last Week’s Work

12 Sep

1. When I use FCKeditor and use its ‘upload image’ function, it just reset all of the explorer’s session values in the process! I have to find out how this plugin acts, and figure out a solution.

2. I am just rewrite all the lib files of FCKeditor’s php demo into rails ones. It is just not completed.

3. The output of decoding for string of store information in the section of sending short message to customer is wrong. The possible cause is the incorrect database encoding. I now leave the problem for Mr. He. He will try to swift the encoding to GBK instead of Utf-8. If it works… it’s really lucky! Or I will not know what to do.

 
No Comments

Posted in 工作

 

用ruby写了转txt编码的程序。gbk-》utf8

12 Sep
#!/usr/bin/env ruby
#require 'rubygems'
#require 'active_record'
require 'iconv'

input_path = 'input/'
output_path = 'output/'
if !File.directory? input_path
  path = File.expand_path(input_path)
  Dir.mkdir(input_path)
end
if !File.directory? output_path
  path = File.expand_path(output_path)
  Dir.mkdir(output_path)
end

docs = Dir.glob(input_path + '*')
#puts docs.inspect
for doc in docs do
  puts doc
  if File.file? doc
    puts 'in here'
    report = ''
    report_utf8 = ''
    File.open( doc,'r') do |f|
      report = f.read
    end
    begin
      puts 'start!'

      #name = File.split(path)[1]
      filename = output_path + File.basename(doc)
      puts filename

      #begin
      #  conv = Iconv.new("GBK", "utf-8")
      #  report_utf8 = conv.iconv(report)
      #rescue
      #end
      begin
        conv = Iconv.new("utf-8", "GBK")
        report_utf8 = conv.iconv(report)
      rescue
        puts File.basename(doc) + ' error'
      end
      puts 'ok'

      if report_utf8 && report_utf8 != ''
        if File.file?(filename)
          File.delete(filename)
        end

        File.open( filename, 'w') do |f|
          f.write report_utf8
        end
      end
    rescue
    end
  end
end
 
No Comments

Posted in 工作

 

search and lookup app in store on web

29 May

I plan to help my colleague to develop a web application to collect the apps I want to buy or recommend.

The first step is to search and find the web page containing the app information.

Take the tool named SOUNDSNAP for example,

The first step is to search it in api and get return string.

The Search URL is:

http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsSearch?term=soundsnap&entity=software

Then you get the return string like:

{ ”resultCount”:33, ”results”: [{"version":"1.0", "kind":"software", "artistId":428212975, "artistName":"Black Arc Studios", "price":0.00, "description":"\u2605\u2605\u2605 Free for this weekend only! \u2605\u2605\u2605 \n\n\u2605\u2605\u2605 Featured on Gizmodo &
Lifehacker AU! \u2605\u2605\u2605 Let your voice do the photo taking!!! (Or any sound for that matter!)\n\nSoundSnap is a simple
camera app that lets you take pictures by detecting ANY sound you make!. Simply tap, speak or make any sound into the built-in
microphone and SoundSnap will quickly snap a picture!  Better yet make use of your earphone headset or Bluetooth headset as a
remote!\n\n\n************\nRemote Control\n************\n\n\u2022 SoundSnap allows you to use your device\u2019s built-in microphone
to pick up sound from a distance! Now there\u2019s even more reason to say \u201CCheese\u201D!\n\u2022 Use your earphone headset
\u2019s microphone as a remote release cable!\n\u2022 For those who need precise wireless control, take advantage of your Bluetooth
headset as wireless remote!\n\n\n************\nPerfect for a variety of situations\n************\n\n\u2022 Too cold to take off your
gloves? No problems, with SoundSnap you can still snap a photo using just your voice!\n\u2022 Take advantage of the high quality rear
camera for self-portraits! No more fiddling around trying to find where the button is.\n\u2022 Reduce accidental shaking of the
camera when taking photos by not having to touch the device at all! \n\u2022 Mount your iPhone/iPod on a tripod and use your earphone
headset\u2019s microphone as remote release cable! Perfect for great night shots!\n\u2022 Take the perfect group photo when no one is
around to assist. For even more precision and timing of a snap, use a Bluetooth headset as a wireless remote!\n\u2022 Need to be
discreet when taking a photo? No problems. STEALTH mode will black out the screen to make it seem as though the device is off!\n\n
\n************\nKey Features\n************\n\n\u2022 Tap, speak or make any sound to snap a photo!\n\u2022 Adjust the sensitivity of
the sound detection! - Optimize the app for quiet or noisy environments.\n\u2022 Use your headset as a remote cable release!\n\u2022
Use your Bluetooth headset as a wireless remote!\n\u2022 Timer Mode \u2013 Give yourself time to perfect your pose before taking that
snap!\n\u2022 Stealth mode \u2013 For those times when discretion is needed!\n\n\n************\nCompatibility\n************
\nSoundSnap is compatible with the iPhone 4, iPhone 3GS and iPod Touch (4th Generation).\nFor Apple\u2019s built-in tap-to-focus
function to be active, iOS 4.3 is required.\n\n\n************\nWebsite\n************\nhttp://www.blackarcstudios.com/soundsnap",
"genreIds":["6008", "6002"], “releaseDate”:”2011-04-07T07:00:00Z”, “sellerName”:”Black Arc Studios Private Limited”,
“currency”:”USD”, “trackId”:428212972, “trackName”:”SoundSnap”, “genres”:["Photography", "Utilities"], “releaseNotes”:”",
“primaryGenreName”:”Photography”, “primaryGenreId”:6008, “isGameCenterEnabled”:false, “supportedDevices”:["iPhone-3GS", "iPhone4",
"iPhone", "iPad23G", "iPad2Wifi", "iPodTouchourthGen", "iPhone-3G"], “wrapperType”:”software”,
“artworkUrl60″:”http://a1.mzstatic.com/us/r1000/013/Purple/eb/8f/9d/mzi.erfyikae.png”,
“artworkUrl100″:”http://a4.mzstatic.com/us/r1000/035/Purple/14/a7/7f/mzm.tqzjqmrh.png”,
“artistViewUrl”:”http://itunes.apple.com/us/artist/black-arc-studios/id428212975?uo=4″, “contentAdvisoryRating”:”4+”,
“trackCensoredName”:”SoundSnap”, “trackViewUrl”:“http://itunes.apple.com/us/app/soundsnap/id428212972?mt=8&uo=4“,
“languageCodesISO2A”:["DE", "EN", "ES", "FR", "IT", "JA", "KO", "ZH"], “fileSizeBytes”:”10089479″, ”screenshotUrls”:["http://a1.mzstatic.com/us/r1000/027/Purple/68/c9/5b/mzl.ctnqyntv.png",
"http://a2.mzstatic.com/us/r1000/050/Purple/3c/93/2d/mzl.qfsdkame.png",
"http://a2.mzstatic.com/us/r1000/029/Purple/37/cf/95/mzl.vugmtqaz.png"], “ipadScreenshotUrls”:[],
“sellerUrl”:”http://www.blackarcstudios.com/soundsnap”, “averageUserRatingForCurrentVersion”:5.0,
“userRatingCountForCurrentVersion”:4, “artworkUrl512″:”http://a4.mzstatic.com/us/r1000/035/Purple/14/a7/7f/mzm.tqzjqmrh.png”,
“trackContentRating”:”4+”, “averageUserRating”:5.0, “userRatingCount”:4},
…………………………………………
{“version”:”1.3″, “kind”:”software”, “artistId”:298593066, “artistName”:”A&G Soluzioni Digitali”, “price”:0.99, ”description”:”This is binaural preview of Peoplelikesound’s Vol.1, the SD card for X-spat player created by A&G Soluzioni Digitali
from the original soundscape compositions by PEOPLELIKESOUND. Press the \”Track 1\” or \”Track 2\” button to start loop playback.
Slide the \”Tilt Listener\” slider  to activate the Open AL spatialisation.  Press the \”info button\” to acquire more information.”,
“genreIds”:["6011", "6013"], “releaseDate”:”2010-01-28T11:49:26Z”, “sellerName”:”A&G Soluzioni Digitali srl”, “currency”:”USD”,
“trackId”:352734137, “trackName”:”PLS1Binaural”, “genres”:["Music", "Healthcare & Fitness"], “releaseNotes”:”Added Support for IOS
4.2″, “primaryGenreName”:”Music”, “primaryGenreId”:6011, “isGameCenterEnabled”:false, “supportedDevices”:["all"],
“wrapperType”:”software”, “artworkUrl60″:”http://a1.mzstatic.com/us/r1000/000/Purple/e1/8f/39/mzi.pkhljhvt.png”,
“artworkUrl100″:”http://a4.mzstatic.com/us/r1000/027/Purple/a1/fc/f3/mzi.hiqsxxto.jpg”,
“artistViewUrl”:”http://itunes.apple.com/us/artist/a-g-soluzioni-digitali/id298593066?uo=4″, “contentAdvisoryRating”:”4+”,
“trackCensoredName”:”PLS1Binaural”, “trackViewUrl”:”http://itunes.apple.com/us/app/pls1binaural/id352734137?mt=8&uo=4″,
“languageCodesISO2A”:["EN"], “fileSizeBytes”:”22972246″, ”screenshotUrls”:["http://a2.mzstatic.com/us/r1000/041/Purple/fd/f8/7b/mzl.oihqgzxg.png",
"http://a1.mzstatic.com/us/r1000/022/Purple/ed/34/1b/mzl.uanybuuh.png",
"http://a3.mzstatic.com/us/r1000/013/Purple/2c/be/ca/mzl.uvsrdgzi.png",
"http://a4.mzstatic.com/us/r1000/024/Purple/05/bb/c8/mzl.wqclmgrk.png",
"http://a1.mzstatic.com/us/r1000/018/Purple/38/29/f4/mzl.wicymqzc.png"], “ipadScreenshotUrls”:[],
“sellerUrl”:”http://www.aegweb.it/pli1.html”, “averageUserRatingForCurrentVersion”:null, “userRatingCountForCurrentVersion”:null,
“artworkUrl512″:”http://a4.mzstatic.com/us/r1000/027/Purple/a1/fc/f3/mzi.hiqsxxto.jpg”, “trackContentRating”:”4+”,
“averageUserRating”:null, “userRatingCount”:null}]}

Get more detail information of how to understand the string in:

http://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html

For me, only two things are important,

The first thing is trackId, which you can further used to compose the lookup url and info page url.

The second thing is trackViewUrl, which you can immediately used to link to the page for information.

Of course, the trackName and Black Arc Studios and price is also quite necessary.

 

Then, let’s come to the Lookup URL:

http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=428212972

You will get the return string like:

{ ”resultCount”:1, ”results”: [{"kind":"software", "artistId":428212975, "artistName":"Black Arc Studios", "price":0.00, "description":"\u2605\u2605\u2605 Free for this weekend only! \u2605\u2605\u2605 \n\n\u2605\u2605\u2605 Featured on Gizmodo &
Lifehacker AU! \u2605\u2605\u2605 Let your voice do the photo taking!!! (Or any sound for that matter!)\n\nSoundSnap is a simple
camera app that lets you take pictures by detecting ANY sound you make!. Simply tap, speak or make any sound into the built-in
microphone and SoundSnap will quickly snap a picture!  Better yet make use of your earphone headset or Bluetooth headset as a
remote!\n\n\n************\nRemote Control\n************\n\n\u2022 SoundSnap allows you to use your device\u2019s built-in microphone
to pick up sound from a distance! Now there\u2019s even more reason to say \u201CCheese\u201D!\n\u2022 Use your earphone headset
\u2019s microphone as a remote release cable!\n\u2022 For those who need precise wireless control, take advantage of your Bluetooth
headset as wireless remote!\n\n\n************\nPerfect for a variety of situations\n************\n\n\u2022 Too cold to take off your
gloves? No problems, with SoundSnap you can still snap a photo using just your voice!\n\u2022 Take advantage of the high quality rear
camera for self-portraits! No more fiddling around trying to find where the button is.\n\u2022 Reduce accidental shaking of the
camera when taking photos by not having to touch the device at all! \n\u2022 Mount your iPhone/iPod on a tripod and use your earphone
headset\u2019s microphone as remote release cable! Perfect for great night shots!\n\u2022 Take the perfect group photo when no one is
around to assist. For even more precision and timing of a snap, use a Bluetooth headset as a wireless remote!\n\u2022 Need to be
discreet when taking a photo? No problems. STEALTH mode will black out the screen to make it seem as though the device is off!\n\n
\n************\nKey Features\n************\n\n\u2022 Tap, speak or make any sound to snap a photo!\n\u2022 Adjust the sensitivity of
the sound detection! - Optimize the app for quiet or noisy environments.\n\u2022 Use your headset as a remote cable release!\n\u2022
Use your Bluetooth headset as a wireless remote!\n\u2022 Timer Mode \u2013 Give yourself time to perfect your pose before taking that
snap!\n\u2022 Stealth mode \u2013 For those times when discretion is needed!\n\n\n************\nCompatibility\n************
\nSoundSnap is compatible with the iPhone 4, iPhone 3GS and iPod Touch (4th Generation).\nFor Apple\u2019s built-in tap-to-focus
function to be active, iOS 4.3 is required.\n\n\n************\nWebsite\n************\nhttp://www.blackarcstudios.com/soundsnap",
"version":"1.0", "genreIds":["6008", "6002"], “releaseDate”:”2011-04-07T07:00:00Z”, “sellerName”:”Black Arc Studios Private Limited”,
“currency”:”USD”, “trackId”:428212972, “trackName”:”SoundSnap”, “genres”:["Photography", "Utilities"], “releaseNotes”:”",
“primaryGenreName”:”Photography”, “primaryGenreId”:6008, “isGameCenterEnabled”:false, “supportedDevices”:["iPodTouchourthGen",
"iPhone", "iPhone4", "iPhone-3G", "iPhone-3GS", "iPad23G", "iPad2Wifi"], “wrapperType”:”software”,
“artworkUrl60″:”http://a1.mzstatic.com/us/r1000/013/Purple/eb/8f/9d/mzi.erfyikae.png”,
“artworkUrl100″:”http://a4.mzstatic.com/us/r1000/035/Purple/14/a7/7f/mzm.tqzjqmrh.png”,
“artistViewUrl”:”http://itunes.apple.com/us/artist/black-arc-studios/id428212975?uo=4″, “contentAdvisoryRating”:”4+”,
“trackCensoredName”:”SoundSnap”, “trackViewUrl”:”http://itunes.apple.com/us/app/soundsnap/id428212972?mt=8&uo=4″,
“languageCodesISO2A”:["DE", "EN", "ES", "FR", "IT", "JA", "KO", "ZH"], “fileSizeBytes”:”10089479″, ”screenshotUrls”:["http://a1.mzstatic.com/us/r1000/027/Purple/68/c9/5b/mzl.ctnqyntv.png",
"http://a2.mzstatic.com/us/r1000/050/Purple/3c/93/2d/mzl.qfsdkame.png",
"http://a2.mzstatic.com/us/r1000/029/Purple/37/cf/95/mzl.vugmtqaz.png"], “ipadScreenshotUrls”:[],
“sellerUrl”:”http://www.blackarcstudios.com/soundsnap”, “averageUserRatingForCurrentVersion”:5.0,
“userRatingCountForCurrentVersion”:4, “artworkUrl512″:”http://a4.mzstatic.com/us/r1000/035/Purple/14/a7/7f/mzm.tqzjqmrh.png”,
“trackContentRating”:”4+”, “averageUserRating”:5.0, “userRatingCount”:4}]}

Parse it and get more information about the app.

Last, when you get the trackId, you can compose the url of info page like:

http://itunes.apple.com/app/id428212972

 
6 Comments

Posted in 工作

 

datetime是个讨厌的东西

29 Jul

datetime是个讨厌的东西

最近基本上就在纠结这个问题了。
第一个问题是,invalid date
第二个问题是,如何传参
第三个问题是,时区

一、

关于第一个问题,当在rails中使用date_select控件的时候,以下为例

true%>
true%>

将会得到参数

"end_at"=>{"my_date(1i)"=>"2010", "my_date(2i)"=>"7", "my_date(3i)"=>"29"}, "start_at"=>{"my_date(1i)"=>"2010", "my_date(2i)"=>"2", "my_date(3i)"=>"31"}

这里存在的问题是,因为产生的控件是一个简单控件,不带有script,所以,当你选择年月日的时候,后续选择不过因为闰年或者月份的不同而改变。也就是说,你可以选择一个2月31日的日期,而它忠实地将这个参数传回。

我曾经写过一个函数来处理这些参数,将它变成一个日期的变量,如下:

def self.hash_to_date(obj,prefix)
Date.new(obj["#{prefix}(1i)"].to_i,obj["#{prefix}(2i)"].to_i,obj["#{prefix}(3i)"].to_i)
end

而这所遇到的问题是,当它接收到一个2月31日时会直接出错。

之后我使用了一个名为DatePicker的插件,它的输出结果为形如‘2010-10-10’的字符串,而合法的字符串通过to_date函数也能直接变成时间。
但这个插件在某些浏览器下并不正常,因为,我也允许手动输入时间串。
这样就同样有了如上所述的invalid date的情况,除非使用更加严格的验证函数。

然而有一点是可以肯定的,当你所选的内容将被存入数据库的时候,rails能够很好的处理它。
我翻找了很久并没有找到它是如何处理的,所以做了一个折中的做法,新建一个model,专门处理时间。

起初,我设想,这个model有且只有一个字段,也就是时间,名为my_date。最简单的方法是使用attr_accessor。
然后,attr_accessor似乎无法处理datetime这种复杂的数据类型,是故一直报错:1 error(s) on assignment of multiparameter attributes
并且,因为没有相关表,必须tableless掉,于是也可能有一定影响,深入未考。

后来我想到了tableless时候的做法。
我可以首先tableless掉,然后添加一个column。
这段代码我在上次探索tableless的时候已经找到了,其内容如下:

class ConvertDate < ActiveRecord::Base
def self.columns
@columns ||= [];
@columns << ActiveRecord::ConnectionAdapters::Column.new('my_date', nil,
'datetime', true)
end
end

使用的方式为:view依旧如之前所示,在controller中处理如下:

@start_date = ConvertDate.new(params[:start_at]).my_date
@end_date = ConvertDate.new(params[:end_at]).my_date

这样就解决的得到合法时间的问题。

二、

关于第二个问题,如果使用rails的date_select控件,那么,可以看到,参数多而杂,而如果使用之前提到的DatePicker则参数相对简单。
如果在一次post中,复杂的参数也许并不如何,但是,如果在get中,特别是在结合了排序和翻页之类的功能,参数列表就会显得长而繁杂。

所以我在考虑是否在两者间进行转化,通过class函数来判断:
如果是hash,直接new,如果是string,先进行转化。
而在传参之前,也可以尝试将hash转化为string,然后向后传。
不过这一点,我还没有深入来做。

三、

关于时区问题,因为在enviroment.rb中配置了:

config.time_zone = 'Beijing'

所以就会产生时区问题。

当建立一条记录的时候,当你包含了time_stamp,你就会发现,数据库中存储的是utc时间,当你使用user.created_at取出的时候,则会得到一个包含时区信息的时间类型,时间与你所期望的吻合。

然后来看一下其他几个函数
首先是to_date和to_datetime
日期部分的合法模式是2010.1.1或2010/1/1或2010-1-1
时间部分为0:0:0

还有一些简单的时间操作为 + 1.year/month/day/hour/minute/second
可以‘+’也可以‘-’,后面可以加s,也可以不加,不过要注意,数字和点和量词连写,并且前后空格,否则容易出错。

当我们使用to_date函数一下子无法看出时区信息,那么我们加上几个小时
那么就会发现to_date产生后运算后的时间是8时区,而,如果使用to_datetime则是0时区

同样对于Date.new的结果也是8时区的,而DateTime.new的结果是0时区的。

最后才是最纠结的问题,一个我一直没有发现的大问题。
例如,当数据库的utc时间为4,也么8时区时间是12,于是我们尝试用8时区的11点和13点来筛选数据,发现一切出乎了想象。

>> User.last
=> #
>> User.last.created_at
=> Thu, 29 Jul 2010 12:23:06 CST +08:00

>> b='2010.7.29'.to_date + 11.hour
=> Thu Jul 29 11:00:00 +0800 2010
>> c='2010.7.29'.to_date + 13.hour
=> Thu Jul 29 13:00:00 +0800 2010

>> User.last(:conditions=>['created_at > ?',b])
=> nil
>> User.last(:conditions=>['created_at #
>> User.last(:conditions=>['created_at > ?',c])
=> nil
>> User.last(:conditions=>['created_at #

我曾经认为是用12点多,和11点13点这三个时间比较,结果发现它才没有那么智能。
也就是说,大于11点,无,小于11点,有,大于13点,无,小于13点,有。
也就是说,其实它是用4点多和11点和13点在比较。

解决这个问题的方法,要么:
1、不设时区,在显示时转换
2、使用to_date和Date.new,并在选择的时候使用selected_time.utc来转换时区

另外,补充
Date只有today,没有now
DateTime只有now,没有today
两个的yesterday都是8时区。

于是,纠结暂完。

 
13 Comments

Posted in 工作