NotTheEconomist / twitch_followers

0 stars 0 forks source link

Documentation? #1

Closed ccrvlh closed 6 years ago

ccrvlh commented 6 years ago

Hello, saw your repo on StackOverflow, and if you don't mind, would like to understand a bit better how the code works.

I'm trying to get new orders and/or change in the payment status of my orders on my Shopify account (which is basically the same logic I guess) - does this code outputs a dictionary with new followers/new tweets?

Thanks a lot!

NotTheEconomist commented 6 years ago

Hey there,

This was really just a private utility I used to track new viewers/followers on Twitch since I didn't like the existing (or at least the existing free) options. I'll push up a commit with some docstrings that should help out.

It's not terribly complicated, api.py has all the moving parts and main.pyw is just the GUI to put it in. App.tick tells all those moving parts to, well, move.

NotTheEconomist commented 6 years ago

Commit fe7c45eb98faaa5b11b99f6d78994e3909d11bda closes #1

ccrvlh commented 6 years ago

Thanks, just so I understand it tracks new followers, right? So I understood that the class Followers creates a "flag" to make the program understand what is the status of every follower (whether it is new or it was already there)? Do you compare it with a JSON file in your computer?

def __init__(self, name, is_new=True):
        self.name = name
        self.__new = is_new

Then the get_followers get all the followers in the new JSON (vs the JSON in your computer) and "checks" whether that object has been flagged and if it hasn't, returns a dictionary with new users?

def get_followers(channel_name):
return {Follower(d['user'].get("display_name")) for d in r.json()['follows']}

Thanks a lot!

NotTheEconomist commented 6 years ago

Hey there,

All new Follower objects are marked as new when they're instantiated as part of __init__. The new property lets you check that flag, and the see method sets it forever to False.

The state is maintained inside main.App

# some lines redacted here
class App(tk.Tk):
    def __init__(self, *args, channel_name=None, **kwargs):
        self.followers = self.get_followers()  # as we instantiate, get all followers
        self.clear_new_followers()             # ...and mark them as seen
        # two lines later...
        self.clear_new_followers()             # then do it again for no reason other than
                                               # yours truly can't proofread his own code :-)

The only JSON that exists comes back from the Twitch API and is parsed by my api module.

def get_followers(channel_name):
    """get_followers expects the name of a twitch channel and issues a command via the Twitch API that
    returns a set of that channel's followers (as Follower objects).
    """
    request_target = "/".join([twitch_base_point, "channels", channel_name, "follows"])
    r = requests.get(request_target, headers=twitch_headers)
    return {Follower(d['user'].get("display_name")) for d in r.json()['follows']}

def get_viewers(channel_name):
    """get_viewers expects the name of a twitch channel and issues a command via the Twitch API that
    returns a list of that channel's viewers (as strings).
    """
    request_target = "/".join([twitch_base_point, "streams", channel_name])
    r = requests.get(request_target, headers=twitch_headers)
    stream = r.json()['stream']
    if stream is None:
        return 0
    else:
        return stream['viewers']
ccrvlh commented 6 years ago

Great, thanks a lot for your explanation! I'll try to do something similar with Shopify... will see if I can learn from the logic you've used and apply it in another context. Cheers!