Dump file from url with cron job - python

I wrote simple Python script with urllib to retrieve file. Everything is working if the file in address owns the ending, lets say .txt, or .exe or .csv. But I got an url with the ending of file like this -- action=download&user=kff&projectname=15&date=today; So as I see it query the data from sql database and dump it in csv format in browser. When I use this link simple in the browser it works perfectly. When I am trying to do it with python it dumps me file with 0 Kb size. Here is the code:
import os
import urllib
pp = os.path.join("path", "fileName")
urllib.urlretrieve ("urlLink", pp)
Should I use wget instead of that or any other ideas would be helpful. Thanks

Related

How can I have a JSON file with my current setup?

My current setup is typing out my discord.py code script in Notepad and using Python 3.9 app to open them as a command prompt. I have a JSON file in a separate Notepad file. Id like to include the JSON file to my code notepad or have it open up with the Python prompt. Ive tried opening them as 2 different Python prompts but it doesnt seem to work. I can explain in more detail to someone who knows how to fix it or needs more explanation on my discord. #Lone Wolf#2245
try using open() and json.loads
You can import dict by using them
from json import loads
dictionary = loads(open('Your file directory', 'r'))
Now you can use variable dictionary like dict

Unable to play WAV file generated by IBM Watson's TTS (Text To Speech)

Currently working with the example script found on IBM Watson's GitHub:
Link: https://github.com/watson-developer-cloud/python-sdk/blob/master/examples/text_to_speech_v1.py
When I run the script, it works perfectly creating the WAV file. However, when I try to play it back within the script, it simply runs and never plays. I tried using PyAudio, Os, Subprocess, and other third party libraries to play the file, however, nothing worked. Is there something I would have to do to the file first before attempting to play it in the script? I'm assuming it has something to do with it being written in binary, which is what the script calls for, but I'm still too new at programming to understand how to solve the problem.
I'll attach my full script below with placeholders for personal info. Thanks!
# coding=utf-8
from os.path import join, dirname
from watson_developer_cloud import TextToSpeechV1
from watson_developer_cloud.websocket import SynthesizeCallback
import subprocess
service = TextToSpeechV1(url='EXAMPLE URL TO API', iam_apikey='EXAMPLE API KEY')
with open(join(dirname(__file__), '..EXAMPLE PATH../resources/output2.wav'),'wb') as audio_file:
response = service.synthesize("What's the weather?", accept='audio/wav', voice="en-US_MichaelVoice").get_result()
audio_file.write(response.content)
def audio_call():
audio_file_path = "..EXAMPLE PATH../resources/output2.wav"
return subprocess.call(["afplay", audio_file_path])
audio_call()
[SOLVED]: Apparently there was a problem with my file directory playing WAV files. By changing the file acceptance to "accept = 'audio/wav'" it worked fine.

How to use iTune's API to download a file and import the string using python?

iTune has an API that lets me download a file about an app.
When I type in https://itunes.apple.com/search?term=yelp&country=us&entity=software, it prompts me to download a file.
Is there a command in python that downloads the file and import the string into a variable?
Thanks
To download the contents of the file you can simply do:
import requests
string_var = requests.get("https://itunes.apple.com/search?term=yelp&country=us&entity=software").content
Seeing, that the response is a JSON-file you would probably want to add:
import json
resp_dict = json.loads(string_var)
This will give you a dictionary to work with
These are just small snippets. I used Python2.7

urllib2 download HTML file

Using urllib2 in Python 2.7.4, I can readily download an Excel file:
output_file = 'excel.xls'
url = 'http://www.nbmg.unr.edu/geothermal/GEOTHERM-30Jun11.xls'
file(output_file, 'wb').write(urllib2.urlopen(url).read())
This results in the expected file that I can use as I wish.
However, trying to download just an HTML file gives me an empty file:
output_file = 'webpage.html'
url = 'http://www.nbmg.unr.edu/geothermal/mapfiles/nvgeowel.html'
file(output_file, 'wb').write(urllib2.urlopen(url).read())
I had the same results using urllib. There must be something simple I'm missing or don't understand. How do I download an HTML file from a URL? Why doesn't my code work?
If you want to download files or simply save a webpage you can use urlretrieve(from urllib library)instead of use read and write.
import urllib
urllib.urlretrieve("http://www.nbmg.unr.edu/geothermal/mapfiles/nvgeowel.html","doc.html")
#urllib.urlretrieve("url","save as..")
If you need to set a timeout you have to put it at the start of your file:
import socket
socket.setdefaulttimeout(25)
#seconds
It also Python 2.7.4 in my OS X 10.9, and the codes work well on it.
So I think there maybe other problems prevent its working. Can you open "http://www.nbmg.unr.edu/geothermal/GEOTHERM-30Jun11.xls" in your browser?
This may not directly answer the question, but if you're working with HTTP and have sufficient privileges to install python packages, I'd really recommend doing this with 'requests'. There's a related answered here - https://stackoverflow.com/a/13137873/45698

Python urllib2 file upload problems

I'm currently trying to initiate a file upload with urllib2 and the urllib2_file library. Here's my code:
import sys
import urllib2_file
import urllib2
URL='http://aquate.us/upload.php'
d = [('uploaded', open(sys.argv[1:]))]
req = urllib2.Request(URL, d)
u = urllib2.urlopen(req)
print u.read()
I've placed this .py file in my My Documents directory and placed a shortcut to it in my Send To folder (the shortcut URL is ).
When I right click a file, choose Send To, and select Aquate (my python), it opens a command prompt for a split second and then closes it. Nothing gets uploaded.
I knew there was probably an error going on so I typed the code into CL python, line by line.
When I ran the u=urllib2.urlopen(req) line, I didn't get an error;
alt text http://www.aquate.us/u/55245858877937182052.jpg
instead, the cursor simply started blinking on a new line beneath that line. I waited a couple of minutes to see if something would happen but it just stayed like that. To get it to stop, I had to press ctrl+break.
What's up with this script?
Thanks in advance!
[Edit]
Forgot to mention -- when I ran the script without the request data (the file) it ran like a charm. Is it a problem with urllib2_file?
[edit 2]:
import MultipartPostHandler, urllib2, cookielib,sys
import win32clipboard as w
cookies = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),MultipartPostHandler.MultipartPostHandler)
params = {"uploaded" : open("c:/cfoot.js") }
a=opener.open("http://www.aquate.us/upload.php", params)
text = a.read()
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardText(text)
w.CloseClipboard()
That code works like a charm if you run it through the command line.
If you're using Python 2.5 or newer, urllib2_file is both unnecessary and unsupported, so check which version you're using (and perhaps upgrade).
If you're using Python 2.3 or 2.4 (the only versions supported by urllib2_file), try running the sample code and see if you have the same problem. If so, there is likely something wrong either with your Python or urllib2_file installation.
EDIT:
Also, you don't seem to be using either of urllib2_file's two supported formats for POST data. Try using one of the following two lines instead:
d = ['uploaded', open(sys.argv[1:])]
## --OR-- ##
d = {'uploaded': open(sys.argv[1:])}
First, there's a third way to run Python programs.
From cmd.exe, type python myprogram.py. You get a nice log. You don't have to type stuff one line at a time.
Second, check the urrlib2 documentation. You'll need to look at urllib, also.
A Request requires a URL and a urlencoded encoded buffer of data.
data should be a buffer in the
standard
application/x-www-form-urlencoded
format. The urllib.urlencode()
function takes a mapping or sequence
of 2-tuples and returns a string in
this format.
You need to encode your data.
If you're still on Python2.5, what worked for me was to download the code here:
http://peerit.blogspot.com/2007/07/multipartposthandler-doesnt-work-for.html
and save it as MultipartPostHandler.py
then use:
import urllib2, MultipartPostHandler
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler())
opener.open(url, {"file":open(...)})
or if you need cookies:
import urllib2, MultipartPostHandler, cookielib
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), MultipartPostHandler.MultipartPostHandler())
opener.open(url, {"file":open(...)})

Categories