Skip to content

Commit

Permalink
new search-helper function. Corrected test data.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrudio committed Jul 12, 2016
1 parent 64020e1 commit 0ad89e1
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 1,293 deletions.
47 changes: 47 additions & 0 deletions plex_data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package plex

const testMetadata = `
<?xml version="1.0" encoding="UTF-8"?>
<MediaContainer size="1" allowSync="1" identifier="com.plexapp.plugins.library" librarySectionID="1" librarySectionTitle="TV Shows" librarySectionUUID="abc123" mediaTagPrefix="/system/bundle/media/flags/" mediaTagVersion="1466733873">
<Directory ratingKey="1" key="/library/metadata/1/children" guid="com.plexapp.agents.thetvdb://153021?lang=en" librarySectionID="1" studio="AMC" type="show" title="The Walking Dead" titleSort="Walking Dead" contentRating="TV-MA" summary="The world we knew is gone. An epidemic of apocalyptic proportions has swept the globe causing the dead to rise and feed on the living. In a matter of months society has crumbled. In a world ruled by the dead, we are forced to finally start living. Based on a comic book series of the same name by Robert Kirkman, this AMC project focuses on the world after a zombie apocalypse. The series follows a police officer, Rick Grimes, who wakes up from a coma to find the world ravaged with zombies. Looking for his family, he and a group of survivors attempt to battle against the zombies in order to stay alive.&#xD;&#xA;" index="1" rating="9.0" viewCount="22" lastViewedAt="1466507083" year="2010" thumb="/library/metadata/1/thumb/1459739349" art="/library/metadata/1/art/1459739349" banner="/library/metadata/1/banner/1459739349" theme="/library/metadata/1/theme/1459739349" duration="2400000" originallyAvailableAt="2010-10-31" leafCount="12" viewedLeafCount="11" childCount="1" addedAt="1448444228" updatedAt="1459739349">
<Genre id="47" tag="Action" />
<Genre id="6" tag="Drama" />
<Genre id="7" tag="Horror" />
<Genre id="48" tag="Suspense" />
<Role id="49" tag="Andrew Lincoln" role="Rick Grimes" />
<Role id="50" tag="Melissa McBride" role="Carol Peletier" />
<Role id="51" tag="Norman Reedus" role="Daryl Dixon" />
<Role id="52" tag="Steven Yeun" role="Glenn" />
<Role id="53" tag="Lennie James" role="Morgan Jones" />
<Role id="54" tag="Chandler Riggs" role="Carl Grimes" />
<Role id="55" tag="Lauren Cohan" role="Maggie Greene" thumb="http://image.tmdb.org/t/p/original/5W4AV3ZXn38NlEMqPy9QPjwRRz8.jpg" />
<Role id="56" tag="Danai Gurira" role="Michonne" />
<Role id="1709" tag="Tom Payne" role="Paul &apos;Jesus&apos; Rovia" />
<Role id="58" tag="Austin Nichols" role="Spencer Monroe" />
<Role id="62" tag="Tyler James Williams" role="Noah" />
<Role id="63" tag="Seth Gilliam" role="Father Gabriel" />
<Role id="64" tag="Josh McDermitt" role="Eugene Porter" />
<Role id="65" tag="Christian Serratos" role="Rosita Espinosa" />
<Role id="66" tag="Michael Cudlitz" role="Abraham Ford" />
<Role id="67" tag="Alanna Masterson" role="Tara Chambler" />
<Role id="68" tag="Lawrence Gilliard Jr." role="Bob Stookey" />
<Role id="69" tag="Dallas Roberts" role="Milton Mamet" />
<Role id="70" tag="Chad L. Coleman" role="Tyreese" />
<Role id="71" tag="Sonequa Martin-Green" role="Sasha" />
<Role id="72" tag="David Morrissey" role="The Governor" thumb="http://image.tmdb.org/t/p/original/4w0XlXlk4IKO8g8HejnBHn7xOPG.jpg" />
<Role id="73" tag="Michael Rooker" role="Merle Dixon" />
<Role id="74" tag="Emily Kinney" role="Beth Greene" />
<Role id="75" tag="Scott Wilson" role="Hershel Greene" />
<Role id="76" tag="Irone Singleton" role="T-Dog" />
<Role id="77" tag="Emma Bell" role="Amy" />
<Role id="78" tag="Laurie Holden" role="Andrea" />
<Role id="79" tag="Jeffrey DeMunn" role="Dale" />
<Role id="80" tag="Jon Bernthal" role="Shane Walsh" />
<Role id="81" tag="Sarah Wayne Callies" role="Lori Grimes" />
<Role id="1993" tag="Jeffrey Dean Morgan" role="Negan" thumb="http://image.tmdb.org/t/p/original/6ors2HGy8UdXm8kBx6on0HwRHB5.jpg" />
<Label id="2455" tag="Siirclutch-guest" />
<Field name="label" locked="1" />
<Location path="/media/tv/The Walking Dead" />
</Directory>
</MediaContainer>
`
243 changes: 3 additions & 240 deletions plex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,249 +80,12 @@ func TestPlexTest(t *testing.T) {
}
}

func TestGetMediaInfo(t *testing.T) {
testData := `
{
"_elementType": "abc123",
"allowSync": "abc123",
"identifier": "abc123",
"librarySectionID": "abc123",
"librarySectionTitle": "abc123",
"librarySectionUUID": "abc123",
"mediaTagPrefix": "abc123",
"mediaTagVersion": "abc123",
"_children": [
{
"_elementType": "abc123",
"ratingKey": 797,
"key": "abc123",
"guid": "abc123",
"librarySectionID": 2,
"studio": "abc123",
"type": "abc123",
"title": "abc123",
"contentRating": "PG-13",
"summary": "As his wedding day approaches, Ben heads to Miami with his soon-to-be brother-in-law James to bring down a drug dealer who's supplying the dealers of Atlanta with product.",
"rating": "abc123",
"viewCount": 4,
"lastViewedAt": 1464768458,
"year": 2016,
"tagline": "The brothers-in-law are back",
"thumb": "abc123",
"art": "abc123",
"duration": 6090091,
"originallyAvailableAt": "2016-01-15",
"addedAt": 1460520144,
"updatedAt": 1460520159,
"chapterSource": "abc123",
"primaryExtraKey": "abc123",
"_children": [
{
"_elementType": "abc123",
"videoResolution": "abc123",
"id": 1893,
"duration": 6090091,
"bitrate": 6166,
"width": 1280,
"height": 536,
"aspectRatio": "abc123",
"audioChannels": 6,
"audioCodec": "abc123",
"videoCodec": "abc123",
"container": "abc123",
"videoFrameRate": "abc123",
"audioProfile": "abc123",
"videoProfile": "abc123",
"_children": [
{
"_elementType": "abc123",
"id": "abc123",
"key": "abc123",
"duration": 6090091,
"file": "abc123",
"size": 4693741813,
"audioProfile": "abc123",
"container": "abc123",
"videoProfile": "abc123",
"_children": [
{
"_elementType": "abc123",
"id": 3922,
"streamType": 1,
"default": true,
"codec": "abc123",
"index": 0,
"bitrate": 4654,
"language": "abc123",
"languageCode": "abc123",
"bitDepth": 8,
"cabac": true,
"chromaSubsampling": "abc123",
"codecID": "abc123",
"duration": 6090090,
"frameRate": "abc123",
"frameRateMode": "abc123",
"hasScalingMatrix": false,
"height": 536,
"level": 41,
"pixelFormat": "abc123",
"profile": "abc123",
"refFrames": 5,
"scanType": "abc123",
"width": 1280
},
{
"_elementType": "abc123",
"id": 3923,
"streamType": 2,
"selected": true,
"default": true,
"codec": "abc123",
"index": 1,
"channels": 6,
"bitrate": 1509,
"language": "abc123",
"languageCode": "abc123",
"audioChannelLayout": "abc123",
"bitDepth": 24,
"bitrateMode": "abc123",
"codecID": "abc123",
"duration": 6090091,
"profile": "abc123",
"samplingRate": 48000
}
]
}
]
},
{
"_elementType": "abc123",
"id": 47,
"tag": "abc123",
"count": 25
},
{
"_elementType": "abc123",
"id": 133,
"tag": "abc123",
"count": 22
},
{
"_elementType": "abc123",
"id": 2041,
"tag": "Phil Hay"
},
{
"_elementType": "abc123",
"id": 2042,
"tag": "Matt Manfredi"
},
{
"_elementType": "abc123",
"id": 2040,
"tag": "Tim Story"
},
{
"_elementType": "abc123",
"id": 136,
"tag": "abc123",
"count": 61
},
{
"_elementType": "abc123",
"id": 2043,
"tag": "abc123",
"role": "abc123",
"thumb": "http://image.tmdb.org/t/p/original/nTYKqSQzJ9VlYKgqoES7WIDHywi.jpg"
},
{
"_elementType": "abc123",
"id": 2044,
"tag": "abc123",
"role": "abc123",
"thumb": "http://image.tmdb.org/t/p/original/dzdn1tyWkC4EjlBVKvpAhg5osYA.jpg"
},
{
"_elementType": "abc123",
"id": 2045,
"tag": "abc123",
"role": "abc123",
"thumb": "http://image.tmdb.org/t/p/original/bEaxBT4JSoab2OFSvQwrNAnoNsU.jpg"
},
{
"_elementType": "abc123",
"id": 2046,
"tag": "abc123",
"role": "abc123",
"thumb": "http://image.tmdb.org/t/p/original/9H87Ss6qcnxpQHtBhiMKYNSGr1g.jpg"
},
{
"_elementType": "abc123",
"id": 575,
"tag": "abc123",
"count": 2,
"role": "abc123",
"thumb": "http://image.tmdb.org/t/p/original/rEebZHRju1WtSOdvQJB5je5ZNGj.jpg"
},
{
"_elementType": "abc123",
"id": 2047,
"tag": "abc123",
"role": "abc123",
"thumb": "http://image.tmdb.org/t/p/original/8AaJIsIb7yJcfwcgbD7qsT6ameq.jpg"
},
{
"_elementType": "abc123",
"id": 2048,
"tag": "abc123",
"role": "abc123",
"thumb": "http://image.tmdb.org/t/p/original/7bd8Qtmz25PtMuVN8uSzTp8wemx.jpg"
},
{
"_elementType": "abc123",
"id": 2049,
"tag": "abc123",
"role": "abc123",
"thumb": "http://image.tmdb.org/t/p/original/nTJQPpn8OBkFM31rukjxM32rA0F.jpg"
},
{
"_elementType": "abc123",
"id": 2050,
"tag": "abc123",
"role": "abc123",
"thumb": "http://image.tmdb.org/t/p/original/sCeY5nuPUhfwWElxNtXuwRYKMBr.jpg"
},
{
"_elementType": "abc123",
"id": 2051,
"tag": "abc123",
"role": "abc123",
"thumb": "http://image.tmdb.org/t/p/original/ycNO8wU6H75wDDfojMCTjoxtEmt.jpg"
},
{
"_elementType": "abc123",
"id": 2052,
"tag": "abc123",
"role": "abc123",
"thumb": "http://image.tmdb.org/t/p/original/hQyyGPUHmbp5A4f9vI7z0dQGpuY.jpg"
},
{
"_elementType": "abc123",
"id": 2053,
"tag": "abc123",
"role": "Amir"
}
]
}
]
}
`

func TestGetMetadata(t *testing.T) {
query := "blahblah"

_, _plex := newTestServer(200, testData)
_, _plex := newTestServer(200, testMetadata)

_, err := _plex.GetMediaInfo(query)
_, err := _plex.GetMetadataChildren(query)

if err != nil {
t.Error(err.Error())
Expand Down
78 changes: 78 additions & 0 deletions search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package plex

import "regexp"

// SearchPlex searches just like Search, but omits the last 4 results which are not relevant
func (p *Plex) SearchPlex(title string) (SearchResults, error) {
results, err := p.Search(title)

if err != nil {
return SearchResults{}, err
}

results.Children = results.Children[:4]

return results, nil
}

// ExtractKeyAndThumbFromURL extracts the rating key and thumbnail id from the url
func (p *Plex) ExtractKeyAndThumbFromURL(_url string) (string, string) {
count := len(_url)

var key string
var thumbID string

if count > 18 {
k := _url[18:]
keyCount := len(k)

if keyCount > 7 {
// Get key
for ii := 0; ii < keyCount; ii++ {
if k[ii:ii+1] == "/" {
key = k[:ii]
thumbID = k[ii+7:]
break
}

}
}
}

return key, thumbID
}

// ExtractKeyFromRatingKey extracts the key from the rating key url
func (*Plex) ExtractKeyFromRatingKey(key string) string {
keyCount := len(key)

if keyCount > 18 {
key = key[18:]
keyCount = len(key)

if keyCount > 9 {
for ii := 0; ii < keyCount; ii++ {
if key[ii:ii+1] == "/" {
key = key[:ii]
break
}
}
}

return key
}

return key
}

// ExtractKeyFromRatingKeyRegex extracts the key from a rating key url via regex
func (p *Plex) ExtractKeyFromRatingKeyRegex(key string) string {
r := regexp.MustCompile(`\d+`)
result := r.FindStringSubmatch(key)

if len(key) == 0 {
return ""
}

return result[0]
}
Loading

0 comments on commit 0ad89e1

Please sign in to comment.