Skip to content

Instantly share code, notes, and snippets.

@djkrose
Created April 11, 2023 06:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djkrose/7e43543539aee5bd2d048270fb4d814d to your computer and use it in GitHub Desktop.
Save djkrose/7e43543539aee5bd2d048270fb4d814d to your computer and use it in GitHub Desktop.
Randomly pics an image from a folder and uploads to The Frame
# Required for installation:
# - https://github.com/xchwarze/samsung-tv-ws-api
# pip3 install samsungtvws[async,encrypted]
# - https://pypi.org/project/wakeonlan/
# pip3 install wakeonlan
import os
import sys
import logging
import random
import time
from datetime import datetime
from samsungtvws import SamsungTVWS
from wakeonlan import send_magic_packet
TV_IP = '192.168.2.100' # IP of your Samsung The Frame
TV_MAC = 'AB.CD.EF.12.35.56' # MAC address of your Samsung The Frame (only for wake-on-lan)
INTERFACE = '192.168.2.109' # IP of your network adapter (only for wake-on-lan)
FOLDER = 'D:/Images/The Frame' #
DELETE_PREVIOUS_IMAGE = True
WAKE_ON_LAN = True
MATTE = 'none'
RETRIES = 5
# Returns True if the given image content_id is marked as favorite in TV
def is_favorite(content_id):
# Sometimes fails randomly; retry it a few times
retry = RETRIES
all = []
while retry > 0:
try:
print("Trying to get all artwork from TV ...")
all = tv.art().available()
retry = 0
except:
retry = retry -1
if retry == 0:
print("FATAL ERROR: Could not get all artwork from TV after " + RETRIES + " tries.")
sys.exit(1)
else:
print("Error while trying to get all artwork. Retrying ...")
time.sleep(5)
for image in all:
if image['content_id'] == content_id and image['category_id'] == 'MY-C0004':
return True
return False
# Uploads given PNG or JPG file and sets it as new artwork
def set_new_image(image):
# Determine image type
if image.endswith('.jpg'):
file_type = 'JPEG'
else:
file_type = 'PNG'
# Upload to TV
print("Uploading " + file_type + " image " + image + " to TV ...")
file = open(image, 'rb')
data = file.read()
new_id = str(tv.art().upload(data, matte=MATTE, file_type=file_type))
print("Image uploaded successfully as " + new_id + ".")
# Set new image art; only show immediately if currently in art mode
artmode = tv.art().get_artmode()
if artmode == 'on':
print("Art mode is currently on, so showing image immediately.")
tv.art().select_image(new_id, show=True)
else:
print("Art mode is currently off, so just setting image in background without showing it now.")
tv.art().select_image(new_id)
# MAIN PROGRAM -----------------------------------------------------------------------------------------
print("-------------[", datetime.now(), "]------------")
print("Samsung The Frame Art Rotator v1.0")
# Logger for debugging
logging.basicConfig(level=logging.INFO)
# Define TV connection
tv = SamsungTVWS('192.168.2.100')
# Test connection and art mode availability
retry = RETRIES
supported = False;
try:
print("Trying to get art mode status from TV ...")
supported = bool(tv.art().supported())
except:
retry = retry - 1
if retry == 0:
print("FATAL ERROR: Could not get art mode status. Maybe TV is off?")
sys.exit(1)
else:
print("Error while trying to get artmode status. Retrying ...")
# Wake up TV in case it is fully shut down (3 sec off button)
if WAKE_ON_LAN:
print("Sending wake-on-lan (WOL) package to TV, just in case it is off ...")
send_magic_packet(TV_MAC, interface=INTERFACE)
time.sleep(15)
time.sleep(5)
if not supported:
print('FATAL ERROR: Art mode is not supported or connection not ready.')
sys.exit(1)
# Store current image for deletion later
if DELETE_PREVIOUS_IMAGE:
print("Looking up current image ...")
old_id = str(tv.art().get_current()['content_id'])
print("Current image " + old_id + " remembered for deleting later.")
if not old_id.startswith('MY_'):
print("Image is not custom, so skipping deletion.")
DELETE_PREVIOUS_IMAGE = False
if is_favorite(old_id):
print("Image is marked as favorite, so skipping deletion.")
DELETE_PREVIOUS_IMAGE = False
# Load all images
images = []
for root, dirs, files in os.walk(FOLDER):
for file in files:
if file.endswith('.jpg') or file.endswith('.png'):
images.append(os.path.join(root, file))
# Choose random image
image = random.choice(images)
print("New image selected: " + image)
set_new_image(image)
if DELETE_PREVIOUS_IMAGE:
print("Deleting old image " + old_id + " from TV ...")
tv.art().delete(old_id)
print("Old image deleted.")
print("All done. Enyou your new artwork!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment