Apscheduler runs once then throws TypeError
Asked Answered
F

2

11

I'm trying to add a list of someone's soundcloud followers to a database every hour. I have the code working to pull their list of followers and add them to a db, but I run into errors when I use it with apscheduler.

Here's an example of the error:

Traceback (most recent call last):
  File "desktop/SoundcloudProject/artistdailyfollowers.py", line 59, in <module>
    scheduler.add_job(inserttodaysdata(), 'interval', hours=1)
  File "//anaconda/lib/python3.5/site-packages/apscheduler/schedulers/base.py", line 425, in add_job
    job = Job(self, **job_kwargs)
  File "//anaconda/lib/python3.5/site-packages/apscheduler/job.py", line 44, in __init__
    self._modify(id=id or uuid4().hex, **kwargs)
  File "//anaconda/lib/python3.5/site-packages/apscheduler/job.py", line 165, in _modify
    raise TypeError('func must be a callable or a textual reference to one')
TypeError: func must be a callable or a textual reference to one

Here's the code:

import soundcloud
import sqlite3
import datetime
import time
from apscheduler.schedulers.blocking import BlockingScheduler

client = soundcloud.Client(client_id='f3b669e6e4509690939aed943c56dc99')
conn = sqlite3.connect('desktop/SoundcloudProject/RageLogic.db')
c = conn.cursor()
writenow = datetime.datetime.now()
print("If this is printing that means it's running")
print("The time is now: \n" +str(writenow))
page ='https://soundcloud.com/ragelogic'
page_size = 200

def create_table():
    c.execute('CREATE TABLE IF NOT EXISTS RageLogicFollowersByDay(day TEXT, list_of_followers TEXT)')

#add todays date and a list of followers to the db
def inserttodaysdata():
    global page
    full = False
    user = client.get('/resolve', url=page)
    ufollowing = []
    ufirstfollowing = client.get('/users/'+str(user.id)+'/followers', order='id',limit=page_size, linked_partitioning=1)
    for user in ufirstfollowing.collection:
        ufollowing.append(user.id)
    if hasattr(ufirstfollowing, "next_href"):
        #print("MANYFOLLOWING")
        newlink = ufirstfollowing.next_href
        try:
            while full == False:
                newlist = client.get(newlink)
                for user in newlist.collection:
                    ufollowing.append(user.id)
                print(len(ufollowing))
                if newlist.next_href == None:
                    print("full")
                    full = True
                else:
                    newlink = newlist.next_href
        except AttributeError:
            None

    #print(len(ufollowing))
    wtl = []
    wtl = repr(ufollowing)
    writenow = datetime.datetime.now()
    c.execute("INSERT INTO RageLogicFollowersByDay (day, list_of_followers) VALUES (?, ?)",(str(writenow), wtl))
    conn.commit()

#create_table()
scheduler = BlockingScheduler()
scheduler.add_job(inserttodaysdata(), 'interval', hours=1)
scheduler.start()

I'm really new to this whole thing and any help anyone could give would be awesome, thanks!

Fid answered 20/11, 2016 at 3:35 Comment(0)
B
35

See this line:

scheduler.add_job(inserttodaysdata(), 'interval', hours=1)

and it should be

scheduler.add_job(inserttodaysdata, 'interval', hours=1)

You're calling inserttodaysdata() and passing its return value to add_job(). Don't do that. Pass the function itself, not its call result.

Blurt answered 20/11, 2016 at 22:6 Comment(4)
Oh wow that was way easier than I thought it would be, thanks!Fid
How would you pass it if the function requires arguments?Aloisius
If you look at the documentation, you can see that add_job() takes arguments named args and kwargs. Use those.Cuirass
WOW, this is super simple and not intuitive... Big 10X.Parterre
G
0

I solved it with just adding time-zone:

scheduler = BackgroundScheduler(timezone="Europe/Berlin")
Germanous answered 26/1, 2022 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.