Advertisement
Guest User

Untitled

a guest
Mar 13th, 2024
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 KB | None | 0 0
  1. ------------ User ------------
  2.  
  3. Write a python functions that will:
  4.  
  5. - get json data from this url: https://daga123-tv-api.onrender.com/getPrograms
  6. - Here is a sample of what is returned:
  7.  
  8. {
  9.    "data":[
  10.       {
  11.          "id":"TF1.fr",
  12.          "name":"TF1",
  13.          "icon":"https://www.teleboy.ch/assets/stations/308/icon320_light.png?v2023_48_0",
  14.          "programs":[...]
  15.       },
  16.       {
  17.          "id":"France2.fr",
  18.          "name":"France 2",
  19.          "icon":"https://www.teleboy.ch/assets/stations/342/icon320_light.png?v2023_48_0",
  20.          "programs":[...]
  21.       },
  22.       ...]
  23. }
  24.  
  25. And a sample of "programs":
  26.  
  27. [
  28.             {
  29.                "name":"TFou",
  30.                "start":1710223200,
  31.                "end":1710226500,
  32.                "channel":"TF1.fr",
  33.                "icon":"https://proxymedia.woopic.com/api/v1/images/1870%2Fepg%2Fcontainere29844d933654d77866e49aa5ed2bf2bb7a7414bf6dd26773441c91f43a0d1d1ff5c25b5_F51DAFA904E2D64C2D945C6CFF3856D6.jpg",
  34.                "rating":"Tout public",
  35.                "cat":"Jeunesse",
  36.                "desc":"Une émission jeunesse qui propose les meilleures séries de dessins animés du moment."
  37.             },
  38.             {
  39.                "name":"Bonjour ! La Matinale TF1",
  40.                "start":1710226500,
  41.                "end":1710236100,
  42.                "channel":"TF1.fr",
  43.                "icon":"https://proxymedia.woopic.com/api/v1/images/1870%2Fepg%2Fcontainere29844d933654d77866e49aa5ed2bf2bbb63f341db64d102dc533df2d894f881846a66f0_7EA6E517861B43FA54B0E44C718BC529.jpg",
  44.                "rating":"Tout public",
  45.                "cat":"Magazine",
  46.                "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."
  47.             },
  48.             ...
  49. ]
  50.  
  51. what you should do:
  52. save a pretty printed json to a file of this payload that should only include:
  53. - for elements under 'data':
  54.     - keep only the 'id', 'name' and 'programs' key
  55. - for elements under 'program':
  56.     - only keep programs whose start key is after the current time (unix stimestamps)
  57.     - only keep those keys:
  58.         - name
  59.         - start
  60.         - end
  61.         - channel
  62.         - cat
  63.         - desc
  64.  
  65. - 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
  66.  
  67. ------------ Assistant ------------
  68.  
  69. Here's a Python function that meets your requirements:
  70.  
  71. ```python
  72. import requests
  73. import json
  74. import time
  75. import os
  76.  
  77. def retrieve_tv_program():
  78.    filename = 'tv_program.json'
  79.    timestamp_filename = 'last_run_timestamp.txt'
  80.    current_time = time.time()
  81.    
  82.    # Check if function was called less than 24 hours ago
  83.    if os.path.exists(timestamp_filename):
  84.        with open(timestamp_filename, 'r') as f:
  85.            last_run_timestamp = float(f.read())
  86.            if current_time - last_run_timestamp < 86400: # 24 hours
  87.                with open(filename, 'r') as f:
  88.                    return json.load(f)
  89.    
  90.    # Call the API
  91.    response = requests.get("https://daga123-tv-api.onrender.com/getPrograms")
  92.    data = response.json()['data']
  93.    
  94.    filtered_data = []
  95.    for element in data:
  96.        filtered_programs = [program for program in element['programs'] if program['start'] > current_time]
  97.        filtered_programs = [{k: v for k, v in program.items() if k in ['name', 'start', 'end', 'channel', 'cat', 'desc']} for program in filtered_programs]
  98.        filtered_element = {'id': element['id'], 'name': element['name'], 'programs': filtered_programs}
  99.        filtered_data.append(filtered_element)
  100.    
  101.    result = {'data': filtered_data}
  102.    
  103.    # Write the result to the file
  104.    with open(filename, 'w') as f:
  105.        json.dump(result, f, indent=4)
  106.    
  107.    # Update the timestamp
  108.    with open(timestamp_filename, 'w') as f:
  109.        f.write(str(current_time))
  110.    
  111.    return result
  112.  
  113. # Call the function and pretty-print the result (for demonstration purposes)
  114. if __name__ == '__main__':
  115.    print(json.dumps(retrieve_tv_program(), indent=4))
  116. ```
  117.  
  118. This script does the following:
  119. - Checks
  120.  
  121. ------------ Usage ------------
  122.  
  123. Last message: 0.01916M with gpt-4-0125-preview
  124. Today:        0.08207M
  125. This month:   3.3737880M
  126.  
  127. ------------ User ------------
  128.  
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement