Playability of a file in windows media player
Basically, I want to check if a URL is playable or not (without actually playing it). i.e. given a URL I want to write an automation script to figure it out for me if it's playable or not. If you give a bad/invalid URL for windows media player to play a pop-up window shows us that it cannot be played. So I would want to catch that event. I have found 2 ways to do this - 1) import win32com.client, win32api, sre, time data = file(webclips.txt) web_clips = data.readlines () shell = win32com.client.Dispatch(WScript.Shell) shell.Run(wmplayer.exe) shell.AppActivate(Windows Media Player) win32api.Sleep(100) print Total :, len(web_clips) for webclip in web_clips: shell.SendKeys(^u, 0) shell.AppActivate(Open URL) shell.SendKeys({DEL}) shell.SendKeys(webclip) shell.SendKeys(~) time.sleep(25) if shell.AppActivate(Windows Media Player): webclip = webclip not_there.append(webclip) shell.SendKeys(~) print len(not_there) print Not here: , not_there ~ In this way I manually fire Windows media player and do the checking. But It's a brute force way of solving the problem (since for every URL I keep a time-out of 25 seconds). As a result it takes a lot of time.I had to look for a better solution. My second solution uses Windows much hyped ActiveX controls. 2) from win32com.client import Dispatch,DispatchWithEvents class WMPEvents: def OnVisible(self,evt): print OnVisible changed:,evt def OnError(self,evt=None): print OnError,evt def OnMediaError(self,evt=None): print OnMediaError,evt def OnDisconnect(self,evt): print OnDisconnect,evt def OnStatusChange(self): print OnStatusChange def OnDisconnect(self,evt): print Disconnect,evt def OnBuffering(self,evt): print OnBuffering changed:,evt def OnOpenStateChange(self,evt=None): print OnOpenStateChange ,evt mp = DispatchWithEvents(WMPlayer.OCX, WMPEvents) mp.settings.autoStart = True webclip_playlist = mp.newPlaylist('Web_Clips', ) raw_web_clips = [] data = file(webclips.txt) web_clips = data.readlines() for url in web_clips: tune = mp.newMedia(url) mp.currentPlaylist.appendItem(tune) mp.controls.playItem (tune) mp.controls.play() mp.controls.stop() raw_input(Press enter to stop playing) mp.controls.stop() mp.close() This solution is much faster. But still I'm not able to solve it. Initially I had planned to use the OnOpenStateChange event to detect if a given URL is in a state just about to be opened. That's good enough for me to declare that a URL can be played. But as written in MSDN, they suggest not to rely on state changes as a definitive way to find details. So, I had to find an alternative. There is an event called OnBuffering, i.e. when a link is found and is just about to be buffered this event (Boolean value) is triggered. But it never seem to happen. I would be truly grateful if you can help in any way. -- http://mail.python.org/mailman/listinfo/python-list
Attaching files in windows using Python.
Hi all, I have to select a particular file (using the 'Browse') button in Windows. After this I need to populate the 'Open Dialogue Box' with the path of the file I need (I have the entier path of the file I need). Then I need to select the 'Open' Button. Only after this the file I want is attached. Any idea as to how this can be done using 'Win32 API's. While looking for the proper answer to this I found that 'Mark Hammond's' Python for Windows documentation is not detail enough. Any help in this regard would be much appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Attaching files in windows using Python.
Hi all, I have got this far till now - import win32gui, struct, array, string OFN_ALLOWMULTISELECT=0x0200 OFN_EXPLORER=0x0008 def arrayToStrings(resultArray): return list-of-strings corresponding to a char array, where each string is terminated by \000, and the whole list by two adjacent \000 bytes astr=resultArray.tostring() manyStrings=[] # perhaps a loop of string.split would be faster... while len(astr) and astr[0]!='\000': i=astr.index('\000') manyStrings.append(astr[:i]) astr=astr[i+1:] return manyStrings def szFrom(anarray): return the string-pointer (sz) corresponding to a char array, 0 (null pointer) if no array if anarray: return anarray.buffer_info()[0] else: return 0 def arrayFrom(astring,additional=0): return a char array built from a string, plus 0 or more \000 bytes as filler if not astring: astring='' return array.array('c',astring+additional*'\000') def arrayMulti(stringlist): return a char array built from many strings, each separated by a \000 byte, and two \000's at the end return arrayFrom(string.join(stringlist,'\000'),2) def buildOfn(resultarray,filters=None,initdir=None,title=None, multisel=1,oldlook=0): build an OPENFILENAME struct as a string, with several options and a given result-array for the string[s] that will result from the GetOpenFileName call flags=OFN_EXPLORER if multisel: flags=flags|OFN_ALLOWMULTISELECT if oldlook: flags=flags~OFN_EXPLORER szfile,maxfile=resultarray.buffer_info() szfilter=szFrom(filters) szinitdir=szFrom(initdir) sztitle=szFrom(title) return struct.pack( 3i2P2iPiPi2PI2hPi2P, 76, 0, 0, # size, owner-hwnd, hinstance szfilter, 0, 0, 0, # filter, custom-filter, max-cust-filter,filter-index szfile, maxfile,# file, max-file 0, 0, # file-title, max-file-title szinitdir, sztitle, # initial-dir, dialog-title flags, 0, 0,# flags, file-offset, file-extension 0, # def-ext 0, 0, 0)# cust-data, func-hook, template-name def openNames(forsave=0,filters=None,initdir=None,title=None, initfile=None,multisel=1,oldlook=0): return a list of filenames for open or save, given interactively by the user through a common-dialog; if more than 1 string is returned, the first is the directory, followed by the filenames. resultBuffer=arrayFrom(initfile,8192) title=arrayFrom(title) initdir=arrayFrom(initdir) filters=arrayMulti(filters) ofn=buildOfn(resultBuffer,filters,initdir,title,multisel,oldlook) if forsave: isok=win32gui.GetSaveFileName(ofn) else: isok=win32gui.GetOpenFileName(ofn) if not isok: return [] return arrayToStrings(resultBuffer) def _test(): return openNames( filters=('Texts and scripts','*.txt;*.py','Py stuff','*.py*') ) if __name__=='__main__': print _test() But hear the Dialogue_box stops and waits for the user to select a file. But Since I have the entire path of the file, How do I pass it to the file name to populate the box automatically instead of the user manually selecting a file. Any further help will be appreciated. -- http://mail.python.org/mailman/listinfo/python-list
cookielib
Hi, I need to get to a particular page in a website. The site uses cookeis and naturally I had to use cookielib since urllib2 does not support cookies. But even after adding the cookies to the headers, I keep getting a error message from the web-site saying that - 'My Browser has disabled cookies and I cannot access the page'. Here is the code I wrote - cookie_obj = cookielib.CookieJar() # First page socket = urllib2.Request(http://www.zap2it.com/index;) cookie_obj.add_cookie_header(socket) data = urllib2.urlopen(socket).read() print data # Second page socket = urllib2.Request(http://tvlistings2.zap2it.com/index.asp?partner_id=nationalmash=mash1zipcode=01810submit1=Continue;) cookie_obj.add_cookie_header(socket) data = urllib2.urlopen(socket).read() print data # Third Page socket = urllib2.Request(http://tvlistings2.zap2it.com/system.asp?partner_id=nationalzipcode=01810;) cookie_obj.add_cookie_header(socket) data = urllib2.urlopen(socket).read() print data Is there anything wrong in the above done code. I printed out the all the HTML headers in each stage to see if the cookie is getting added or not. I found out that the cookies are not getting added. But when I do socket.info().headers I get a list of all the headers wherein I can see 'Set-Cookie' tag and the cookie value. Anyone got any suggestions ? -- http://mail.python.org/mailman/listinfo/python-list
Link List in Python
Hi all, I have written a Link list implementation in Python (Although it's not needed with Lists and Dictionaries present. I tried it just for the kicks !). Anyway here is the code - # Creating a class comprising of node in Link List. class linklist: def __init__(self, data=None,link=None): self.data = data self.link = link def __str__(self): return str(self.data) def printing(node): print -*80 print ([data][link] --- [data][link] and so on till the end) print -*80 while 1: if node.link: print node.data, node.link,---, node = node.link else: # Printing the last node and exiting. print node.data, node.link print (All nodes printed) break def assigning(): global node1, node2, node3, node4 node1 = linklist([raw_input(Enter name: ), raw_input(Enter address: )]) node2 = linklist([raw_input(Enter name: ), raw_input(Enter address: )]) node3 = linklist([raw_input(Enter name: ), raw_input(Enter address: )]) node4 = linklist([raw_input(Enter name: ), raw_input(Enter address: )]) # Checking to see if all the node.data are getting populated. print node1 print node2 print node3 print node4 print linking() def linking(): node1.link = node2 node2.link = node3 node3.link = node4 # Passing the node1 to the print function so that it prints the rest of the nodes using the links. printing(node1) if __name__ == __main__: assigning() Doubt - Now, Here I needed only 4 nodes. But what if I need more nodes. Is there any way to create the number of nodes at runtime. Since I plan to 'import' this module later. I wouldn't know how many nodes I need even before executing it. So, my doubt is - Is there any way to create 'n' number of object (here nodes) at runtime ? Any general criticisms about the code are also welcome... -- http://mail.python.org/mailman/listinfo/python-list
Re: Removing Duplicate entries in a file...
Thanx Mike, My problem solved !! I loaded the entire file contnets into list and my job was a piece of cake after that. Srikar -- http://mail.python.org/mailman/listinfo/python-list
Re: Removing Duplicate entries in a file...
Hi there, I'm just curious to know as to how the changes you have suggested will solve the problem. Instead of appending (what I was doing), now we are opening and storing the files in 'binary' format. All the other entries in my file will be gone when I write into the file again. What I actuall need is this - I have some dictionary values stored in a file. I retrieve these entries based on the key value specified by the user. Now if I want to modify the values under a particular key, I first search if that key exists in the file and if yes retrieve the values associated with the key and modify them. Now when I re-insert this modified key-value pair back in the file. I have 2 entries now (one is the old wntry and the second is the new modified one). So if I search for that key the next time I'll have 2 entries for it. That's not what we want. So how do I remove the old entry without the other values getting deleted ? In other words, keeping the other entries as it is, I want to update a particular key-value pair. Let me know in case any bright idea strikes... -- http://mail.python.org/mailman/listinfo/python-list
Removing Duplicate entries in a file...
Hi all, I'm storing number of dictionary values into a file using the 'cPickle' module and then am retrieving it. The following is the code for it - # Code for storing the values in the file import cPickle book = {raw_input(Name: ): [int(raw_input(Phone: )), raw_input(Address: )] } file_object = file(database, 'w+') cPickle.dump(book, file_object) file_object.close() # Code for retrieving values and modifiing them. tobe_modified_name = raw_input(Enter name to be modified: ) file_object = file(database) while file_object.tell() != EOFError: try: stored_dict = cPickle.load(file_object) if stored_dict.has_key(tobe_modified_name): print (Entry found !) # I want to modify the values retrieved from the file and then put it back to the file without duplicate entry. file_object = file(database, 'a+') except EOFError: break file_object.close() Now, my problem is after finding the entry in the file, I want to make changes to the 'values' under the searched 'key' and then insert it back to the file. But in doing so I'm having duplicate entries for the same key. I want to remove the previous key and value entry in the file and key the latest one. How to solve this problem ? I actually thought of 2 ways - 1) In Java there is something called 'file_pointer' concept where in after you find the entry you are looking for you move all the entries below this entry. Then you get the searched entry at the bottom of the file. After this truncate the file by a certain bytes to remove the old entry. Can we do this in Python using the file.truncate([size]) method ? 2) Although this is a really crappy way but nevertheless I'll put it across. First after finding the entry you are looking for in the file, make a copy of this file without the entry found in the previous file. Make the changes to the 'values' under this key and insert this into the second file what you have created. Before exiting delete the first file. Are there any more ways to solve my problem ? Any criticisms are welcome -- http://mail.python.org/mailman/listinfo/python-list
Re.'Compressing folders in Windows using Python'
Hi all,This is in reply to the 'Compressing folders in Windows using Python' query I raised y'day. I figured out that windows does not allow command line zipping so I started looking for alternatives. I found some modules in Perl which does zipping. I guess it goes by the name 'gzip'. I plan to write the zipping feature in Perl and import it into Python. Thanx a lot for all those who took time to answer my query. If you find any loophole in the above approach feel free to drop me mail... Srikar -- http://mail.python.org/mailman/listinfo/python-list
Compressing folders in Windows using Python.
Hi, I'm trying to zip a particular fiolder and place the zipped folder into a target folder using python. I have used the following command in 'ubuntu'. zip_command = 'zip -qr %s %s' % (target, ' '.join(source)) I execute this using os.command(zip_command). It works fine... But when I run this script in Windows XP, I get an error while executing the above zip command. What command is there to zip files in Windows? Or is there any other problem ? -- http://mail.python.org/mailman/listinfo/python-list