How to make a python Script wait until download has finished - python

My Script downloads a zipfile, exctracts the relevant parts replaced files and folders etc. It used to work flawlessly, for some reason its has now decided to stop working and only partly downloads the zipfile, and of course, as the zipfile is incomplete I get an error, saying the downloaded file is not a zipfile. my script is as follows.
def downloadupdate():
xbmcgui.Dialog().ok(
"[B][COLOR white]Daily Updater[/B][/COLOR]",
"Favourites and some software will now update",
"Elements of your system will be cleaned",
"Daily Update will take at most 2 minutes")
#ONLY HAVE THE SUPER FAVOURITES FOLDER IN THE ZIPFILE!!
url = 'http://x.com/x/x/Super Favourites.zip'
destination = xbmc.translatePath('special://home/userdata/addon_data/iupdatefix/Super Favourites.zip')
favzip = urllib.urlopen(url)
xbmc.executebuiltin("Notification(Downloading new updates, PLEASE WAIT,()")
with open(xbmc.translatePath('special://home/userdata/addon_data/iupdatefix/Super Favourites.zip'), "wb") as zipFile:
zipFile.write(favzip.read())
xbmc.executebuiltin("Notification(Download Complete, Please wait,()")
time.sleep(5)
xbmc.executebuiltin("Notification(Updating Click and Play channels, Please wait,()")
updatezip = xbmc.translatePath('special://home/userdata/addon_data/iupdatefix/Super Favourites.zip')
extractupdate = xbmc.translatePath('special://home/userdata/addon_data/plugin.program.super.favourites/')
oldfav = xbmc.translatePath('special://home/userdata/addon_data/plugin.program.super.favourites/Super Favourites')
yeszip = os.path.exists(updatezip)
time.sleep(5)
if yeszip:
xbmc.executebuiltin("Notification(Removing previous, Please wait,()")
shutil.rmtree(oldfav, ignore_errors=False)
xbmc.executebuiltin("Notification(Updating, now,()")
gh = open(updatezip, 'rb')
zp = zipfile.ZipFile(gh)
zp.extractall(extractupdate)
gh.close()
time.sleep(3)
xbmc.executebuiltin("Notification(updated, Now Checking sd,()")
# put this command at the end of whole script --->>>os.remove(updatezip)
else:
xbmc.executebuiltin("Notification(Update file Corrupt, Please try again,()")

The problem has been solved, It was not the code. It was the hosting service, I had to reset the File permissions on the server and after that all works perfectly again.

Related

Google Vision Api in python stopped working - beginner coder alarm

I was interested in coding a long time. So this year I got a job where I was supposed to scrape text from old event programs. The pictures where terrible quality and results with normal OCR where horrible. I checked the google vision api and tested it manually and the results where great, so I used this opportunity to learn about coding.( I did some python before but the lack of usefulness always drove me away).
I wrote this program, I know its walking on crouches but it worked and did exactly what I wanted for 3 months now. I dont use it regularly but today when I wanted to use it again, it didnt work any more it just jumps to the end of the program, and finishes without making the api requests, at least so it seems to me.
I am really mostly finished with my work and this request doesnt make sense in terms of efficiency but I am just curious why the program I created suddenly stopped working.
If somebody can hint me in the right direction I would highly appreciate it, also if somebody wants to use the program, if it works for them sure let it work instead of yourself :D
I am not sure but I am using linux mint and maybe it stopped working because of some updates of python or vision-api or something.
# coding=utf-8
from google.cloud import vision
import os
import io
import sys
reload(sys)
sys.setdefaultencoding('utf8')
directory = "/home/weareone/Documents/programming/test/here"
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/home/weareone/Documents/programming/test/key.json"
def workinghard(page):
client = vision.ImageAnnotatorClient()
#file_name = os.path.join( os.path.dirname(__file__), page) # Loads the image into memory
#page_er = os.path.abspath(os.path.join(os.path.dirname(page))) <--- my improvisation
with io.open( page, 'rb') as image_file: # after io.open.(file_name <---exchanged with "page")
content = image_file.read()
request = {
"image": {
"content": content
},
"features": [
{
"type": "DOCUMENT_TEXT_DETECTION"
}
]
}
response = client.annotate_image(request)
storage = response.full_text_annotation.text
return storage
def listdirs(folder):
return [
d for d in (os.path.join(folder, d1) for d1 in os.listdir(folder))
if os.path.isdir(d)
]
directories = listdirs(directory)
for year in directories:
logtxt = open(year + ".txt", "w+" )
for root, dirs, files in os.walk(year):
files.sort()
for file23 in files:
if file23.endswith('.jpg'):
pathparent = os.path.join(year,file23)
logtxt.write(workinghard(pathparent))
logtxt.write("-------------------------------------------------------------------------")
print(pathparent)
logtxt.close()
print("DONE")
Thank you very much dear internet
EDIT: I solved it by changing this line the statement apaprently equaled to FALSE.
if file23.endswith('.JPG'):

How to recall previous output of python script

i am working on a telethon script of python which runs if the channel/group receives new message
i am looking at the message id for running my script
i am a beginner of python so with what knowledge i have
i am using this following code.
prev_msgid=0
latest_msgid = message.id
if latest_msgid>prev_msgid:
print('latest message')
prev_msgid = message.id
else:
print('old message')
but when i run this code every time the previous message resets to 0
i need a way for when i run this code multiple times the prev_msgid is automatically changed to the latest message id.
thank you.
like #Quba said you need a way to store data in persistent way
Pickle is the fastest solution for you. It can save python object as a file:
import pickle
from os import path
prev_msgid = 0
# check if saved
if path.exists("prev_msgid"):
# load
with open("prev_msgid", 'rb') as f:
prev_msgid = pickle.load(f)
prev_msgid += 1
# save
with open("prev_msgid", 'wb') as f:
pickle.dump(prev_msgid, f)
print(prev_msgid)
Every time you run the script it will add one to prev_msgid. See that it makes a file that named "prev_msgid"

Using win32com to download attachments through outlook with python

I've written a short code to download and rename files from a specific folder in my outlook account. The code works great, the only problem is that I typically need to run the code several times to actually download all of the messages. It seems the code is just failing to acknowledge some of the messages, there are no errors when I run through it.
I've tried a few things like walking through each line step by step in the python window, running the code with outlook closed or opened, and trying to print the files after they're successfully saved to see if there are specific messages that are causing the problem.
Here's my code
#! python3
# downloadAttachments.py - Downloads all of the weight tickets from Bucky
# Currently saves to desktop due to instability of I: drive connection
import win32com.client, os, re
#This line opens the outlook application
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
#Not exactly sure why the inbox is default folder 6 but it works
inbox = outlook.GetDefaultFolder(6)
#box where the messages are to save
TicketSave = inbox.Folders('WDE').Folders('SAVE').Folders('TicketSave')
#box where the messages are moved to
done = inbox.Folders('WDE').Folders('CHES').Folders('Weight Tickets')
ticketMessages = TicketSave.Items
#Key is used to verify the subject line is correct. This script only works if the person sends
# their emails with a consistent subject line (can be altered for other cases)
key = re.compile(r'wde load \d{3}') #requires regulars expressions (i.e. 'import re')
for message in ticketMessages:
#will skip any message that does not match the correct subject line format (non-case sensitive)
check = str(message.Subject).lower()
if key.search(check) == None:
continue
attachments = message.Attachments
tic = attachments.item(1)
ticnum = str(message.Subject).split()[2]
name = str(tic).split()[0] + ' ticket ' + ticnum + '.pdf' #changes the filename
tic.SaveAsFile('C:\\Users\\bhalvorson\\Desktop\\Attachments' + os.sep + str(name))
if message.UnRead == True:
message.UnRead = False
message.Move(done)
print('Ticket pdf: ' + name + ' save successfully')
Alright I found the answer to my own question. I'll post it here in case any other youngster runs into the same problem as me.
The main problem is the "message.Move(done)" second from the bottom.
Apparently the move function alters the current folder thus altering the number of loops that the for loop will go through. So, the way it's written above, the code only ever processes half of the items in the folder.
An easy work around is to switch the main line of the for loop to "for message in list(ticketMessages):" the list is not affected by the Move function and therefore you'll be able to loop through every message.
Hope this helps someone.

Syncronizing files with AJA Ki Pro using Python

We have an AJA Ki Pro recorder at another location and I a need to created an automated system that pulls the recorded files over to my editing studio. So far I have successfully been able to pull recordings using a python script run via an Applescript through Automator. I than can trigger the application from iCal. Basically my script involves setting the "MediaState" parameter on my recorder to "Data" (value=1) so I can pull files, comparing the files on the recorder to my local files (it only downloads what I dont already have locally), and then setting the "MediaState" property back to "Rec" (value=0) so the recorder is ready to go again.
Here are the 2 problems I have been unable to resolve so far. Bear with me, I have about 2 days worth of experience with Python :) It seems that I have somehow created a loop where it constantly says "Looking for new clips" and "No new clips found". Ideally I would like to have the script terminate if no new clips are found. I would also like to set this up so that when it finishes a download through cURL, it automatically sets my "MediaState" back to value=0 and ends the script. Here is my code so far:
# This script polls the unit downloads any new clips it hasn't already downloaded to the current directory
# Arguments: hostname or IP address of Ki Pro unit
import urllib, sys, string, os, posix, time
def is_download_allowed(address):
f = urllib.urlopen("http://"+address+"/config?action=get&paramid=eParamID_MediaState")
response = f.read()
if (response.find('"value":"1"') > -1):
return True
f = urllib.urlopen("http://"+address+"/config?action=set&paramid=eParamID_MediaState&value=1")
def download_clip(clip):
url = "http://" + address + "/media/" + clip
print url
posix.system("curl --output " + clip + " " + url);
def download_clips(response):
values = response.split(":")
i = 0
for word in values:
i += 1
if(word.find('clipname') > -1):
clip = values[i].split(',')[0].translate(string.maketrans("",""), '[]{} \,\"\" ')
if not os.path.exists(clip):
print "Downloading clip: " + clip
download_clip(clip)
else:
f = urllib.urlopen("http://"+address+"/config?action=set&paramid=eParamID_MediaState&value=0")
print "No new clips found"
address = sys.argv[1]
while 1:
if (is_download_allowed(address)):
print "Looking for new clips"
f = urllib.urlopen("http://"+address+"/clips")
response = f.read()
download_clips(response)
If the download_clips function is looping through the clip names, why you need that infinite while 1 loop? I think it is not necessary. Just remove it and dedent the block.

QPrinter doesn't print more than once

In my application I have a method for users to convert a report to a PDF document. This works perfectly - once. If the user clicks the button again, the conversion hangs.
This is my code:
def print_report(self):
web = QtWebKit.QWebView()
filename = "reporttemplate.html"
file = open(filename,'r')
html = file.read()
file.close()
web.setHtml(html)
#web.show()
printer = QtGui.QPrinter()
printer.setPageSize(QtGui.QPrinter.Letter)
printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
# ---- BROKEN ----
# This next line is where it hangs on the second call to this function.
# The first time it works, and generates the PDF as expected.
# ---- BROKEN ON THE NEXT LINE! ----
printer.setOutputFileName(r'C:\path\to\report\directory\file.pdf')
def convertIt():
web.print_(printer)
print "Pdf generated"
web.close()
QtCore.QObject.connect(web, QtCore.SIGNAL("loadFinished(bool)"), convertIt)
My thought is that the printer still has the file open. If that's the case, how can I close the file?
It works if I relaunch the application and the file already exists. For that reason, I don't believe it's hanging because the file already exists.
Testing your code I noticed that for me it only works when I put web.setHtml(html) at the end (last statement) in the print_report method. Doing that I was able to generate file.pdf as many times as I wanted to.

Categories

Resources