Search code examples
pythonpywinautoutorrent

uTorrent Automation using pywinauto


I am trying out an utorrent automation using pywinauto lib. I want to add a torrent with URL. This option is under the file menu. I can get as far as opening uTorrent and then nothing happens. I used Swapy for generating this code. The box below opens only when I run the code in swapy. But when I save it into a file and run with cmd, only utorrent opens and a traceback occurs in the cmd.

The Box where I want to enter the URL and Click OK

from pywinauto.application import Application

app = Application().Start(cmd_line=u'"C:\\Users\\User\\AppData\\Roaming\\uTorrent\\u Torrent.exe" ')
torrentdfb = app[u'\xb5Torrent4823DF041B09']
torrentdfb.Wait('ready')
menu_item = torrentdfb.MenuItem(u'&File->Add Torrent from &URL...\tCtrl+U')
menu_item.Click()

app.Kill_()

Traceback:
Traceback (most recent call last):
File "AddTorrent.py", line 5, in <module>
torrentdfb.Wait('ready')
File "C:\Python27\lib\site-packages\pywinauto\application.py", line 380, in Wait
WaitUntil(timeout, retry_interval, lambda: self.__check_all_conditions(check_method_names))
File "C:\Python27\lib\site-packages\pywinauto\timings.py", line 308, in WaitUntil
raise err
pywinauto.timings.TimeoutError: timed out

I am new to python coding and I am not an expert. It would be helpful if you provide the explanation to solve my problem or the code. Thanks!!


Solution

  • uTorrent is spawning another process, this is how I got it:

    >>> app.windows_()
    []
    >>> app.process
    6096
    >>> app.connect(title_re=u'^μTorrent.*(build \d+).*')
    <pywinauto.application.Application object at 0x000000000405C240>
    >>> app.process
    4044L
    

    This is a final code working for me (with 32-bit uTorrent and 32-bit Python 2.7):

    import pywinauto
    
    app = pywinauto.Application().start(r'uTorrent.exe')
    time.sleep(5) # because method connect() has no timeout param yet (planned for 0.6.0)
    app.connect(title_re=u'^\u03bcTorrent.*(build \d+).*')
    
    main_window = app.window_(title_re=u'^\u03bcTorrent.*(build \d+).*')
    main_window.MenuSelect(u'&File->Add Torrent from &URL...\tCtrl+U')
    
    app.AddTorrentFromURL.Edit.SetText('some URL')
    app.AddTorrentFromURL.OK.Click()
    

    Bitness is important. 32-bit uTorrent crashes if I use 64-bit Python.