Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adds basic exception handling for some invalid data #39

Merged
merged 1 commit into from

3 participants

@ckdake

Fixes issue #38. If some data fails to parse in XmlSimple, convert to ISO-8859-1 and try again.

This is far from perfect, but lets these invalid data points be skipped when walking through api results. It's a shame that Last.fm returns invalid data sometimes :(

Test covers this fix and passes on ruby-1.9.3-p194

@nettofarah

when is this going to be merged?
I'm having a lot of problems with this parsing issue.

@youpy youpy merged commit d0aa7ce into youpy:master

1 check passed

Details default The Travis build passed
@youpy
Owner

Thanks, sorry for slow reply.

I've merged and released 1.13.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 15, 2012
  1. @ckdake
This page is out of date. Refresh to see the latest.
View
2  lib/lastfm/response.rb
@@ -7,6 +7,8 @@ class Response
def initialize(body)
@xml = XmlSimple.xml_in(body, 'ForceArray' => ['image', 'tag', 'user', 'event', 'correction'])
+ rescue REXML::ParseException
+ @xml = XmlSimple.xml_in(body.encode(Encoding.find("ISO-8859-1"), :undef => :replace), 'ForceArray' => ['image', 'tag', 'user', 'event', 'correction'])
end
def success?
View
18 spec/fixtures/user_get_recent_tracks_malformed.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0"?>
+<lfm status="ok">
+ <recenttracks user="RJ" page="1" perPage="10" totalPages="3019">
+ <track>
+ <artist mbid="">￾䬀愀瀀 匀氀愀瀀</artist>
+ <name>￾匀琀爀漀戀攀 䰀椀最栀琀猀 ㈀ 䤀戀椀稀愀 ⠀䐀攀愀搀洀愀甀㔀 瘀猀 䔀氀氀椀攀 䜀漀甀氀搀椀渀最 瘀猀 匀眀攀搀椀猀栀 䠀漀甀猀攀 䴀愀昀椀愀 瘀猀 䘀攀爀最椀攀 瘀猀 䰀椀氀 䨀漀渀⤀</name>
+ <streamable>0</streamable>
+ <mbid></mbid>
+ <album mbid="">￾䔀愀爀洀椀氀欀⸀挀漀洀 ⴀ ⌀䴀愀猀栀甀瀀䴀漀渀搀愀礀</album>
+ <url>http://www.last.fm/music/%EF%BF%BE%E4%AC%80%E6%84%80%E7%80%80%E2%80%80%E5%8C%80%E6%B0%80%E6%84%80%E7%80%80/_/%EF%BF%BE%E5%8C%80%E7%90%80%E7%88%80%E6%BC%80%E6%88%80%E6%94%80%E2%80%80%E4%B0%80%E6%A4%80%E6%9C%80%E6%A0%80%E7%90%80%E7%8C%80%E2%80%80%E3%88%80%E2%80%80%E4%A4%80%E6%88%80%E6%A4%80%E7%A8%80%E6%84%80%E2%80%80%E2%A0%80%E4%90%80%E6%94%80%E6%84%80%E6%90%80%E6%B4%80%E6%84%80%E7%94%80%E3%94%80%E2%80%80%E7%98%80%E7%8C%80%E2%80%80%E4%94%80%E6%B0%80%E6%B0%80%E6%A4%80%E6%94%80%E2%80%80%E4%9C%80%E6%BC%80%E7%94%80%E6%B0%80%E6%90%80%E6%A4%80%E6%B8%80%E6%9C%80%E2%80%80%E7%98%80%E7%8C%80%E2%80%80%E5%8C%80%E7%9C%80%E6%94%80%E6%90%80%E6%A4%80%E7%8C%80%E6%A0%80%E2%80%80%E4%A0%80%E6%BC%80%E7%94%80%E7%8C%80%E6%94%80%E2%80%80%E4%B4%80%E6%84%80%E6%98%80%E6%A4%80%E6%84%80%E2%80%80%E7%98%80%E7%8C%80%E2%80%80%E4%98%80%E6%94%80%E7%88%80%E6%9C%80%E6%A4%80%E6%94%80%E2%80%80%E7%98%80%E7%8C%80%E2%80%80%E4%B0%80%E6%A4%80%E6%B0%80%E2%80%80%E4%A8%80%E6%BC%80%E6%B8%80%E2%A4%80</url>
+ <image size="small"></image>
+ <image size="medium"></image>
+ <image size="large"></image>
+ <image size="extralarge"></image>
+ <date uts="1338664693">2 Jun 2012, 19:18</date>
+ </track>
+ </recenttracks>
+</lfm>
View
11 spec/method_specs/user_spec.rb
@@ -210,6 +210,17 @@
tracks[1]['artist']['content'].should == 'Kylie Minogue'
tracks.size.should == 2
end
+
+ it 'should not error when a user\'s recent tracks includes malformed data' do
+ @lastfm.should_receive(:request).with('user.getRecentTracks', {
+ :user => 'test',
+ :page => nil,
+ :limit => nil,
+ :to => nil,
+ :from => nil
+ }).and_return(make_response('user_get_recent_tracks_malformed'))
+ tracks = @lastfm.user.get_recent_tracks(:user => 'test')
+ end
end
describe '#get_top_tags' do
Something went wrong with that request. Please try again.