Пример #1
0
def livestream(subreddit=None,
               username=None,
               sleepy=30,
               limit=100,
               submissions=True,
               comments=False,
               debug=False):
    '''
    Continuously get posts from this source
    and insert them into the database
    '''
    if subreddit is None and username is None:
        print('Enter username or subreddit parameter')
        return
    if subreddit is not None and username is not None:
        print('Enter subreddit OR username, not both')
        return

    if subreddit:
        print('Getting subreddit %s' % subreddit)
        sql = sqlite3.connect(DATABASE_SUBREDDIT % subreddit)
        item = r.get_subreddit(subreddit)
        submissions = item.get_new if submissions else None
        comments = item.get_comments if comments else None
    else:
        print('Getting redditor %s' % username)
        sql = sqlite3.connect(DATABASE_USER % username)
        item = r.get_redditor(username)
        submissions = item.get_submitted if submissions else None
        comments = item.get_comments if comments else None

    livestreamer = lambda *a, **k: livestream_helper(submissions, comments,
                                                     debug, *a, **k)

    cur = sql.cursor()
    while True:
        try:
            bot.refresh(r)  # personal use
            items = livestreamer(limit=limit)
            newitems = smartinsert(sql, cur, items)
            print('%s +%d' % (humannow(), newitems), end='')
            if newitems == 0:
                print('\r', end='')
            else:
                print()
            if debug:
                print('Loop finished.')
            time.sleep(sleepy)
        except KeyboardInterrupt:
            print()
            sql.commit()
            sql.close()
            del cur
            del sql
            return
        except Exception as e:
            traceback.print_exc()
            time.sleep(5)
Пример #2
0
def livestream(subreddit=None, username=None, sleepy=30, limit=100, submissions=True, comments=False, debug=False):
    '''
    Continuously get posts from this source
    and insert them into the database
    '''
    if subreddit is None and username is None:
        print('Enter username or subreddit parameter')
        return
    if subreddit is not None and username is not None:
        print('Enter subreddit OR username, not both')
        return

    if subreddit:
        print('Getting subreddit %s' % subreddit)
        sql = sqlite3.connect(DATABASE_SUBREDDIT % subreddit)
        item = r.get_subreddit(subreddit)
        submissions = item.get_new if submissions else None
        comments = item.get_comments if comments else None
    else:
        print('Getting redditor %s' % username)
        sql = sqlite3.connect(DATABASE_USER % username)
        item = r.get_redditor(username)
        submissions = item.get_submitted if submissions else None
        comments = item.get_comments if comments else None
    
    livestreamer = lambda *a, **k: livestream_helper(submissions, comments, debug, *a, **k)

    cur = sql.cursor()
    while True:
        try:
            bot.refresh(r)  # personal use
            items = livestreamer(limit=limit)
            newitems = smartinsert(sql, cur, items)
            print('%s +%d' % (humannow(), newitems), end='')
            if newitems == 0:
                print('\r', end='')
            else:
                print()
            if debug:
                print('Loop finished.')
            time.sleep(sleepy)
        except KeyboardInterrupt:
            print()
            sql.commit()
            sql.close()
            del cur
            del sql
            return
        except Exception as e:
            traceback.print_exc()
            time.sleep(5)
Пример #3
0
def manage():
    hundredg = get_hundred()
    for hundred in hundredg:
        bot.refresh(r)
        print('checking %s - %s %d' %
              (hundred[0][SQL_NAME], hundred[-1][SQL_NAME],
               hundred[-1][SQL_SUBSCRIBERS]))
        subreddits = r.get_info(
            thing_id=['t5_' + x[SQL_IDSTR] for x in hundred])
        hundred.sort(key=lambda x: x[SQL_CREATED])
        subreddits.sort(key=lambda x: x.created_utc)

        for sub in subreddits:
            for item in hundred:
                if item[SQL_IDSTR] == sub.id:
                    sub.previous = item[SQL_SUBREDDIT_TYPE]
                    sub.psubs = item[SQL_SUBSCRIBERS]

        for sub in subreddits:
            if sub.previous == 2:
                continue

            idint = b36(sub.id)
            cur2.execute('SELECT * FROM amageddon WHERE idint == ?', [idint])
            f = cur2.fetchone()
            if f:
                if sub.subreddit_type == 'private' and f[-1] is not None:
                    cur2.execute(
                        'UPDATE amageddon SET returntime=? WHERE idint=?',
                        [None, idint])
                    print('  [PRIVATE] %s' % sub.display_name)
                    continue
                if sub.subreddit_type == 'private' or f[-1] is not None:
                    continue
                returntime = int(now())
                print('     [OPEN] %s' % sub.display_name)
                cur2.execute('UPDATE amageddon SET returntime=? WHERE idint=?',
                             [returntime, idint])
            else:
                if sub.subreddit_type != 'private':
                    continue
                data = [idint, sub.id, sub.display_name, sub.psubs, None]
                print('  [PRIVATE] %s' % sub.display_name)
                cur2.execute('INSERT INTO amageddon VALUES(?, ?, ?, ?, ?)',
                             data)
        sql.commit()
Пример #4
0
def livestream(subreddit=None, username=None, sleepy=30, limit=100):
    '''
    Continuously get posts from this source
    and insert them into the database
    '''

    if subreddit is None and username is None:
        print('Enter username or subreddit parameter')
        return
    if subreddit is not None and username is not None:
        print('Enter subreddit OR username, not both')
        return

    if subreddit:
        print('Getting subreddit %s' % subreddit)
        sql = sqlite3.connect(DATABASE_SUBREDDIT % subreddit)
        item = r.get_subreddit(subreddit)
        itemf = item.get_new
    else:
        print('Getting redditor %s' % username)
        sql = sqlite3.connect(DATABASE_USER % username)
        item = r.get_redditor(username)
        itemf = item.get_submitted
    cur = sql.cursor()
    while True:
        bot.refresh(r)  # personal use
        try:
            items = list(itemf(limit=limit))
            newitems = smartinsert(sql, cur, items)
            print('%s +%d' % (humannow(), newitems), end='')
            if newitems == 0:
                print('\r', end='')
            else:
                print()
            time.sleep(sleepy)
        except KeyboardInterrupt:
            print()
            sql.commit()
            sql.close()
            del cur
            del sql
            return
        except Exception as e:
            traceback.print_exc()
            time.sleep(5)
Пример #5
0
def manage():
	hundredg = get_hundred()
	for hundred in hundredg:
		bot.refresh(r)
		print('checking %s - %s %d' % (hundred[0][SQL_NAME], hundred[-1][SQL_NAME], hundred[-1][SQL_SUBSCRIBERS]))
		subreddits = r.get_info(thing_id=['t5_' + x[SQL_IDSTR] for x in hundred])
		hundred.sort(key=lambda x: x[SQL_CREATED])
		subreddits.sort(key=lambda x: x.created_utc)

		for sub in subreddits:
			for item in hundred:
				if item[SQL_IDSTR] == sub.id:
					sub.previous = item[SQL_SUBREDDIT_TYPE]
					sub.psubs = item[SQL_SUBSCRIBERS]

		for sub in subreddits:
			if sub.previous == 2:
				continue

			idint = b36(sub.id)
			cur2.execute('SELECT * FROM amageddon WHERE idint == ?', [idint])
			f = cur2.fetchone()
			if f:
				if sub.subreddit_type == 'private' and f[-1] is not None:
					cur2.execute('UPDATE amageddon SET returntime=? WHERE idint=?', [None, idint])
					print('  [PRIVATE] %s' % sub.display_name)
					continue
				if sub.subreddit_type == 'private' or f[-1] is not None:
					continue
				returntime = int(now())
				print('     [OPEN] %s' % sub.display_name)
				cur2.execute('UPDATE amageddon SET returntime=? WHERE idint=?', [returntime, idint])
			else:
				if sub.subreddit_type != 'private':
					continue
				data = [idint, sub.id, sub.display_name, sub.psubs, None]
				print('  [PRIVATE] %s' % sub.display_name)
				cur2.execute('INSERT INTO amageddon VALUES(?, ?, ?, ?, ?)', data)
		sql.commit()