Python Forum
Refresh data in python script while running in Terminal
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Refresh data in python script while running in Terminal
#1
Hi,

I have gotten a script to pull in new helpdesk tickets from Zendesk and display then in a terminal window (Ubuntu). It pulls the information without any issue but what I need to it do is refresh the data in the same session so for every minute, it will display any new tickets that have been requested by staff.

This is the code that I have that works:
from urllib.parse import urlencode

import requests
import os
os.system('clear')

credentials = 'zendesk_email' + '/token', 'zendesk_token'
session = requests.Session()
session.auth = credentials

params = {
    'query': 'type:ticket status:new',
    'sort_by': 'created_at',
    'sort_order': 'desc'
}

url = 'https://subdomain.zendesk.com/api/v2/search.json?' + urlencode(params)
response = session.get(url)
if response.status_code != 200:
    print('Status:', response.status_code, 'Problem with the request. Exiting.')
    exit()

# Print the subject of each ticket in the results
data = response.json()
for result in data['results']:
    print(result['subject'])
Now, I have played around with a couple of ways to refresh the content in the script in real time. I entered these at the bottom of the above coding in the same script. One was to reload the script as a module:
import open_tickets
import importlib
import time
importlib.reload(open_tickets)
time.sleep(60)
Now that sort of worked, as it would reload the script, but it wouldn't update the content when a new ticket is requested by a user.

Another method I hacked together was while True statement, something along the lines of:
while True:
  print(result['subject'])
  time.sleep(60)
  os.system('clear')
I need the script to reload the data automatically and in real time and display any new helpdesk ticket everytime one has been create.

I hope this makes sense and helps.

Cheers
Reply
#2
You need to use an event timer. It will run independent of other precesses, and create an event (interrupt) when the terminal time is reached.

here's a quick example:
import threading, time


def do_work():
    print("OK ... I'm doing my work here")   

def testit(seconds):
    timer = threading.Event()
    while True:
        print(f"delay {seconds} seconds")
        timer.wait(seconds)
        do_work()


if __name__ == '__main__':
    testit(2)
sample output:
Output:
delay 2 seconds OK ... I'm doing my work heredelay 2 seconds OK ... I'm doing my work here delay 2 seconds OK ... I'm doing my work here delay 2 seconds OK ... I'm doing my work here delay 2 seconds delay 2 seconds OK ... I'm doing my work here delay 2 seconds OK ... I'm doing my work here delay 2 seconds
Reply
#3
Hi Larz60+,

Thanks for the reply, appreciate the help.

I have used the code you suggested and have put it at the bottom of my script like so:

import threading, time

def do_work():
    os.system('clear')
    data = response.json()
    for result in data['results']:
        print(result['subject'])

def testit(seconds):
    timer = threading.Event()
    while True:
        print(f"delay {seconds} seconds")
        timer.wait(5)
        do_work()

if __name__ == '__main__':
    testit(5)
I added the clear code so that it doesn't keep listing the tickets like in your sample output. I started running the script with your code and it did work and pulled the tickets from zendesk and displayed them.

However, when I submitted a new test ticket to zendesk, the python script didn't pick up the new ticket and display it. It just kept displaying the list of tickets that were there before I submitted the test ticket.

This is what the output looks like before and after submitting a new ticket with the new code:
Output:
new app test [SEC=OFFICIAL] App test [SEC=OFFICIAL] RE: Request for Secure USB [SEC=OFFICIAL] delay 5 seconds
Cheers
Reply
#4
Never mind, I had a play around and managed to figure it out. This is what the final code looks like:

from urllib.parse import urlencode
import requests
import os
import threading, time

def do_work():

 os.system('clear')

 credentials = 'zendesk_email' + '/token', 'zendesk_api_token'
 session = requests.Session()
 session.auth = credentials

 params = {
    'query': 'type:ticket status:new',
    'sort_by': 'created_at',
    'sort_order': 'desc'
 }

 url = 'https://subdomain.zendesk.com/api/v2/search.json?' + urlencode(params)
 response = session.get(url)
 if response.status_code != 200:
    print('Status:', response.status_code, 'Problem with the request. Exiting.')
    exit()

# Print the subject of each ticket in the results
 data = response.json()
 for result in data['results']:
    print(result['subject'])

def testit(seconds):
    timer = threading.Event()
    while True:
        #print(f"delay {seconds} seconds")
        timer.wait(30)
        do_work()

if __name__ == '__main__':
    testit(30)
Now it refreshes every 30 seconds and pulls in the new tickets when they arrive.

Cheers again for the help.
Reply
#5
Glad to hear it. Also, the function names that I used were from writing code quickly.
Probably not the best choice, so please feel free to change.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No Internet connection when running a Python script basil_555 8 589 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 477 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Help Running Python Script in Mac OS emojistickers 0 345 Nov-20-2023, 01:58 PM
Last Post: emojistickers
  Trying to make a board with turtle, nothing happens when running script Quascia 3 670 Nov-01-2023, 03:11 PM
Last Post: deanhystad
  invoking python in Terminal Euler 2 628 Aug-25-2023, 06:17 AM
Last Post: perfringo
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,235 Jun-29-2023, 11:57 AM
Last Post: gologica
  script to calculate data in csv-files ledgreve 0 1,098 May-19-2023, 07:24 AM
Last Post: ledgreve
  Python script running under windows over nssm.exe JaroslavZ 0 708 May-12-2023, 09:22 AM
Last Post: JaroslavZ
  googletrans library to translate text language for using data frame is not running gcozba2023 0 1,226 Mar-06-2023, 09:50 AM
Last Post: gcozba2023
  Launch Python IDLE Shell from terminal Pavel_47 5 1,232 Feb-17-2023, 02:53 PM
Last Post: Pavel_47

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020