1

I am scraping current follow requests from Instagram. I have a main infinite loop that it making the requests and prints OK when it is done. I want to display an animated cursor or loading progress while it is downloading names.

    while  1:
        response = requests.get(IG_CFR_PAGE, headers=headers(""), params=params, cookies=cookies)
        if response.status_code == 200:
            cfr = response.json()
            for entry in cfr["data"]["data"]:
                #print(entry["text"])
                usernames.append(entry["text"])
            if cfr["data"]["cursor"]!= None:
                params['cursor'] = cfr["data"]["cursor"]
                time.sleep(1)
            else:
                break
        else:
           print(response.status_code)
           print("Error in request .... aborting")
           break
print("ok")

I looked for tqdm but it takes an iterable. In my case, I am just looping over JSON keys in line for entry in cfr["data"]["data"]: and so I guess can't use it. I just need suggestions as to know what should I use to indicate that this script is actually doing something. I just need suggestions or pseudocode is fine to send me in a right direction... the actual programming code is not needed as I will do that myself.

Thank you

  • What do you mean? A gif or just the . .. ... .? – Xyndra Jun 05 '21 at 11:47
  • @Xyndra no an animation in the command line –  Jun 05 '21 at 12:37
  • Just `for entry in tqdm(cfr["data"]["data"])` is OK I guess. Since it is iterable(wrote in a `for` loop), it can be passed to `tqdm`, isn't it? – C.K. Jun 05 '21 at 13:02
  • @C.K. not it is giving error I already tried it. –  Jun 05 '21 at 13:46
  • @SulemanElahi I'd like to know what kind of error message is given, can you show that? – C.K. Jun 05 '21 at 15:06
  • @C.K. `Traceback (most recent call last): File "inlog.py", line 75, in for entry in tqdm(cfr["data"]["data"]): TypeError: 'module' object is not callable` –  Jun 05 '21 at 17:57
  • @SulemanElahi It is likely that you wrongly imported the `tqdm`. Use `from tqdm import tqdm` instead of `import tqdm`, then your problem should be solved. – C.K. Jun 06 '21 at 02:52

1 Answers1

0

As far as I'm aware, most functions that allow you to change the mouse cursor in Python are available only from various GUI modules - most of the popular ones, such as tkinter, PyQt5, pygame or others.

The problem is that most of these may only work when you've created a window of the GUI, which is probably unnecessary or not a nice idea if you're not using the same GUI, or any GUI for that matter. Even then, some may only take effect when the mouse pointer hovers over a certain widget in that GUI.

Note:

  • I've only (unsuccessfully) tried doing this with pygame.cursors before. It may be convenient because it even lets you create a custom shape with strings, or use a system cursor. But it displays a pygame.error: video system not initialized if you try doing this without having called pygame.display.init() first. I tried creating a window and setting a cursor, but it didn't seem to take effect.
  • After a quick google search for other ways to set an animated cursor, I came across this SO answer which might offer some insight if you're on windows.

Overall, using a terminal animation may probably be better and easier, so this is an attempt at a basic answer for a loading animation in a terminal window :

(Of course, for an indefinite length of loop, it doesn't make sense to store a percentage completion etc, so this just animates at an arbitrary pace and repeats when it reaches the end)

i, w, d = 0, 20, 10000
while True:
    # Do whatever
    # No print statements except this last one
    i = (i+1)%(w*d)
    l = i//d
    print("\r Processing... |" + " "*l+ "█" + " "*(w-l-1) +"|", end='')

i is used for iteration, w is the length of the bar, and d used to create some sort of 'delay', so that the bar doesn't change at every single iteration, but some slower (visible) speed

Edit: Important Note: The '\r' that resets the cursor position doesn't work in every terminal - it may still move to a new line for the next print() instead of the start of the same line - but this should most likely be fine in your system Terminal/cmd etc... May not be a good idea to run this in IDLE :P

Edit 2: Based on your comment, for a blinking indicator (using the same approach) -

i, d = 0, 300000
while True:
    i = (i+1)%d
    print("\r Working... " + ("█" if i < d/2 else " "), end='')
gdcodes
  • 266
  • 2
  • 10
  • I liked the idea, but not having a bar is okay because it doesn't work in this way. If I can just make this "█" cursor blink then it would be a perfect solution. Any idea for that? –  Jun 05 '21 at 14:11
  • 1
    I've added an alternative in my answer Of course, that still uses `print()` to make a "cursor", doesn't actually make the terminal cursor blink – gdcodes Jun 05 '21 at 14:17