SageTV Community  

Go Back   SageTV Community > General Discussion > General Discussion
Forum Rules FAQs Community Downloads Today's Posts Search

Notices

General Discussion General discussion about SageTV and related companies, products, and technologies.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 05-23-2021, 08:37 AM
nyplayer nyplayer is offline
SageTVaholic
 
Join Date: Sep 2005
Posts: 4,997
Need Python help .... ChannelsDVR Json to properties,

I have a Python Script to parse a JSON file ... I can Parse and write the output but I would like it to be in Sage Properties format ... But I am having a brain
fart .. Trying to get Channels DVR recordings to import correctly in SageTV using property files.

Json File ...

Code:
{"Description":"Navy lawyers (Tom Cruise, Demi Moore) defend Marines for a murder at Gitmo.","Episode":0,"Genres":["Drama"],"IsMovie":true,"IsNews":false,"IsSports":false,"MetadataGenerator":"Channels DVR","OriginalBroadcastDateTime":"1992-12-11T00:00:00Z","RecordedDateTime":"2021-05-23T03:00:00Z","Season":0,"SubTitle":"","Title":"A Few Good Men (1992)"}

My Simple Script

Code:
#! /usr/bin/python

import json

json_file = 'c:/Python/A Few Good Men (1992) 2021-05-22-1959.json'

with open(json_file) as json_data:
    data = json.load(json_data)


print (data['Title'], file=open('c:/python/output.txt', 'w'))
print (data['Description'], file=open('c:/python/output.txt', 'a'))
print (data['Genres'], file=open('c:/python/output.txt', 'a'))
print (data['OriginalBroadcastDateTime'], file=open('c:/python/output.txt', 'a'))
Output

Code:
A Few Good Men (1992)
Navy lawyers (Tom Cruise, Demi Moore) defend Marines for a murder at Gitmo.
['Drama']
1992-12-11T00:00:00Z
Would like this output ...

Code:
Title=A Few Good Men (1992)
Year=1992
Genre=Movie/Drama
Description=Navy lawyers (Tom Cruise, Demi Moore) defend Marines for a murder at Gitmo.
Mediatype=Movie
__________________
Channels DVR UBUNTU Server 2 Primes 3 Connects TVE SageTV Docker with input from Channels DVR XMLTV and M3U VIA Opendct.

Last edited by nyplayer; 05-23-2021 at 09:10 AM.
Reply With Quote
  #2  
Old 05-23-2021, 09:05 AM
nyplayer nyplayer is offline
SageTVaholic
 
Join Date: Sep 2005
Posts: 4,997
Well I am getting there ....

Code:
#! /usr/bin/python

import json

json_file = 'c:/Python/A Few Good Men (1992) 2021-05-22-1959.json'

with open(json_file) as json_data:
    data = json.load(json_data)


print ('Title=',data['Title'], file=open('c:/python/output.txt', 'w'))
print ('Description=',data['Description'], file=open('c:/python/output.txt', 'a'))
print ('Genre=',data['Genres'], file=open('c:/python/output.txt', 'a'))
print ('Year=',data['OriginalBroadcastDateTime'], file=open('c:/python/output.txt', 'a'))
Code:
Title= A Few Good Men (1992)
Description= Navy lawyers (Tom Cruise, Demi Moore) defend Marines for a murder at Gitmo.
Genre= ['Drama']
Year= 1992-12-11T00:00:00Z
__________________
Channels DVR UBUNTU Server 2 Primes 3 Connects TVE SageTV Docker with input from Channels DVR XMLTV and M3U VIA Opendct.
Reply With Quote
  #3  
Old 05-23-2021, 10:18 AM
wnjj wnjj is offline
Sage Icon
 
Join Date: Jan 2009
Posts: 1,514
I’ve never really done any Python code but try:

Code:
#! /usr/bin/python

import json

json_file = 'c:/Python/A Few Good Men (1992) 2021-05-22-1959.json'

with open(json_file) as json_data:
    data = json.load(json_data)

json_data.close()

outfile=open('c:/python/output.txt', 'w')

print (“Title={}”.format(data['Title']), file=outfile)
print (“Description={}”.format(data['Description']), file=outfile)
print (“Genre={}”.format(data['Genres']), file=outfile)
print (“Year={}”.format(data['OriginalBroadcastDateTime']), file=outfile)

outfile.close()
__________________
Windows Installer

Last edited by wnjj; 05-23-2021 at 10:22 AM.
Reply With Quote
  #4  
Old 05-23-2021, 10:39 AM
nyplayer nyplayer is offline
SageTVaholic
 
Join Date: Sep 2005
Posts: 4,997
That is Great this is my output from your script. .. I just have to manipulate the year and genre somehow .... I am just studying Python thanks for your help.


Code:
Title=A Few Good Men (1992)
Description=Navy lawyers (Tom Cruise, Demi Moore) defend Marines for a murder at Gitmo.
Genre=['Drama']
Year=1992-12-11T00:00:00Z
Working on getting it like this ..

Genre=Drama
Year=1992
__________________
Channels DVR UBUNTU Server 2 Primes 3 Connects TVE SageTV Docker with input from Channels DVR XMLTV and M3U VIA Opendct.

Last edited by nyplayer; 05-23-2021 at 10:42 AM.
Reply With Quote
  #5  
Old 05-23-2021, 11:18 AM
graywolf's Avatar
graywolf graywolf is offline
Sage Icon
 
Join Date: Oct 2009
Location: NC
Posts: 1,389
haven't tested but you might try:


#! /usr/bin/python

import json
import datetime

json_file = 'c:/Python/A Few Good Men (1992) 2021-05-22-1959.json'

with open(json_file) as json_data:
data = json.load(json_data)

json_data.close()

outfile=open('c:/python/output.txt', 'w')
.strftime(current_day, "%m/%d/%Y")
print (“Title={}”.format(data['Title']), file=outfile)
print (“Description={}”.format(data['Description']), file=outfile)
print (“Genre={}”.format(data['Genres'].replace("'[]","")), file=outfile)
print (“Year={}”format(datetime.date..strftime(data['OriginalBroadcastDateTime'],"%Y-%m-%d")), file=outfile)

outfile.close()
Reply With Quote
  #6  
Old 05-23-2021, 12:36 PM
nyplayer nyplayer is offline
SageTVaholic
 
Join Date: Sep 2005
Posts: 4,997
Quote:
Originally Posted by graywolf View Post
haven't tested but you might try:


#! /usr/bin/python

import json
import datetime

json_file = 'c:/Python/A Few Good Men (1992) 2021-05-22-1959.json'

with open(json_file) as json_data:
data = json.load(json_data)

json_data.close()

outfile=open('c:/python/output.txt', 'w')
.strftime(current_day, "%m/%d/%Y")
print (“Title={}”.format(data['Title']), file=outfile)
print (“Description={}”.format(data['Description']), file=outfile)
print (“Genre={}”.format(data['Genres'].replace("'[]","")), file=outfile)
print (“Year={}”format(datetime.date..strftime(data['OriginalBroadcastDateTime'],"%Y-%m-%d")), file=outfile)

outfile.close()

I think the formatting got messed up when posted .... but I will research the replace.

Code:
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> #! /usr/bin/python
>>>
>>> import json
>>> import datetime
>>>
>>> json_file = 'c:/Python/A Few Good Men (1992) 2021-05-22-1959.json'
>>>
>>> with open(json_file) as json_data:
... data = json.load(json_data)
  File "<stdin>", line 2
    data = json.load(json_data)
    ^
IndentationError: expected an indented block
>>>
>>> json_data.close()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'json_data' is not defined
>>>
>>> outfile=open('c:/python/output.txt', 'w')
>>> .strftime(current_day, "%m/%d/%Y")
  File "<stdin>", line 1
    .strftime(current_day, "%m/%d/%Y")
    ^
SyntaxError: invalid syntax
>>> print ("Title={}".format(data['Title']), file=outfile)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
__________________
Channels DVR UBUNTU Server 2 Primes 3 Connects TVE SageTV Docker with input from Channels DVR XMLTV and M3U VIA Opendct.
Reply With Quote
  #7  
Old 05-23-2021, 01:05 PM
stuckless's Avatar
stuckless stuckless is offline
SageTVaholic
 
Join Date: Oct 2007
Location: London, Ontario, Canada
Posts: 9,713
Because Genres is typically an array.. you might try...

Code:
print (“Genre={}”.format(",".join(data['Genres'])), file=outfile)
Which should print out a comma separated list of Genres (which is what the props can read, if I recall... it's either "," or ";" forget which.
Reply With Quote
  #8  
Old 05-23-2021, 02:01 PM
wnjj wnjj is offline
Sage Icon
 
Join Date: Jan 2009
Posts: 1,514
Quote:
Originally Posted by stuckless View Post
Because Genres is typically an array.. you might try...

Code:
print (“Genre={}”.format(",".join(data['Genres'])), file=outfile)
Which should print out a comma separated list of Genres (which is what the props can read, if I recall... it's either "," or ";" forget which.
Or would an access work? data['Genres'][0]
__________________
Windows Installer
Reply With Quote
  #9  
Old 05-23-2021, 02:07 PM
stuckless's Avatar
stuckless stuckless is offline
SageTVaholic
 
Join Date: Oct 2007
Location: London, Ontario, Canada
Posts: 9,713
Quote:
Originally Posted by wnjj View Post
Or would an access work? data['Genres'][0]
I suspect so, IF you only wanted the first one. But, in Sage properties, the Genre field is an array field and if multiple genres are listed then the video gets put into each genre. So, I would think you'd want to specify all possible genres. But if not, then [0] should get the first one.
Reply With Quote
  #10  
Old 05-23-2021, 02:07 PM
wnjj wnjj is offline
Sage Icon
 
Join Date: Jan 2009
Posts: 1,514
For the year, split should work:

Code:
print (“Genre={}”.format(data['Genres'][0]), file=outfile)
print (“Year={}”.format(data['OriginalBroadcastDateTime'].split(“-“)[0]), file=outfile)
I’m not sure on the Genres but if it’s just an array that should work to give you the first item. If you want more than one genre to show up, there are ways to turn lists into text with separators.
__________________
Windows Installer

Last edited by wnjj; 05-23-2021 at 02:09 PM.
Reply With Quote
  #11  
Old 05-23-2021, 03:37 PM
graywolf's Avatar
graywolf graywolf is offline
Sage Icon
 
Join Date: Oct 2009
Location: NC
Posts: 1,389
also, python is very format/indentation specific

So the actions that you want taken with you "with open" need to be indented

with open(json_file) as json_data:
data = json.load(json_data)

also, when you use "with open" you don't need to do the close
Reply With Quote
  #12  
Old 05-23-2021, 04:39 PM
emveepee emveepee is offline
Sage Aficionado
 
Join Date: Nov 2006
Posts: 417
I'm a regex fan and don't trust the original broadcast date to be the film release year, perhaps channels is non standard.

Code:
import re
title = 'A Few Good Men (1992)'
year = re.search(r'\(([12]\d{3})\)$', title)
if year != None:
    print (year.group(1))
Martin
Reply With Quote
  #13  
Old 05-23-2021, 04:44 PM
nyplayer nyplayer is offline
SageTVaholic
 
Join Date: Sep 2005
Posts: 4,997
Thanks everyone ... some do have multiple Genres ... I have learned a lot today from you guys thanks for your time.

Code:
#! /usr/bin/python

import json

json_file = 'c:/Python/A Few Good Men (1992) 2021-05-22-1959.json'

with open(json_file) as json_data:
    data = json.load(json_data)

json_data.close()

outfile=open('c:/python/output.txt', 'w')

print ("Title={}".format(data['Title']), file=outfile)
print ("Description={}".format(data['Description']), file=outfile)
print ("Genre={}".format(",".join(data['Genres'])), file=outfile)
print ("Year={}".format(data['OriginalBroadcastDateTime'].split("-")[0]), file=outfile)

outfile.close()
output

Code:
Title=A Few Good Men (1992)
Description=Navy lawyers (Tom Cruise, Demi Moore) defend Marines for a murder at Gitmo.
Genre=Drama
Year=1992
__________________
Channels DVR UBUNTU Server 2 Primes 3 Connects TVE SageTV Docker with input from Channels DVR XMLTV and M3U VIA Opendct.

Last edited by nyplayer; 05-23-2021 at 04:49 PM.
Reply With Quote
  #14  
Old 05-23-2021, 04:46 PM
wnjj wnjj is offline
Sage Icon
 
Join Date: Jan 2009
Posts: 1,514
Quote:
Originally Posted by stuckless View Post
I suspect so, IF you only wanted the first one. But, in Sage properties, the Genre field is an array field and if multiple genres are listed then the video gets put into each genre. So, I would think you'd want to specify all possible genres. But if not, then [0] should get the first one.
I’m a little slow today. I actually read what you wrote more carefully and your suggestion makes the CSV list. Seems like the right way to go there.
__________________
Windows Installer
Reply With Quote
  #15  
Old 05-23-2021, 04:58 PM
nyplayer nyplayer is offline
SageTVaholic
 
Join Date: Sep 2005
Posts: 4,997
Quote:
Originally Posted by stuckless View Post
Because Genres is typically an array.. you might try...

Code:
print (“Genre={}”.format(",".join(data['Genres'])), file=outfile)
Which should print out a comma separated list of Genres (which is what the props can read, if I recall... it's either "," or ";" forget which.
Thanks that works I looked at a properties from CMT and the seperator is a "/" and it works many thanks.
__________________
Channels DVR UBUNTU Server 2 Primes 3 Connects TVE SageTV Docker with input from Channels DVR XMLTV and M3U VIA Opendct.
Reply With Quote
  #16  
Old 05-23-2021, 06:05 PM
nyplayer nyplayer is offline
SageTVaholic
 
Join Date: Sep 2005
Posts: 4,997
I created one for TV Shows and it also works great ....

Code:
print ("Title={}".format(data['Title']), file=outfile)
print ("EpisodeName={}".format(data['SubTitle']), file=outfile)
print ("SeasonNumber={}".format(data['Season']), file=outfile)
print ("EpisodeNumber={}".format(data['Episode']), file=outfile)
print ("Description={}".format(data['Description']), file=outfile)
print ("Genre={}".format("/".join(data['Genres'])), file=outfile)
print ("Year={}".format(data['OriginalBroadcastDateTime'].split("-")[0]), file=outfile)
outfile.close()
Code:
Title=Law & Order: Criminal Intent
EpisodeName=Three-in-One
SeasonNumber=9
EpisodeNumber=16
Description=When a real estate agent's body is discovered, Nichols must rely on his father.
Genre=Crime drama/Docudrama
Year=2010
__________________
Channels DVR UBUNTU Server 2 Primes 3 Connects TVE SageTV Docker with input from Channels DVR XMLTV and M3U VIA Opendct.

Last edited by nyplayer; 05-23-2021 at 06:08 PM.
Reply With Quote
  #17  
Old 05-24-2021, 05:08 PM
nyplayer nyplayer is offline
SageTVaholic
 
Join Date: Sep 2005
Posts: 4,997
This is the way I run it using good old DIRMON2 ... target JSON must exist MPG and must not exist mpg.properties. Thanks to all that contributed.

my batch file to call the Scripts.

channels2sageFinal.bat
Code:
set filein=%1
findstr /c:"IsMovie\":true," %filein% %filein%
Set isMovie=%ERRORLEVEL%

if "%~x1" EQU ".json" set propout=%filein:.json=.mpg.properties%
copy "%~f1" C:\Python\channels2sage.json
if %isMovie% EQU 0 python.exe c:\Python\sageMovie.py
if %isMovie% NEQ 0 python.exe c:\Python\sageTV.py
copy c:\Python\channels2sage.properties %propout%
exit

sageMovie.py
Code:
#! /usr/bin/python

import json

json_file = 'C:\Python\channels2sage.json'

with open(json_file) as json_data:
    data = json.load(json_data)

json_data.close()

outfile=open('c:/python/channels2sage.properties', 'w')

print ("Title={}".format(data['Title']), file=outfile)
print ("Description={}".format(data['Description']), file=outfile)
print ("Genre=Movie/{}".format("/".join(data['Genres'])), file=outfile)
print ("Year={}".format(data['OriginalBroadcastDateTime'].split("-")[0]), file=outfile)
outfile.close()

sageTV.py
Code:
#! /usr/bin/python

import json

json_file = 'C:\Python\channels2sage.json'

with open(json_file) as json_data:
    data = json.load(json_data)

json_data.close()

outfile=open('c:/python/channels2sage.properties', 'w')

print ("Title={}".format(data['Title']), file=outfile)
print ("EpisodeName={}".format(data['SubTitle']), file=outfile)
print ("SeasonNumber={}".format(data['Season']), file=outfile)
print ("EpisodeNumber={}".format(data['Episode']), file=outfile)
print ("Description={}".format(data['Description']), file=outfile)
print ("Genre={}".format("/".join(data['Genres'])), file=outfile)
print ("Year={}".format(data['OriginalBroadcastDateTime'].split("-")[0]), file=outfile)
outfile.close()
Results Movie
Code:
Title=Tracking a Killer (2021)
Description=A woman tries to prove her daughter's innocence after police accuse her of murdering a friend.
Genre=Movie/Thriller/Mystery
Year=2021
Results TVShow
Code:
Title=Star Trek: The Next Generation
EpisodeName=Chain of Command
SeasonNumber=6
EpisodeNumber=11
Description=Jellico and the Enterprise try to rescue Picard from Cardassian torture.
Genre=Science fiction/Fantasy/Adventure
Year=1992
__________________
Channels DVR UBUNTU Server 2 Primes 3 Connects TVE SageTV Docker with input from Channels DVR XMLTV and M3U VIA Opendct.

Last edited by nyplayer; 05-24-2021 at 05:25 PM.
Reply With Quote
  #18  
Old 02-21-2022, 08:15 PM
boukmandutty boukmandutty is offline
Sage Advanced User
 
Join Date: Oct 2011
Location: Norman OK
Posts: 199
How do you manage the opposite of this? Do you have a script to automatically import sagetv library into channels dvr for outside streaming?
Reply With Quote
  #19  
Old 02-21-2022, 10:42 PM
nyplayer nyplayer is offline
SageTVaholic
 
Join Date: Sep 2005
Posts: 4,997
Quote:
Originally Posted by boukmandutty View Post
How do you manage the opposite of this? Do you have a script to automatically import sagetv library into channels dvr for outside streaming?
MCEBUDDY is what I use to rename Sage Recordings ... SageTV is the worst as far as naming convention. Plus it puts everything in 1 Folder. Any series I want to keep I use EMBY because it organizes them quite nicely and they finally added a comskip plugin. I am trying to streamline the process and getting away from renaming and transcoding recordings. It is much easier to import recordings into SageTV then it is to import Sage recordings to other APPS.
__________________
Channels DVR UBUNTU Server 2 Primes 3 Connects TVE SageTV Docker with input from Channels DVR XMLTV and M3U VIA Opendct.

Last edited by nyplayer; 02-21-2022 at 11:01 PM.
Reply With Quote
  #20  
Old 02-22-2022, 12:05 AM
boukmandutty boukmandutty is offline
Sage Advanced User
 
Join Date: Oct 2011
Location: Norman OK
Posts: 199
I wonder if there is a way to adjust the Plex Scanner to do this for Channels dvr.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 02:32 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.
Copyright 2003-2005 SageTV, LLC. All rights reserved.