-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support parsing for m3u and m3u8 files #187
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6ed5bb3
Support m3u format
nitika080289 49fd3c0
Merge branch 'master' of ssh://github.com/WeTransfer/format_parser in…
nitika080289 cc89558
Implemented review suggestions
nitika080289 ecc9f90
Updated regex and supported formats
nitika080289 f3bf637
Remove size
nitika080289 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class FormatParser::M3UParser | ||
include FormatParser::IOUtils | ||
|
||
HEADER = '#EXTM3U' | ||
|
||
def likely_match?(filename) | ||
filename =~ /\.m3u8?$/i | ||
end | ||
|
||
def call(io) | ||
io = FormatParser::IOConstraint.new(io) | ||
|
||
header = safe_read(io, 7) | ||
return unless HEADER.eql?(header) | ||
|
||
FormatParser::Text.new( | ||
format: :m3u, | ||
size: io.size | ||
) | ||
end | ||
FormatParser.register_parser new, natures: :text, formats: :m3u | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module FormatParser | ||
class Text | ||
include FormatParser::AttributesJSON | ||
|
||
NATURE = :text | ||
|
||
attr_accessor :format | ||
attr_accessor :size | ||
|
||
# Only permits assignments via defined accessors | ||
def initialize(**attributes) | ||
attributes.map { |(k, v)| public_send("#{k}=", v) } | ||
end | ||
|
||
def nature | ||
NATURE | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
C:\My Music\Artist\Year\1.Test.mp3 | ||
C:\My Music\Artist\Year\10.Test.mp3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#EXTM3U | ||
#EXTINF:170,Artist - 1 | ||
C:\My Music\Artist\Year\1.Test.mp3 | ||
#EXTINF:3,Artist - 10 | ||
C:\My Music\Artist\Year\10.Test.mp3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#EXTM3U | ||
#EXTINF:170,Artist - 1 | ||
C:\My Music\Artist\Year\1.Test.mp3 | ||
#EXTINF:3,Artist - 10 | ||
C:\My Music\Artist\Year\10.Test.mp3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'spec_helper' | ||
|
||
describe FormatParser::M3UParser do | ||
let(:parsed_m3u) do | ||
subject.call( | ||
File.open( | ||
Pathname.new(fixtures_dir).join('M3U').join(m3u_file), | ||
'rb' | ||
) | ||
) | ||
end | ||
|
||
describe 'an m3u file with missing header' do | ||
let(:m3u_file) { 'plain_text.m3u' } | ||
|
||
it 'does not parse the file successfully' do | ||
expect(parsed_m3u).to be_nil | ||
end | ||
end | ||
|
||
describe 'an m3u file with valid header' do | ||
let(:m3u_file) { 'sample.m3u' } | ||
|
||
it 'parses the file successfully' do | ||
expect(parsed_m3u).not_to be_nil | ||
expect(parsed_m3u.nature).to eq(:text) | ||
expect(parsed_m3u.format).to eq(:m3u) | ||
expect(parsed_m3u.size).to eq(124) | ||
end | ||
end | ||
|
||
describe 'an m3u8 file with valid header' do | ||
let(:m3u_file) { 'sample.m3u8' } | ||
|
||
it 'parses the file successfully' do | ||
expect(parsed_m3u).not_to be_nil | ||
expect(parsed_m3u.nature).to eq(:text) | ||
expect(parsed_m3u.format).to eq(:m3u) | ||
expect(parsed_m3u.size).to eq(124) | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just want to be sure: will this read the whole file in order to get the size? Because if it does, it might be wiser not to include it (or implement it in a different way).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
size
is available once a single read has been performed and will not incur any extra reads