How to use the sdv.connect_db function in sdv

To help you get started, we’ve selected a few sdv examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Sketchy502 / SDV-Summary / sdv / imgur.py View on Github external
def swapCodeForTokens(response):
	# takes dict of response parameters as input, like {'error':'blah blah'} or {'code':'blah blah','state':'blah blah'}
	db = app.connect_db()
	c=db.cursor()
	state = json.loads(response['state'])
	user_identifier = state['id']
	redir = state['redir']
	if 'error' in response:
		c.execute('UPDATE users SET imgur_json=NULL WHERE imgur_id='+app.app.sqlesc,(user_identifier,))
		db.commit()
		return {'success':False}
	# called at the server redirect when imgur returns the code
	client = ImgurClient(app.app.config['IMGUR_CLIENTID'],app.app.config['IMGUR_SECRET'])
	credentials = client.authorize(response['code'],'authorization_code')
	# print credentials
	if 'access_token' in credentials.keys() and 'refresh_token' in credentials.keys():
		db = app.connect_db()
		c = db.cursor()
		c.execute('UPDATE users SET imgur_json='+app.app.sqlesc+' WHERE imgur_id='+app.app.sqlesc,(json.dumps(credentials),user_identifier))
github Sketchy502 / SDV-Summary / sdv / imgur.py View on Github external
def getAuthUrl(userid,target=None):
	db = app.connect_db()
	c = db.cursor()
	iuid = unicode(uuid.uuid4())
	imgur_id = json.dumps({'id':iuid,'redir':target})
	c.execute('UPDATE users SET imgur_id='+app.app.sqlesc+' WHERE id='+app.app.sqlesc,(iuid,userid))
	db.commit()
	db.close()
	client = ImgurClient(app.app.config['IMGUR_CLIENTID'],app.app.config['IMGUR_SECRET'])
	authorization_url = client.get_auth_url('code')+'&state='+unicode(imgur_id)
	return authorization_url
github Sketchy502 / SDV-Summary / sdv / imageDrone.py View on Github external
def process_queue():
    start_time = time.time()
    records_handled = 0
    db = connect_db()
    cur = db.cursor()

    while True:
        cur.execute(
                sql.GET_TODO_TASKS,
                (True, 'process_image',)
        )
        tasks = cur.fetchall()
        db.commit()

        if tasks:
            for task in tasks:
                task_id = task[0]
                farm_id = task[2]

                data = save_from_id(farm_id, cur)
github Sketchy502 / SDV-Summary / sdv / imageDrone.py View on Github external
def process_plans():
    start_time = time.time()
    records_handled = 0
    db = connect_db()
    cur = db.cursor()
    while True:
        # cur.execute('SELECT * FROM todo WHERE task='+sqlesc+' AND currently_processing NOT TRUE',('process_image',))
        cur.execute(
                'UPDATE todo SET currently_processing=' + sqlesc + ' WHERE id=(SELECT id FROM todo WHERE task=' + sqlesc + ' AND currently_processing IS NOT TRUE LIMIT 1) RETURNING *',
                (True, 'process_plan_image',))
        tasks = cur.fetchall()
        db.commit()
        if len(tasks) != 0:
            for task in tasks:
                cur.execute('SELECT source_json, url, season FROM plans WHERE id=(' + sqlesc + ')',
                            (task[2],))
                result = cur.fetchone()
                farm_json = json.loads(result[0])
                url = result[1]
                season = 'spring' if result[2] == None else result[2]
github Sketchy502 / SDV-Summary / sdv / imgur.py View on Github external
def uploadToImgur(userid,url):
	db = app.connect_db()
	c = db.cursor()
	c.execute('SELECT map_url,name,farmname,date,imgur_json FROM playerinfo WHERE url='+app.app.sqlesc,(url,))
	result = c.fetchone()
	if result[4] != None:
		previous_upload_properties = json.loads(result[4])
		if time.time() < previous_upload_properties['upload_time']+(2*3600):
			return {'error':'too_soon','link':previous_upload_properties['imgur_url']}
	map_url = result[0]
	titlestring = u"{} Farm, {} by {}".format(result[2],result[3],result[1])
	descriptionstring = u"Stardew Valley game progress, full summary at http://upload.farm/{}".format(url)
	# try:
	c.execute('SELECT imgur_json FROM users WHERE id='+app.app.sqlesc,(userid,))
	r = json.loads(c.fetchone()[0])
	access_token = r['access_token']
	refresh_token = r['refresh_token']
	client = ImgurClient(app.app.config['IMGUR_CLIENTID'],app.app.config['IMGUR_SECRET'])
github Sketchy502 / SDV-Summary / sdv / migrateImageDirectory.py View on Github external
def getEntries(where=None):
	connection = connect_db()
	c = connection.cursor()
	if where==None:
		where=''
	c.execute('SELECT id, base_path, avatar_url, portrait_url, farm_url, map_url, thumb_url, url FROM playerinfo '+where)
	entries = c.fetchall()
	connection.close()
	return entries
github Sketchy502 / SDV-Summary / sdv / imgur.py View on Github external
def checkApiAccess(userid):
	# something that checks whether we have api keys and whether they work;
	# if not, return False
	db = app.connect_db()
	c = db.cursor()
	c.execute('SELECT imgur_json FROM users WHERE id='+app.app.sqlesc,(userid,))
	r = c.fetchone()
	if len(r) > 0:
		try:
			r = json.loads(r[0])
			access_token = r['access_token']
			refresh_token = r['refresh_token']
		except TypeError:
			return False
	else:
		return False
	client = ImgurClient(app.app.config['IMGUR_CLIENTID'],app.app.config['IMGUR_SECRET'])
	client.set_user_auth(access_token,refresh_token)
	try:
		client.get_account('me').url
github Sketchy502 / SDV-Summary / sdv / migrateImageDirectory.py View on Github external
if url == None:
		return None

	base_subfolder = str(int(math.floor(int(rowid)/app.config.get('IMAGE_MAX_PER_FOLDER'))))
	base_path = os.path.join(app.config.get('IMAGE_FOLDER'), base_subfolder, url)

	if base_path == original_base_path:
		return None

	try:
		os.makedirs(legacy_location(base_path))
	except OSError:
		pass

	try:
		connection = connect_db()
		cur = connection.cursor()

		avatar_paths = [original_avatar_path, os.path.join(base_path, url+'-a.png')]
		portrait_paths = [original_portrait_path, os.path.join(base_path, url+'-p.png')]
		farm_paths = [original_farm_path, os.path.join(base_path, url+'-f.png')]
		map_paths = [original_map_path, os.path.join(base_path, url+'-m.png')]
		thumb_paths = [original_thumb_path, os.path.join(base_path, url+'-t.png')]

		for original, new in [avatar_paths,portrait_paths,farm_paths,map_paths,thumb_paths]:
			try:
				shutil.move(legacy_location(original),legacy_location(new))
			except IOError:
				pass
		try:
			os.rmdir(legacy_location(original_base_path))
		except WindowsError: