Apscheduler runs once then throws TypeError - soundcloud

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!

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.

I solved it with just adding time-zone:
scheduler = BackgroundScheduler(timezone="Europe/Berlin")

Related

Error when reloading new Plugin code in QGIS

I am nooby to Plugin Development but I'm trying to create a plugin in QGIS, for a Uni-subject, with its own graphical interface, which will receive a zipcode from the user and return the name of the corresponding location.
I already created the plugin skeleton, through the Plugin Builder and have designed the graphical interface with QtDesigner https://i.stack.imgur.com/h6k6Q.png . I also added the .txt file that contains the zipcodes database to the plugin folder as a resource.
From what I understand, the file to edit is the one that ends in dialog.py, through the init() method, in order to establish the connections between the signals emitted by the elements of the graphical interface and the corresponding callbacks.
However, when I change the code in the dialog.py and reload the plugin, it gives me an error, and when starting the QGIS it pop-ups an error message, and the plugin no longer appears. Error message after plugin reload
Could you give me some guidance here and maybe point me to where the problem could be? Thanks
The code is this one:
import os
import sys
import qgis.core
from qgis.PyQt import uic
from qgis.PyQt import (
QtCore,
QtWidgets
)
import geocoder
sys.path.append(os.path.dirname(__file__))
FORM_CLASS, _ = uic.loadUiType(
os.path.join(
os.path.dirname(__file__),
"example_dialog_base.ui"
),
resource_suffix=""
)
class ExampleDialog(QtWidgets.QDialog, FORM_CLASS):
POSTAL_CODES_PATH = ":/plugins/example/todos_cp.txt"
def __init__(self, parent=None):
"""Constructor."""
super(ExampleDialog, self).__init__(parent)
self.setupUi(self)
# connect signals
self.postal_code_le.textChanged.connect(self.toggle_find_button)
self.find_code_btn.clicked.connect(self.execute)
# set initial state
self.find_code_btn.setEnabled(False)
def toggle_find_button(self):
if self.postal_code_le.text() == "":
self.find_code_btn.setEnabled(False)
else:
self.find_code_btn.setEnabled(True)
def execute(self):
self.address_te.clear()
try:
raw_postal_code = self.postal_code_le.text()
main_code, extension = validate_postal_code(raw_postal_code)
record = self.find_record(main_code, extension)
place_name = record[3]
self.address_te.setPlainText(place_name)
if self.create_layer_chb.isChecked():
self.handle_layer_creation(record)
except (ValueError, RuntimeError) as err:
self.show_error(str(err))
def find_record(self, main_code, extension):
file_handler = QtCore.QFile(self.POSTAL_CODES_PATH)
file_handler.open(QtCore.QIODevice.ReadOnly)
stream = QtCore.QTextStream(file_handler)
while not stream.atEnd():
line = stream.readLine()
info = line.split(";")
code1 = info[-3]
code2 = info[-2]
if code1 == main_code and code2 == extension:
result = info
break
else:
raise RuntimeError("Sem resultados")
return result
def handle_layer_creation(self, record):
place_name = record[3]
point = geocode_place_name(place_name)
print("lon: {} - lat: {}".format(point.x(), point.y()))
layer = create_point_layer(
point,
f"found_location_for_{record[-3]}_{record[-2]}",
place_name
)
current_project = qgis.core.QgsProject.instance()
current_project.addMapLayer(layer)
def show_error(self, message):
message_bar = self.iface.messageBar()
message_bar.pushMessage("Error", message, level=message_bar.Critical)
def validate_postal_code(raw_postal_code):
code1, code2 = raw_postal_code.partition("-")[::2]
if code1 == "" or code2 == "":
raise ValueError(
"Incorrect postal code: {!r}".format(raw_postal_code))
return code1, code2
def geocode_place_name(place_name):
geocoder_object = geocoder.osm(place_name)
lon = geocoder_object.json.get("lng")
lat = geocoder_object.json.get("lat")
if lat is None or lon is None:
raise RuntimeError(
"Could not retrieve lon/lat for "
"place: {!r}".format(place_name)
)
point = qgis.core.QgsPointXY(lon, lat)
return point
def create_point_layer(point, layer_name, place_name):
layer = qgis.core.QgsVectorLayer(
"Point?crs=epsg:4326&field=address:string(100)",
layer_name,
"memory"
)
provider = layer.dataProvider()
geometry = qgis.core.QgsGeometry.fromPointXY(point)
feature = qgis.core.QgsFeature()
feature.setGeometry(geometry)
feature.setAttributes([place_name])
provider.addFeatures([feature])
layer.updateExtents()
return layer

Python mysql.connector .fetchall() returns (redundant) data in different formats

I have this method to execute Queries:
def exeQuery(query, data, dbEdit ):
try:
myDatabase = mysql.connector.connect(**dbLoginInfo)
cursor = myDatabase.cursor()
except mysql.connector.Error as e:
print('[ERROR WHILE CONNECTING TO DATABASE]: ', e)
else:
if dbEdit == True:
if data == None:
res = cursor.execute(query)
myDatabase.commit()
else:
res = cursor.execute(query, data)
myDatabase.commit()
else:
if data == None:
cursor.execute(query)
res = cursor.fetchall()
else:
cursor.execute(query, data)
res = cursor.fetchall()
cursor.close()
myDatabase.close()
#print(type(res))
#print(res)
return res
And I call it here:
#app.route("/profil/delete", methods= ['POST'])
#token_required
def deleteProfil():
dicUser = decodeToken(request.args.get('token'))
profilName = request.args.get('profilName')
path = exeQuery('SELECT profilbild FROM Profil WHERE profilName = %s AND konto_email = %s', (profilName, dicUser['user']), False)
print(path)
return Response(status = 200)
It should print me ('pics/jj#gmail.de/DelProf.png',) once.
For some reason I get the right data but sometimes the formatting is changing. Everytime there was only one row in the database that fit to the query. These are the three outputs I got so far:
[('pics/jj#gmail.de/DelProf.png',), ('pics/jj#gmail.de/DelProf.png',), ('pics/jj#gmail.de/DelProf.png',), ('pics/jj#gmail.de/DelProf.png',)]
[('pics/jj#gmail.de/DelProf.png',)]
('pics/jj#gmail.de/DelProf.png',)
The method MySQLCursor.fetchall() returns a list of tuples. as mentioned here: https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchall.html.
Even if there are no results it will return a list, but empty [].
If you got a list with repeated values, it's possible that your query it may found those matches.

ValurError when creating a Pygame surface in a class

for some reason, this code doesn't work and for the life of me I can't figure out
import pygame as py
py.init()
py.font.init()
c_size = 35
monaco = py.font.SysFont("monaco", int(round(c_size * 1.4)))
class Button(object):
def __init__(self,title):
self.title = title
self.width = monaco.size(self.title)[0]
self.height = monaco.size(self.title)[1]
self.surf = py.Surface(int(self.width), int(self.height))
stop = Button("Stop")
Raises this error:
Traceback (most recent call last):
File "/Users/yonatanmitzna/Desktop/hiuoho.py", line 21, in <module>
stop = Button("Stop")
File "/Users/yonatanmitzna/Desktop/hiuoho.py", line 19, in __init__
self.surf = py.Surface(int(self.width), int(self.height))
ValueError: size needs to be (int width, int height)
I tried to test the type of the values going into the py.Surface command, and they are both int! no clue why this doesn't work.
The argument to pygame.Surface must be a tuple with the size of the surface, instead of 2 separate arguments:
self.surf = py.Surface((int(self.width), int(self.height)))
self.surf = py.Surface((int(self.width), int(self.height)))

Insert to postgres DB: invalid String format (parsing exception error)

I am creating a log db where I track execution steps and catch where it might or might not fail.
I am having issues inserting the Exceptions thrown to a DB.
I am calling an endpoint first in order then query a django database.
In this case I wanted to catch a mistake in the selection_query and thus being unable to fetch the data from the DB.
I want to log this message.
Example:
Exception catch
try:
select_query = "SELECT columnn from row"
cur.execute(select_query)
selected_vals = cur.fetchall()
except Exception as e:
response = insert_into_logging(message = f"{e}", logging_id = 3)
Error
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
This is because of the value passed in the message argument.
(message = f"{e}")
It is not insertable in the DB
The value of "e" is:
auto_close_alarm relation "row" does not exist
LINE 1: SELECT columnn from row
^
I tried to pass
str(e)
Value passed: identical output as above
repr(e.args[0])
Output: 'relation "row" does not exist\nLINE 1: SELECT columnn from row\n ^\n'
repr(e)
Value passed: UndefinedTable('relation "row" does not exist\nLINE 1: SELECT columnn from row\n ^\n',)
And multiple other ways but to no avail.
They all result in the same JSONDecodeError.
Basically the key problem is that this is not a valid String format for it to be able to be parsed in the DB.
All I need is a representation of this Exception error for it to be insertable
Flow of data:
insert function
def insert_into_logging(message, logging_id, date_time):
url = f"""url/insert-into-logging/"""
data = {
"message": message,
"logging_id": logging_id,
}
response = r.post(url, data=json.dumps(data, indent=4, sort_keys=True, default=str))
print(response)
return response.json()
Querying DB
def insert_to_logging(request):
from datetime import datetime, timedelta
if request.method != 'POST':
print(request.method)
return JsonResponse({'status':'error', 'message':'Client insertion only accepts post requests.' + str(request.method)})
today = datetime.today()
body = json.loads(request.body)
message = body.get('message')
logging_id = body.get('logging_id')
Logging.objects.create(message = message, logging_id = logging_id, time_stamp = today)
return JsonResponse({'status':'OK', 'message':'Logging successful'})
Django Model
class Logging(models.Model):
objects = GetOrNoneManager()
message = models.CharField(max_length=64, blank=True, null=True)
logging_id = models.IntegerField(blank=True, null=True)
time_stamp = models.DateTimeField(blank=True, null=True)
class Meta:
db_table = 'logging'

Calling a custom function in Rasa Actions

I am facing a problem in developing a chatbot using rasa .
I am trying to call a custom function in rasa action file. But i am getting an error saying "name 'areThereAnyErrors' is not defined"
here is my action class. I want to call areThereAnyErrors function from run method. Could someone please help how to resolve this?
class ActionDayStatus(Action):
def areThereAnyErrors(procid):
errormessagecursor = connection.cursor()
errormessagecursor.execute(u"select count(*) from MT_PROSS_MEAGE where pro_id = :procid and msg_T = :messageT",{"procid": procid, "messageT": 'E'})
counts = errormessagecursor.fetchone()
errorCount = counts[0]
print("error count is {}".format(errorCount))
if errorCount == 0:
return False
else:
return True
def name(self):
return 'action_day_status'
def run(self, dispatcher, tracker, domain):
import cx_Oracle
import datetime
# Connect as user "hr" with password "welcome" to the "oraclepdb" service running on this computer.
conn_str = dbconnection
connection = cx_Oracle.connect(conn_str)
cursor = connection.cursor()
dateIndicator = tracker.get_slot('requiredDate')
delta = datetime.timedelta(days = 1)
now = datetime.datetime.now()
currentDate = (now - delta).strftime('%Y-%m-%d')
print(currentDate)
cursor = connection.cursor()
cursor.execute(u"select * from M_POCESS_FILE where CREATE_DATE >= TO_DATE(:createDate,'YYYY/MM/DD') fetch first 50 rows only",{"createDate":currentDate})
all_files = cursor.fetchall()
total_number_of_files = len(all_files)
print("total_number_of_files are {}".format(total_number_of_files))
Answer given by one of the intellectuals :
https://realpython.com/instance-class-and-static-methods-demystified/ Decide whether you want a static method or class method or instance method and call it appropriately . Also when you are using connection within the function it should be a member variable or passed to the method You dont have self as a parameter so you may be intending it as a static method - but you dont have it created as such