# Use existing tag data in script

**URL:** https://community.mp3tag.de/t/use-existing-tag-data-in-script/61745
**Category:** Web Sources Discussion
**Created:** [August 5, 2023, 7:26am UTC](https://community.mp3tag.de/t/use-existing-tag-data-in-script/61745 "2023-08-05T07:26:54Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Morningstar](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@Morningstar](https://community.mp3tag.de/u/Morningstar)
#### Post date: [August 5, 2023, 7:26am UTC](https://community.mp3tag.de/t/use-existing-tag-data-in-script/61745/1 "2023-08-05T07:26:54Z")

</div>

Hey all, I was looking for some advice on how to do a thing.

I was trying to build a script for [TMDB.org](http://TMDB.org) that would pull TV episode info and ran into a snag since all the info is down behind season and episode subdirectories. I know there should be a way to have the script grab the season and episode numbers from the existing tag, but I can't seem to find it in the documentation.

f someone could direct me to the right part of the documentation or has some advice that would be great.

---

<div class="post-metadata">

### Author: ![Morningstar](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@Morningstar](https://community.mp3tag.de/u/Morningstar)
#### Post date: [August 5, 2023, 9:14pm UTC](https://community.mp3tag.de/t/use-existing-tag-data-in-script/61745/2 "2023-08-05T21:14:04Z")

</div>

OK I was trying to figure this out, why is this not working?

> ```
> json_select "id"
> SayRest
> Say "/season/"
> $num(%tvseason%,)
> SayRest
> Say "/episode/"
> $num(%tvepisode%,)
> SayRest
> Say "?api_key=XXXXXXXXXXXX&language=English" # USE SAME API KEY AND LANGUAGE AS MENTIONED ABOVE
> Say "|"
> 
> ```

---

<div class="post-metadata">

### Author: ![Florian](https://community.mp3tag.de/user_avatar/community.mp3tag.de/florian/32/5759_2.png) [@Florian](https://community.mp3tag.de/u/Florian)
#### Post date: [August 6, 2023, 6:53am UTC](https://community.mp3tag.de/t/use-existing-tag-data-in-script/61745/3 "2023-08-06T06:53:31Z")

</div>

> [@Morningstar](#):
>
> I know there should be a way to have the script grab the season and episode numbers from the existing tag, but I can't seem to find it in the documentation.

Yes, it is not possible to access the tag data from the script. Also, scripting functions like `$num()` are not available there.

You can use the input from the search parameters to compose the target URL, or let the user enter the direct URL and append the API key in the script.

---

<div class="post-metadata">

### Author: ![Florian](https://community.mp3tag.de/user_avatar/community.mp3tag.de/florian/32/5759_2.png) [@Florian](https://community.mp3tag.de/u/Florian)
#### Post date: [August 6, 2023, 7:23am UTC](https://community.mp3tag.de/t/use-existing-tag-data-in-script/61745/4 "2023-08-06T07:23:55Z")

</div>

Please also see this topic for an existing Tag Source for TMDB:

> [@\[WS\] TheMovieDB](https://community.mp3tag.de/t/ws-themoviedb/48029):
>
> Hi, first of all I would just like to commend briefly MP3Tag and everyone who is involved in this app - you do a great job, thanks so much for that. I combine tagging my library with Musicbrainz Picard and I haven't found any other functional software alternative of this combination yet. I tried about 30 of them myself, including paid ones. I attach WS script to grab data from TheMovieDB(.org). Perhaps most will not use it, but it can help someone. I make the result of tagging as the largest p…

---

<div class="post-metadata">

### Author: ![RobertM](https://community.mp3tag.de/user_avatar/community.mp3tag.de/robertm/32/16067_2.png) [@RobertM](https://community.mp3tag.de/u/RobertM)
#### Post date: [February 23, 2025, 7:09pm UTC](https://community.mp3tag.de/t/use-existing-tag-data-in-script/61745/5 "2025-02-23T19:09:16Z")

</div>

> [@Morningstar](#):
>
> `?api_key=XXXXXXXXXXXX&language=`

Would it be possible to get more detail on _how_ to use the input from the search parameters to compose a target URL?

Like Morningstar, I also got stuck trying to build a script for [TMDB.org](http://TMDB.org) that would pull TV episode info.

Ideally, an example that, given input search parameters series\_id, season\_number and episode\_number, could get details constructing a URL like this:

[https://api.themoviedb.org/3/tv/{series\_id}/season/{season\_number}/episode/{episode\_number}?api\_key=XXXXXXXXXXXX&language=en-GB](https://api.themoviedb.org/3/tv/%7Bseries_id%7D/season/%7Bseason_number%7D/episode/%7Bepisode_number%7D?api_key=XXXXXXXXXXXX&language=en-GB)

As per TMDB API reference page for TV episode details: [Details](https://developer.themoviedb.org/reference/tv-episode-details)

---

<div class="post-metadata">

### Author: ![RobertM](https://community.mp3tag.de/user_avatar/community.mp3tag.de/robertm/32/16067_2.png) [@RobertM](https://community.mp3tag.de/u/RobertM)
#### Post date: [February 26, 2025, 2:58pm UTC](https://community.mp3tag.de/t/use-existing-tag-data-in-script/61745/6 "2025-02-26T14:58:46Z")

</div>

Ah-ha! I got tag sources to work at TV episode level.

At first, I was trying to modify the logic in Sweder's excellent [TheMovieDB script](https://community.mp3tag.de/t/ws-themoviedb/48029).

That approach for movies can be made to work well in searching for TV series by name and year.  
I use Mp3tag's album field for series name (suffixed with season number in the format - S99), so my SearchBy and IndexUrl for series level tagging are set up as follows:

```auto
[SearchBy]=Series||$regexp(%album%,'(.*)( - S\d\d)',$1)||&query=%s||Year||%year%||&year=%s
[IndexUrl]=https://api.themoviedb.org/3/search/tv?query=%s&api_key=XXXXXXXXXXXX&language=en-GB

```

However, once _series_ level tags are populated this way, in particular, ensuring that TMDB tag is populated...

```auto
OutputTo "TMDB"
json_select "id"
SayRest

```

... then, assuming you already have (or can input) season number and episode number, you can call a second tag source script to get TV _episode_ level information (for example, episode name).

The trick with the episode level script is to leave out IndexUrl, and have an empty ParserScriptIndex section, as you don't need to search and select from query results when you have unique identifiers.

Instead (as I figured out after looking at Cover Art#Discogs Release ID.src), you can just use SearchBy criteria to construct AlbumUrl directly.

Bearing in mind that I use %discnumber% rather than %tvepisiode% (as Mp3tag's TVEPISIODE field isn't mapped properly for MKV), SearchBy and AlbumUrl are thus set in my TV episode script as follows:

```auto
[SearchBy]=SeriesId||%tmdb%||%s||Season||%tvseason%||/season/%s||Episode||%discnumber%||/episode/%s 
[AlbumUrl]=https://api.themoviedb.org/3/tv/%s?append_to_response=credits,external_ids&api_key=XXXXXXXXXXXX&language=en-GB

```

---

<div class="post-metadata">

### Author: ![RobertM](https://community.mp3tag.de/user_avatar/community.mp3tag.de/robertm/32/16067_2.png) [@RobertM](https://community.mp3tag.de/u/RobertM)
#### Post date: [April 11, 2025, 2:40pm UTC](https://community.mp3tag.de/t/use-existing-tag-data-in-script/61745/7 "2025-04-11T14:40:17Z")

</div>

> [@RobertM](#):
>
> I got tag sources to work at TV episode level.

A more detailed (and more up-to-date) response describing how to retrieve TV episode information from TMDB has been posted on the [TheMovieDB web sources script topic](https://community.mp3tag.de/t/ws-themoviedb/48029/9).
