def build(crawl_subreddits, crawl_urls): urls = [] log.write("Building location list...", 'message') for subreddit in crawl_subreddits: for sort in subreddit['sort']: if sort == "all": sort = "" urls.append("http://www.reddit.com/r/" + subreddit['subreddit'] + "/" + sort + ".json") for url in crawl_urls: urls.append(url + ".json") for url in urls: try: cur.execute("select id from crawl_locations where url = %s", (url,)) if cur.rowcount > 0: cur.execute("update crawl_locations set last_seen = now() where url = %s", (url,)) else: cur.execute("""insert into crawl_locations ( url, last_seen, last_crawled ) values (%s, now(), 0)""", ( url )) db.commit() except Exception, e: log.write('Error storing location: ' + url + ': %s' % e, 'error') db.rollback()
def getCommentTree(nodes, url, linkid, commentid, args, depth): global ccount for node in nodes: try: if node is None: break elif node['kind'] == 't1': try: cur.execute("""replace into t1 ( id, link_id, parent_id, body, author, created, last_seen ) values (%s, %s, %s, %s, %s, %s, now())""", ( lib.base36decode(node['data']['id']), node['data']['link_id'], node['data']['parent_id'], node['data']['body'], node['data']['author'], datetime.datetime.fromtimestamp(node['data']['created_utc']) )) db.commit() ccount += 1 if node['data']['replies'] != "": getCommentTree([node['data']['replies']], url, linkid, commentid, args, depth) except Exception, e: log.write('Error storing t1_' + node['data']['id'] + ': %s' % e, 'error') db.rollback() elif node['kind'] == "Listing": getCommentTree(node['data']['children'], url, linkid, commentid, args, depth) elif node['kind'] == "more": if _['autoget_lte_20'] and node['data']['count'] <= 20 and node['data']['count'] >= _['autoget_threshold']: children = ",".join(node['data']['children']) time.sleep(_['sleep']) get('http://www.reddit.com/api/morechildren/', linkid, "", "api_type=json&depth=8&link_id=%s&children=%s" % (linkid, children), 0, True) elif node['data']['count'] >= _['comment_traverse_threshold']: if node['data']['parent_id'] == linkid or node['data']['parent_id'] == commentid: #sibling traversal breadth = 0 for child in node['data']['children']: if breadth >= _['comment_siblings_total']: break time.sleep(_['sleep']) get(url, linkid, child, args, depth) breadth += 1 else: #child traversal time.sleep(_['sleep']) get(url, linkid, node['data']['parent_id'][3:], args, depth + 1)
permalink, content, author, created, last_seen, last_crawled ) values (%s, %s, %s, %s, %s, %s, %s, now(), 0)""", ( lib.base36decode(l['data']['id']), l['data']['title'], l['data']['url'], l['data']['permalink'], content, l['data']['author'], datetime.datetime.fromtimestamp(l['data']['created_utc']) )) db.commit() except Exception, e: log.write('Error storing t3_' + l['data']['id'] + ': %s' % e, 'error') db.rollback() except Exception, e: log.write('Error checking links file node type: %s' % e, 'error') #endfor l in links stats.linkTimes['counts'].append(len(links['data']['children'])) stats.linkTimes['times'].append(time.time() - start) time.sleep(_['sleep'])
from init import db, cur import comments import lib import links import locations import log import respond import stats import user # Delete old links and comments if 'runall' in argv or 'cleanup' in argv: if _['delete_links_after'] > -1: cur.execute("delete from t3 where created < date_sub(now(), interval %s second)", (_['delete_links_after'],)) if _['delete_comments_after'] > -1: cur.execute("delete from t1 where created < date_sub(now(), interval %s second)", (_['delete_comments_after'],)) db.commit(); # Build/store locations to retrieve links if 'runall' in argv or 'locations' in argv: locations.build(_['crawl_subreddits'], _['crawl_urls']) # Crawls URLS from locations if 'runall' in argv or 'links' in argv: cur.execute("select id, url from crawl_locations where last_crawled < date_sub(now(), interval %s second)", (_['find_links_after'],)) for l in cur.fetchall(): links.get("%s?limit=%d" % (l[1], _['links_per_page'])) cur.execute("update crawl_locations set last_crawled = now() where id = %s", (l[0],)) db.commit() # Crawl eligible links if 'runall' in argv or 'comments' in argv:
def input_action(data): input_user = data['user_name'] input_action = data['action_name'] input_key = data['key'] game = dataBaser.Game.query.filter_by(key=input_key).first() participant = dataBaser.Character.query.filter_by(user_name=input_user, game_id=game.id).frst() participant.action = input_action # Save changes to the database db.commit() if game.participants[0].action is not None and game.participants[ 1].action is not None: if participant.user_name == game.participants[0].user_name: opponent = game.participants[1] print("THEY HAVE BOTH ACTED!") me_act = participant.action you_act = opponent.action me_dmg = 0 you_dmg = 0 # The actions can either be 'p_attack', 'attack', or 'defense' if me_act is 'p_attack' and you_act is 'defense': participant.health -= 10 me_dmg = 10 elif you_act is 'p_attack' and me_act is 'defense': opponent.health -= 10 you_dmg = 10 elif me_act is 'attack' and you_act is 'defense': participant.health -= 10 me_dmg = 10 opponent.health -= 20 you_dmg = 20 elif you_act is 'attack' and me_act is 'defense': opponent.health -= 10 you_dmg = 10 participant.health -= 20 me_dmg = 20 elif me_act is 'p_attack' and you_act is 'attack': participant.health -= 20 me_dmg = 20 opponent.health -= 30 you_dmg = 30 elif you_act is 'p_attack' and me_act is 'attack': opponent.health -= 20 you_dmg = 20 participant.health -= 30 me_dmg = 30 # elif me_act is 'defense' and you_act is 'defense': # Nothing Happens elif me_act is 'attack' and you_act is 'attack': participant.health -= 20 me_dmg = 20 opponent.health -= 20 you_dmg = 20 elif me_act is 'p_attack' and you_act is 'p_attack': participant.health -= 30 me_dmg = 30 opponent.health -= 30 you_dmg = 30 emit('successful_action', { 'user_action': participant.action, 'opponent_action': opponent.action, 'user_hp': participant.health, 'opponent_hp': opponent.health, 'me_dmg': me_dmg, 'you_dmg': you_dmg }, room=input_key) # reset current action to default participant.action = "Nah" opponent.action = "Nah" # Save changes to the database db.commit()