检查更改的数字是否小于或大于最后一次

时间:2018-11-30 16:32:47

标签: python counter youtube-data-api

我正在研究一个取笑Logan Paul的python程序,以练习我的python技能。从本质上讲,我的想法是监视Logan的子计数,如果他失去了订阅者,这会取笑他。到目前为止,我已经创建了一个(不良的)GUI,用于显示他的子计数。我将如何监视它并知道他是否丢失或获得了潜艇?作为我的概念证明,我希望它在控制台中执行类似“丢失”或“获得”的打印操作。我认为要执行此操作,必须使用io模块将先前的数字存储在内存中,但我认为这不是最好的方法。

到目前为止,这是我的代码,Comic Sans生效:

import urllib.request
import json
from tkinter import*

channelid = "UCG8rbF3g2AMX70yOd8vqIZg"
key = "AIzaSyDAOUFomRB1lxdb_fvSKKaG-FSZDRoVt_s"


def func(label2):
    data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+channelid+"&key="+key).read()
    subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]
    subc =("{:,d}".format(int(subs)))
    label2.config(text=subc)
    label2.update()
    root.after(10, lambda:func(label2))


root = Tk()
root.geometry("900x600")
root.title("yeetmeister")

label1 = Label(text="Logan Paul's Sub Count:", font=("Comic Sans MS", 45), fg="Brown")
label2 = Label(font=("Comic Sans MS", 45), fg="Red")

label1.place(x=10, y=20)
label2.place(x=10, y=130)

func(label2)

root.mainloop()

3 个答案:

答案 0 :(得分:1)

您需要一个变量,该变量将保留上一张支票中的订户并与新支票中的订户进行比较。因此,您必须按照以下步骤修改程序:

import urllib.request
import json
from tkinter import*

channelid = "UCG8rbF3g2AMX70yOd8vqIZg"
key = "AIzaSyDAOUFomRB1lxdb_fvSKKaG-FSZDRoVt_s"
prevSubs = 0 # Variable to hold the subs from previous check
firstTime = True # Flag variable to avoid checking the first time due to lack of previous subscribers.

def func(label2,prevSubs, firstTime): # Change the signature here.
    data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+channelid+"&key="+key).read()
    subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]
    subc =("{:,d}".format(int(subs)))
    subs = int(subc.replace(',',''))
    # Comparisons to print the appropriate message
    if firstTime:
        print("Started monitoring...")
        firstTime = False
        pass
    else:
        if subs > prevSubs:
            print("Gained")
        elif subs < prevSubs:
            print("Lost")   
        else:
            pass
    prevSubs = subs # Update previous subs.
    label2.config(text=subc)
    label2.update()
    root.after(10, lambda:func(label2,prevSubs,firstTime)) # Add argument to 'func'.


root = Tk()
root.geometry("900x600")
root.title("yeetmeister")

label1 = Label(text="Logan Paul's Sub Count:", font=("Comic Sans MS", 45), fg="Brown")
label2 = Label(font=("Comic Sans MS", 45), fg="Red")

label1.place(x=10, y=20)
label2.place(x=10, y=130)

func(label2,prevSubs,firstTime) # Add new argument to 'func'.

root.mainloop()

答案 1 :(得分:0)

只需将其设置为您记住的变量即可,这样,当您获得新的子计数时,就可以对其进行比较

prev_subs = 0

def func():
    current_subs = subs #from api
    if prev_subs:
       if prev_subs > current_subs:
           #went down
       elif prev_subs < current_subs:
           #went up

    prev_subs = current_subs

while true: # just to continually refresh the data
    func(label2)

答案 2 :(得分:0)

您可以添加全局变量:

import urllib.request
import json
from tkinter import*

channelid = "UCG8rbF3g2AMX70yOd8vqIZg"
key = "AIzaSyDAOUFomRB1lxdb_fvSKKaG-FSZDRoVt_s"

score = 0

def func(label2, label3):
    global score
    r = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+channelid+"&key="+key).read()
    # data = r.json()
    subs = json.loads(r.decode('utf-8'))["items"][0]["statistics"]["subscriberCount"]
    newscore = int(subs)
    if(score < newscore):
        label3.config(text="UP" + " prev[ " + str(score) + "]")
        label3.config(fg="Green")
    else:
        if(score == newscore):
            label3.config(text="SAME" + " prev[ " + str(score) + "]")
            label3.config(fg="Blue")
        else:
            label3.config(text="DOWN" + " prev[ " + str(score) + "]")
            label3.config(fg="Red")
    score = newscore
    subc =("{:,d}".format(newscore))
    label2.config(text=subc)
    label2.update()
    root.after(10, lambda:func(label2, label3))


root = Tk()
root.geometry("900x600")
root.title("yeetmeister")

label1 = Label(text="Logan Paul's Sub Count:", font=("Comic Sans MS", 45), fg="Brown")
label2 = Label(font=("Comic Sans MS", 45), fg="Red")
label3 = Label(font=("Comic Sans MS", 45), fg="Blue")

label1.place(x=10, y=20)
label2.place(x=10, y=130)
label3.place(x=340, y=130)
label3.config(text="NO CHANGE")

func(label2, label3)

root.mainloop()