Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------ User ------------
- Write a python functions that will:
- - get json data from this url: https://daga123-tv-api.onrender.com/getPrograms
- - Here is a sample of what is returned:
- {
- "data":[
- {
- "id":"TF1.fr",
- "name":"TF1",
- "icon":"https://www.teleboy.ch/assets/stations/308/icon320_light.png?v2023_48_0",
- "programs":[...]
- },
- {
- "id":"France2.fr",
- "name":"France 2",
- "icon":"https://www.teleboy.ch/assets/stations/342/icon320_light.png?v2023_48_0",
- "programs":[...]
- },
- ...]
- }
- And a sample of "programs":
- [
- {
- "name":"TFou",
- "start":1710223200,
- "end":1710226500,
- "channel":"TF1.fr",
- "icon":"https://proxymedia.woopic.com/api/v1/images/1870%2Fepg%2Fcontainere29844d933654d77866e49aa5ed2bf2bb7a7414bf6dd26773441c91f43a0d1d1ff5c25b5_F51DAFA904E2D64C2D945C6CFF3856D6.jpg",
- "rating":"Tout public",
- "cat":"Jeunesse",
- "desc":"Une émission jeunesse qui propose les meilleures séries de dessins animés du moment."
- },
- {
- "name":"Bonjour ! La Matinale TF1",
- "start":1710226500,
- "end":1710236100,
- "channel":"TF1.fr",
- "icon":"https://proxymedia.woopic.com/api/v1/images/1870%2Fepg%2Fcontainere29844d933654d77866e49aa5ed2bf2bbb63f341db64d102dc533df2d894f881846a66f0_7EA6E517861B43FA54B0E44C718BC529.jpg",
- "rating":"Tout public",
- "cat":"Magazine",
- "desc":"\"Bonjour ! La Matinale TF1\" est le premier rendez-vous d'information de la journée. Bruce Toussaint fait aussi intervenir les correspondants en direct en région."
- },
- ...
- ]
- what you should do:
- save a pretty printed json to a file of this payload that should only include:
- - for elements under 'data':
- - keep only the 'id', 'name' and 'programs' key
- - for elements under 'program':
- - only keep programs whose start key is after the current time (unix stimestamps)
- - only keep those keys:
- - name
- - start
- - end
- - channel
- - cat
- - desc
- - Additionnaly, you should dump the last time this function was run to a file (a a unix timestamp). If the retrieve_tv_program function was called less than 24 hours ago, just return the json as stored in the file, otherwise call the api link I provided, store it on the disk, and return the json
- ------------ Assistant ------------
- Here's a Python function that meets your requirements:
- ```python
- import requests
- import json
- import time
- import os
- def retrieve_tv_program():
- filename = 'tv_program.json'
- timestamp_filename = 'last_run_timestamp.txt'
- current_time = time.time()
- # Check if function was called less than 24 hours ago
- if os.path.exists(timestamp_filename):
- with open(timestamp_filename, 'r') as f:
- last_run_timestamp = float(f.read())
- if current_time - last_run_timestamp < 86400: # 24 hours
- with open(filename, 'r') as f:
- return json.load(f)
- # Call the API
- response = requests.get("https://daga123-tv-api.onrender.com/getPrograms")
- data = response.json()['data']
- filtered_data = []
- for element in data:
- filtered_programs = [program for program in element['programs'] if program['start'] > current_time]
- filtered_programs = [{k: v for k, v in program.items() if k in ['name', 'start', 'end', 'channel', 'cat', 'desc']} for program in filtered_programs]
- filtered_element = {'id': element['id'], 'name': element['name'], 'programs': filtered_programs}
- filtered_data.append(filtered_element)
- result = {'data': filtered_data}
- # Write the result to the file
- with open(filename, 'w') as f:
- json.dump(result, f, indent=4)
- # Update the timestamp
- with open(timestamp_filename, 'w') as f:
- f.write(str(current_time))
- return result
- # Call the function and pretty-print the result (for demonstration purposes)
- if __name__ == '__main__':
- print(json.dumps(retrieve_tv_program(), indent=4))
- ```
- This script does the following:
- - Checks
- ------------ Usage ------------
- Last message: 0.01916M with gpt-4-0125-preview
- Today: 0.08207M
- This month: 3.3737880M
- ------------ User ------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement