예제 #1
0
def get_whole_page_content(amount):
	'''
	get_whold_page_content()
	amount : get_page_amount()
	return : write sqlite
	'''
	count = 0
	for page_num in range(1, amount+1):
		print u'Scraping page <%3d>...' % page_num


		url = build_url(page_num)

		print u'Now in: %s' % url

		# get page_content
		content_lst = get_content_from_page(url)

		print u'Get <%3d> pices from this page.\n' % len(content_lst)

		for author, content in content_lst:
			content_obj = Hot(author=author, content=content)
			session.add(content_obj)
			count += 1

		if count >= 100:
			session.commit()
			count = 0

		# couldn't be too soon
		time.sleep(3)

	session.commit()
	return 
예제 #2
0
def recieve_message():
    try:
        message = request.form.get('message')
        json_resp = False
        if not message:
            message = request.get_json()['message']
            json_resp = True

        message_data = Message(message=message)
        session.add(message_data)
        session.commit()

        if json_resp:
            resp = {
                'status': 'success',
                'message': message
            }
            return jsonify(**resp)
        else:
            return redirect(url_for('dashboard'))


    except Exception as e:
        resp = {
            'status': 'failed',
            'error': str(e)
        }

        return jsonify(**resp)
예제 #3
0
def get_page_links2():
    '''
        Description: get images' second layer links
    '''
    pageLink = page_link1_queue.get()
    print "page links2 process id:%s" %os.getpid()
    print "Starting to crawl : %s" %pageLink
    if pageLink:
        #picture_urls = []  
        response = requests.get(pageLink, headers=headers) 
        soup = BeautifulSoup(response.text, "html.parser")
        picture_divs = soup.find_all("div", {"class":"pic"})
        for picture_div in picture_divs:
            picture_url = picture_div.find("a").get("href")
            page_link2_queue.put(picture_url)
            #picture_urls.append(picture_url)

            query = session.query(SecondLevelLinks)
            query_result = query.filter(SecondLevelLinks.url==picture_url).first()
            if query_result:
                continue
            else:
                second_level_links = SecondLevelLinks(url=picture_url)
                session.add(second_level_links)
        session.flush()
        session.commit()

        return page_link2_queue
    else:
        return None
예제 #4
0
    def addEdit(self, _thread_id, _content, _submitted=False):
        existingList = (
            session.query(bot_comments_anetpool)
            .filter_by(thread_id=_thread_id)
            .order_by(desc(bot_comments_anetpool.edit_id))
            .first()
        )

        if existingList is None:
            row = bot_comments_anetpool()
            row.thread_id = _thread_id
            _from_template = self.anetpool_template.split("&#009;", 1)
            row.content = _from_template[0] + _content + "&#009;" + _from_template[1]
            row.submitted = _submitted
            session.add(row)
        else:
            if len(existingList.content) + len(_content) >= 10000:
                row = bot_comments_anetpool()
                row.thread_id = _thread_id
                _from_template = self.anetpool_template.split("&#009;", 1)
                row.content = _from_template[0] + _content + "&#009;" + _from_template[1]
                row.submitted_id = "e" + str(existingList.edit_id)
                row.submitted = _submitted
                session.add(row)
            else:
                _from_save = existingList.content.split("&#009;", 1)
                session.query(bot_comments_anetpool).filter_by(edit_id=existingList.edit_id).update(
                    {"content": _from_save[0] + _content + "&#009;" + _from_save[1], "submitted": False}
                )
        session.commit()
예제 #5
0
def item_edit(item_title):
    categories = session.query(Category).all()
    item = session.query(Item).filter_by(
        title=item_title).one()
    user = session.query(User).filter_by(
        id=login_session.get('user_id')).one()
    if item.user is not None and item.user.id != user.id:
        return redirect(url_for('home'))
    if request.method == 'POST':
        form = vh.ItemForm(request.form, item)
        file_storage_err = vh.store_item_pic(
            item, request.files['picture'])
        if (not form.validate() or file_storage_err is not None):
            return render_template('item_edit.html',
                                   form=form,
                                   file_err=file_storage_err)
        form.populate_obj(item)
        try:
            session.add(item)
            session.commit()
        except ValueError as e:
            return "Database validation error: " + str(e)
        except SQLAlchemyError as e:
            # todo: log error, but don't display detailed message
            # for security reasons
            return "Database error: " + str(e)
        return redirect(url_for('home'))
    else:
        form = vh.ItemForm(obj=item)
        return render_template('item_edit.html',
                               form=form,
                               file_err=None)
예제 #6
0
    def exec_global(self, request):
        """
        Execute a global-scoped command.
        """

        command = request["command"]

        if command == "go back to perm":
            liq.set_var("selected", "perm")
            liq.send_command("perm-switch.skip")

            self.send_feedback(request)

        elif command == "rescan":
            result = Utilities.rescan()
            self.send_feedback(request, result)

        elif command == "select":
            studio = session.query(Studio).find_by(slug=request["studio"].decode('utf-8'))

            if studio is not None:
                studio.selected = True
                session.add(studio)
                session.commit()
                liq.set_var("selected", str(studio.slug))
                sylog("INFO: Selecting " + studio.slug)
                self.send_feedback(request)
            else:
                self.send_feedback(request, "error", "Unknown studio %s" % request["studio"])
예제 #7
0
 def add_comment(self, _thing_id, _content, _submitted=False, _signature=None):
     if _signature is None:
         _signature = self.signature
     last_id = _thing_id
     extra_len = len("\n\n--- continued below ---") + len(_signature)
     while True:
         if len(_content) <= 0:
             return
         content_parts = _content.split("\n")
         stiched_content = ""
         for part in content_parts:
             if len(stiched_content) + len(part + "\n") + extra_len <= 10000:
                 stiched_content += part + "\n"
             else:
                 break
         row = bot_comments()
         row.thing_id = last_id
         row.submitted = _submitted
         if _content[len(stiched_content) :] == "":
             row.content = stiched_content + _signature
         else:
             row.content = stiched_content + "\n\n--- continued below ---" + _signature
         session.add(row)
         session.commit()
         last_id = "i{}".format(session.query(bot_comments).order_by(desc(bot_comments.id)).first().id)
         _content = _content[len(stiched_content) :]
예제 #8
0
    def __commit(self):
        """
        """
        try:
            handles = [i.handle for i in self.motifs]
            if len(handles) != len(set(handles)):
                pdb.set_trace()

            r = session.query(NR_release).filter(NR_release.id == self.release.id).first()
            if not r:
                session.add(self.release)

            session.add_all(self.motifs)
            session.add_all(self.loops)
            session.add_all(self.history)
            session.add_all(self.intersection)
            session.add_all(self.release_diff)

            session.commit()
            logging.info("Successful update")
        except sqlalchemy.exc.SQLAlchemyError, e:
            logging.error("Update failed. SQLAlchemy error. Rolling back.")
            logging.error(str(e))
            session.rollback()
            self.remove_release(self.release.id)
            sys.exit()
예제 #9
0
def websocket_to_database():
    try:
        websocket = yield from websockets.connect("wss://ws-feed.exchange.coinbase.com")
    except gaierror:
        db_logger.error('socket.gaierror - had a problem connecting to Coinbase feed')
        return
    yield from websocket.send('{"type": "subscribe", "product_id": "BTC-USD"}')
    while True:
        message = yield from websocket.recv()
        if message is None:
            file_logger.error('Websocket message is None!')
            break
        try:
            message = json.loads(message)
        except TypeError:
            db_logger.error('JSON did not load, see ' + str(message))
            continue
        if message['type'] != 'match':
            continue
        new_message = Messages()
        for key in message:
            if hasattr(new_message, key):
                setattr(new_message, key, message[key])
            else:
                db_logger.error(str(key) + ' is missing, see ' + str(message))
                continue
        try:
            session.add(new_message)
            session.commit()
        except IntegrityError:
            session.rollback()
        except DatabaseError:
            file_logger.error('Database Error')
            session.rollback()
예제 #10
0
파일: app.py 프로젝트: NSkelsey/verdant
def mod():
    global __HEXDIGEST__
    if request.method == "GET":
        mess = session.query(EmailMessage).get(__HEXDIGEST__)
        if mess is None or mess.approved == True:
            flash('No new messages to edit')
            return render_template('edit.html')
        dct = json.loads(mess.everything)
        form = EditForm()
        form.subject.data = dct.get('subject')
        form.text.data = dct.get('body-plain')
        form.approved.data = mess.approved
        return render_template('edit.html', form=form) 
    else:
        form = EditForm(request.form)
        mess = session.query(EmailMessage).get(__HEXDIGEST__)
        if form.approved.data == True:
            send_message(form.data)
            mess.approved = True
            session.add(mess)
            session.commit()
            flash('message sent')
            return render_template('edit.html')
        flash("bad form")
        return "bad form"
def load_playlists(session):
    user = 1
    playlist_titles = ["Narrative Shorts", "Something Different", "Hellbent Sports", "Documentaries"]
    for title in playlist_titles:
        playlist = models.Playlists(title=title, user_id=user)
        session.add(playlist)
    session.commit()
예제 #12
0
파일: test.py 프로젝트: EmuxEvans/py_learn
def insert_test():
    tom = Teacher(name='Tom', subject='Physics')
    session.add(tom)
    session.commit()
    lucy = Student(name='Lucy', head_teacher_id=tom.id)
    session.add(lucy)
    session.commit()
예제 #13
0
파일: app.py 프로젝트: NSkelsey/verdant
def message_good(form):
    global __HEXDIGEST__
    _json = json.dumps(form.to_dict())
    mess = EmailMessage(_json)
    mess.hexdigest = __HEXDIGEST__
    session.add(mess)
    session.commit()
def guildwars2_filter_cm(comments, array_anet_names):
    for cm in comments:
        logging.info("comment")
        if cm.author.name in array_anet_names:
            logging.info("comment from anet: " + cm.name)
            row = bot_submissions()
            title = cm.link_title
            if (len(title) + len(cm.author.name) + 3) > 300:
                title = title[: 300 - len(cm.author.name) - 3 - 3]
                title += "..."
            row.title = title + " [" + cm.author.name + "]"
            row.type = "link"
            row.subreddit = "gw2devtrack"
            row.submitted = False
            row.content = cm.permalink.replace("//www.reddit.com", "//np.reddit.com") + "?context=1000"
            session.add(row)
        continue  # DISALLOWS COMMENTS TO BE PARSED FPR GW2 LINKS
        if re.search("http.*?:\/\/.*?guildwars2.com\/", cm.body) != None:
            logging.info("comment with gw2 link: " + cm.name)
            all_links = re.findall("http.*?:\/\/.*?guildwars2.com\/[^ \])\s]*", cm.body)
            for link in all_links:
                if link != "":
                    try:
                        prepare_comment(cm.name, False, guildwars2.locate_origin(link))
                    except Exception as e:
                        logging.error(e)
                        session.rollback()
                    else:
                        session.commit()
예제 #15
0
파일: seed.py 프로젝트: bessiec/SNAPBasket
def load_baskets(session):
	user = 1
	basket_names = ["Example 1"]
	for name in basket_names:
		name = models.Baskets(name=name)
		session.add(name)
	session.commit()
예제 #16
0
def addToDatabase(parent, subtree, i):

    subComments = subtree["data"]["children"]
    if len(subComments) == 0:
        pass
        # print "List EMPTY"
    w = 0
    for j in subComments:
        comment = Comment()
        comment.reddit_id = j["data"]["id"]
        comment.text = j["data"]["body"]
        comment.user_name = j["data"]["author"]
        comment.upvotes = int(j["data"]["ups"])
        comment.downvotes = j["data"]["downs"]
        comment.date_of_last_sweep = datetime.now()
        comment.parent = parent
        comment.post_id = parent.post_id
        session.add(comment)
        if j["data"]["replies"] == "":
            comment.weight = comment.upvotes
            # print "Dict Empty"
            # print type(j["data"]["replies"])

        else:
            addToDatabase(comment, j["data"]["replies"], i + 1)
            comment.weight = comment.weight + comment.upvotes
        w = comment.weight + w
    parent.weight = w

    """
예제 #17
0
파일: shell.py 프로젝트: realjade/match
def add_admin(emailormobile):
    user = session.query(User).filter(or_(User.email == emailormobile,User.mobile == emailormobile)).first()
    if not user:
        raise
    admin = Admin(admin_id = ut.create_admin_id(),user_id = user.user_id,created = time.time()*1000,updated = time.time()*1000)
    session.add(admin)
    session.commit()
예제 #18
0
def get_page_links1(webUrl):
    '''
        Description: get images' first layer links
    '''
    init_db()
    htmlContent = requests.get(webUrl, headers=headers)
    soup = BeautifulSoup(htmlContent.text, "html.parser")
    wp_page_numbers_div = soup.find("div", {"id":"wp_page_numbers"})
    endPageTag = wp_page_numbers_div.find_all("a")[-1]
    endPageLink = endPageTag.get('href')
    if endPageLink: 
        regex = r"(\D+\d+\D+)(\d+)(\D+)"
        m = re.match(regex, endPageLink)
        if m:
            pageNumber = int(m.groups()[1])  #get page number
            for index in xrange(1, pageNumber+1):
                pageLink = "%s"*4 %(webUrl, m.group(1), index, m.group(3))
                #pageLinks.append(pageLink)
                page_link1_queue.put(pageLink)
                query = session.query(FirstLevelLinks)
                query_result = query.filter(FirstLevelLinks.url==pageLink).first()
                if query_result:
                    continue 
                else:
                    first_level_links = FirstLevelLinks(url=pageLink)
                    session.add(first_level_links)
            session.flush()
            session.commit()
            return page_link1_queue
        else:
            return None
 def task_shop(self, grab, task):
     lines = grab.xpath_list('//div[@id="allStores"]/ul/li/a')
     for line in lines:
         title = line.text_content()
         cost = line.tail.strip()
         data = Data(site_name=task.site_name, title=title.encode('utf-8'), cost=cost.encode('utf-8'))
         session.add(data)
     session.commit()
예제 #20
0
def get_or_create(model, **params):
    instance = session.query(model).filter_by(**params).first()
    if instance:
        return instance
    else:
        instance = model(**params)
        session.add(instance)
        return instance
    def saveOneRow(self):
        data = Data(description=self.description.encode('utf-8'), link=self.link, stock=self.inStock.encode('utf-8'),
                    shipping=self.shipping.encode('utf-8'), price=self.price.encode('utf-8'),
                    mail_rebate=self.rebate.encode('utf-8'), instant_rebate=self.instantRebate.encode('utf-8'),
                    final_price=self.finalPrice.encode('utf-8'), item_number=self.itemNo.encode('utf-8'))

        session.add(data)
        session.commit()
예제 #22
0
파일: seed.py 프로젝트: bessiec/SNAPBasket
def load_basket_entries(session):
	with open ('baskets.csv', 'rU') as f:
		filename_stores = csv.reader(f, delimiter=",")
		for basketname_row in filename_stores:
			new_basketname_row = models.Basket_Entry(food_id=basketname_row[1], basket_id=basketname_row[2])
			session.add(new_basketname_row)
		session.commit()
		session.refresh(new_basketname_row)
 def task_ultimate(self, grab, task):
     lines = grab.xpath_list('//div[@class="mn_srchListSection"]/ul/li')
     for line in lines:
         title = line.getchildren()[0].text_content()
         cost = line.getchildren()[2].text_content().strip()
         data = Data(site_name=task.site_name, title=title.encode('utf8'), cost=cost.encode('utf-8'))
         session.add(data)
     session.commit()
 def task_retailers(self, grab, task):
     lines = grab.xpath_list('//div[@class="merch-full"]/a')
     for line in lines:
         title = line[1].text_content()
         cost = line[2].text_content().strip()
         data = Data(site_name=task.site_name, title=title.encode('utf-8'), cost=cost.encode('utf-8'))
         session.add(data)
     session.commit()
def load_playlist_entries(session):
    with open ('stock_playlists.csv', 'rU') as f:
        filename_playlists = csv.reader(f, delimiter=",")
        for playlist_row in filename_playlists:
            new_playlist_row = models.Playlist_Entry( playlist_id=playlist_row[1],
                film_id=playlist_row[2], play_order=playlist_row[3])
            session.add(new_playlist_row)
        session.commit()
예제 #26
0
파일: seed.py 프로젝트: bessiec/SNAPBasket
def load_food(session):
	with open ('food.csv', 'rU') as f:
		filename_food = csv.reader(f, delimiter=",")
		for food_row in filename_food:
			new_food_row = models.Food(name=food_row[0], brand=food_row[1], measurement=food_row[2], 
				price=food_row[3], store_name=food_row[4], location=food_row[5], transit=food_row[6])
			session.add(new_food_row)
		session.commit()
		session.refresh(new_food_row)
예제 #27
0
def scrapeThread(post):
    bd = getJson(post.url)
    c = bd[1]
    parent = Comment(post_id=post.id, user_name="TOPMACHINE")
    buildTree(post, c)
    addToDatabase(parent, c, 0)

    session.add(parent)
    session.commit()
def load_films(session):
    with open ('films.csv','rU') as f:
        filename_films = csv.reader(f, delimiter=',')
        for film_row in filename_films:
            new_film_row = models.Films(title=film_row[0],genre=film_row[1], url=film_row[2], 
                link_type=film_row[3], embed=film_row[4])
            session.add(new_film_row)
        session.commit()
        session.refresh(new_film_row)
예제 #29
0
 def post(self):
     user_id = int(self.get_arguments('user_id'))
     lon_list = [Decimal(each) for each in self.get_argument('lon_list').split(',')]
     lat_list = [Decimal(each) for each in self.get_argument('lat_list').split(',')]
     loc_time_list = [datetime.fromtimestamp(int(each)) for each in self.get_arguments('loc_time_list').split(',')]
     for idx, lon in enumerate(lon_list):
         session.add(Location(**{'user_id': user_id, 'lon': lon_list[idx], 'lat': lat_list[idx], 'loc_time': loc_time_list[idx]}))
     session.commit()
     self.write({'result': True})
예제 #30
0
def addPostToDatabase(thread):
    post = Post()
    post.name =  thread["data"]["title"]
    post.url =  "http://www.reddit.com" + thread["data"]["permalink"]
    post.upvotes =  thread["data"]["ups"]
    post.downs = thread["data"]["downs"]
    session.add(post)
    session.commit()
    return post
예제 #31
0
def add_csv():
    with open('suggested_books.csv') as csvfile:
        data = csv.reader(csvfile)
        for row in data:
            book_in_db = session.query(Book).filter(
                Book.title == row[0]).one_or_none()
            if book_in_db == None:
                title = row[0]
                author = row[1]
                date = clean_date(row[2])
                price = clean_price(row[3])
                new_book = Book(title=title,
                                author=author,
                                published_date=date,
                                price=price)
                session.add(new_book)
        session.commit()
def create_person(data: dict):
    if 'name' not in data or not data['name']:
        return Response.wrong_format({"message": "Name missing!"})
    p1 = Person()
    p1.name = data['name']
    # TODO: Encpyt Passwords
    p1.password = ""
    p1.mail = data['email']
    p1.is_present = False
    p1.role = "0"
    session.add(p1)
    try:
        session.commit()
    except:
        Response.database_error()
    response = model_as_dict(p1)
    return Response.ok(response)
예제 #33
0
    async def enable(self, ctx):
        """Enable birthday messages on the current channel.

        Note: Only one channel per server, using in another channel will update
        the channel to be used.
        """
        result = session.query(BDay).filter_by(guild_id=ctx.guild.id).first()
        if result:
            result.channel_id = ctx.channel.id
            return await ctx.reply(
                f"Birthday notification channel birthday changed to {ctx.channel.mention}."
            )
        session.add(BDay(guild_id=ctx.guild.id, channel_id=ctx.channel.id))
        session.commit()
        return await ctx.reply(
            f"Birthday notification channel birthday enabled to {ctx.channel.mention}."
        )
예제 #34
0
async def sightings_post(request: Request) -> Response:
    """
    ---
    summary: Post device sighting
    description: This endpoint allows users to store sightings of devices on the local network
    tags:
    - Sightings
    produces:
    - application/json
    parameters:
    - in: body
      name: body
      schema:
        type: object
        properties:
          timestamp:
            type: integer
            format: int64
          alias:
            type: string
      description: A sighting to store in the KnockKnock backend
      required: true
    responses:
        "200":
            description: Sighting has been stored successfully, return sighting as it was persisted
        "400":
            description: Invalid ``from`` parameter or unable to parse the request body
    """
    # Try to parse the Request body.
    try:
        data = await request.json()
        sighting = Sighting(alias=data["alias"],
                            last_activity_timestamp=data["timestamp"])
    except BaseException as e:
        return Response(status=400, reason=f"Unable to parse body ({e})")

    session.add(sighting)
    session.commit()

    records = session.query(Sighting).filter(
        Sighting.id == session.query(func.max(Sighting.id)))

    return Response(status=200,
                    body=encode(records[0].to_json()),
                    content_type="application/json")
예제 #35
0
def statSite(params):
    date = params['date']
    date_from = params['date_from']
    date_to = params['date_to']
    logging.info( "act:{0},from:{1},to:{2}".format("site统计",date_from,date_to))

    stat_pay = session.query(func.sum(PayOrder.total_price).label("total_pay_money")) \
        .filter(PayOrder.status == 1) \
        .filter(PayOrder.created_time >= date_from, PayOrder.created_time <= date_to).first()

    stat_member_count = session.query(Member).count()
    stat_new_member_count = session.query(Member).filter(Member.created_time >= date_from,
                        Member.created_time <= date_to).count()

    stat_order_count = session.query(PayOrder).filter_by( status = 1 )\
        .filter(PayOrder.created_time >= date_from, PayOrder.created_time <= date_to)\
        .count()

    stat_share_count = session.query(WxShareHistory).filter(WxShareHistory.created_time >= date_from
        , WxShareHistory.created_time <= date_to).count()

    tmp_stat_site = session.query(StatDailySite).filter_by(date=date).first()
    if tmp_stat_site:
        tmp_model_stat_site = tmp_stat_site
    else:
        tmp_model_stat_site = StatDailySite()
        tmp_model_stat_site.date = date
        tmp_model_stat_site.created_time = getCurrentDate()

    tmp_model_stat_site.total_pay_money = stat_pay[ 0 ] if stat_pay[ 0 ] else 0.00
    tmp_model_stat_site.total_new_member_count = stat_new_member_count
    tmp_model_stat_site.total_member_count = stat_member_count
    tmp_model_stat_site.total_order_count = stat_order_count
    tmp_model_stat_site.total_shared_count = stat_share_count
    tmp_model_stat_site.updated_time = getCurrentDate()
    '''
    为了测试效果模拟数据,生产环境下请注释掉
    '''
    tmp_model_stat_site.total_pay_money = random.randint(1000, 1010)
    tmp_model_stat_site.total_new_member_count = random.randint(50, 100)
    tmp_model_stat_site.total_member_count += tmp_model_stat_site.total_new_member_count
    tmp_model_stat_site.total_order_count = random.randint(900, 1000)
    tmp_model_stat_site.total_shared_count = random.randint(1000, 2000)
    session.add(tmp_model_stat_site)
    session.commit()
예제 #36
0
    def setStockChangeLog(food_id=0, quantity=0, note=''):

        if food_id < 1:
            return False

        food_info = session.query(Food).filter_by(id=food_id).first()
        if not food_info:
            return False

        model_stock_change = FoodStockChangeLog()
        model_stock_change.food_id = food_id
        model_stock_change.unit = quantity
        model_stock_change.total_stock = food_info.stock
        model_stock_change.note = note
        model_stock_change.created_time = getCurrentDate()
        session.add(model_stock_change)
        session.commit()
        return True
예제 #37
0
파일: rong360.py 프로젝트: Moxikai/rong360
    def saveProductToSQLite(self, **kwargs):

        product = Product(
            id=kwargs['id'],
            name=kwargs['name'],
            annualizedReturn=kwargs['annualizedReturn'],
            cycle=kwargs['cycle'],
            remainAmount=kwargs['remainAmount'],
            platform_id=kwargs['platform_id'],
        )
        if not session.query(Product).filter(
                Product.id == kwargs['id']).first():

            session.add(product)
            session.commit()
            print '产品-------%s-------信息保存成功!' % kwargs['name']
        else:
            print '产品-------%s--------信息已存在,无需重复保存!' % kwargs['name']
 def post(self):
     data = request.get_json()
     username = data['username']
     password = data['password']
     if username is None or password is None:
         abort(400)  # missing arguments
     if session.query(User).filter_by(
             username=username).first() is not None:
         abort(400)  # existing user
     user = User(username=username)
     user.hash_password(password)
     session.add(user)
     session.commit()
     if session.query(User).filter_by(
             username=username).first() is not None:
         return jsonify({'username': user.username, 'id': user.id})
     else:
         return jsonify({'error': "The user request was not completed."})
예제 #39
0
    def add(cls, jid, nick=None, show=None):
        """ 添加一个成员
        Arguments:
            `jid`   -   成员jid
            `nick`  -   昵称
            `show`  -   stanze.show
        """
        if cls.get_one(jid):
            return
        m = Member(jid, nick)
        m.status = [Status(show, jid.resource)]
        try:
            session.add(m)
            session.commit()
        except:
            session.rollback()

        return m
예제 #40
0
    def join_handler(self, data):
        """Handle user join packets from Beam."""

        user = session.query(User).filter_by(id=data["id"]).first()

        if not user:
            user = User(id=data["id"], joins=1)
        else:
            session.add(user)
            user.joins += 1
        session.commit()

        self.logger.info("- {user} joined".format(
            user=data["username"]))

        if self.config.get("announce_enter", False):
            self.send_message("Welcome, @{username}!".format(
                username=data["username"]))
예제 #41
0
    def post(self, *args, **kwargs):
        resp = {'code': 200, 'msg': '操作成功~', 'data': {}}
        id = self.get_argument('id', 0)
        act = self.get_argument('act', '')
        pay_order_info = session.query(PayOrder).filter_by(id=id).first()
        if not pay_order_info:
            resp['code'] = -1
            resp['msg'] = "系统繁忙。请稍后再试~~"
            self.finish(resp)
            return

        if act == "express":
            pay_order_info.express_status = -6
            pay_order_info.updated_time = getCurrentDate()
            session.add(pay_order_info)
            session.commit()

        self.finish(resp)
예제 #42
0
def verify_token(token):
    if not cache.exists(conf['redis_prefix'] + token):
        google_id = str(get_google_id(token))
        cache.set(conf['redis_prefix'] + token, google_id, ex=3600)

    google_id = str(cache.get(conf['redis_prefix'] + token))
    if google_id == '-1':
        g.current_user = None
        return True

    user = session.query(User).filter(User.google_id == google_id).first()
    if user is None:
        user = User(google_id=google_id)
        session.add(user)
        session.commit()

    g.current_user = user
    return True
예제 #43
0
def create_request(data):
    """
    Creates a new request and return list of requests

    :param data: dictionary
    :return object:
    """
    check_create_priority(data['client_priority'])

    new_request = Request(title=data['title'],
                          description=data['description'],
                          client=data['client'],
                          client_priority=data['client_priority'],
                          target_date=data['target_date'],
                          product_area=data['product_area'])
    session.add(new_request)
    session.commit()
    return get_requests()
예제 #44
0
def import_csv():
    with open('media_list.csv') as csvfile:
        data = csv.reader(csvfile)

        # display data in the console
        for row in data:
            print(row)
            media_title = row[0]
            media_type = row[1]
            artist = row[2]
            genre = row[3]
            published_date = clean_date(row[4])
            price = clean_price(row[5])

            
            new_media = Media(Title=media_title, Type=media_type, Artist=artist, Genre=genre, Date=published_date, Price=price)
            session.add(new_media)
        session.commit()
예제 #45
0
def get_songs_list():
    if request.method == "POST":
        song_json = request.get_json()
        file_id = song_json["file"]["id"]
        file = session.query(File).filter(File.id == file_id).first()
        if file is None:
            flash("error, no file for song")
            return redirect('/')
        song = Song(file_id=file_id)
        session.add(song)
        session.commit()

        jdump = jsonify(song.as_dictionary())
        return jdump, 200
    elif request.method == "PUT":
        pass
    elif request.method == "DELETE":
        song_json = request.get_json()
        song_id = song_json["id"]
        file_id = song_json["file"]["id"]
        if song_id is None:
            flash("cannot delete song, no id supplied")
            return redirect('/')
        song = session.query(Song).filter(Song.id == song_id).first()
        if song is None:
            flash("cannot delete song, no song exists")
            return redirect('/')
        if song.file_id is not file_id:
            flash("song and file don't match")
            return redirect('/')
        file = session.query(File).filter(File.id == song.file_id).first()
        path = file.path
        session.delete(song)
        session.delete(file)
        session.commit()
        if session.query(File).filter(File.path == path).first() is None:
            if os.path.exists(path):
                os.remove(path)
        return redirect('/')
    songs = session.query(Song).all()
    for song in range(len(songs)):
        songs[song] = songs[song].as_dictionary()
    jdump = jsonify(songs)
    return jdump
예제 #46
0
    async def namespace(self, message, stripped):

        if not message.author.guild_permissions.manage_guild:
            await message.channel.send('You must be a Guild Manager to perform this command')

        elif stripped.split(' ')[0].lower() not in map(lambda x: x.lower(), pytz.all_timezones):
            await message.channel.send('Timezone not recognised. Please view a list here: https://gist.github.com/JellyWX/913dfc8b63d45192ad6cb54c829324ee')

        else:
            args = stripped.split(' ', 1)

            tz = args.pop(0)

            if len(args) != 0:
                name = args[0]

            else:
                name = '🕒 %H:%M (%Z)'

            t = datetime.now(pytz.timezone(tz))

            c = None

            for channel in message.guild.text_channels:
                selection = session.query(Clock).filter(Clock.channel_id == channel.id).first()
                if selection is not None:
                    c = channel
                    break

            else:
                c = await message.guild.create_text_channel(
                    'clocks',

                    overwrites= {
                        message.guild.default_role: discord.PermissionOverwrite(read_messages=True, send_messages=False)
                    }
                )

            m = await c.send(t.strftime(name))

            chan = Clock(channel_id=c.id, guild_id=message.guild.id, timezone=tz, channel_name=name, message_id=m.id)
            session.add(chan)

            await message.channel.send('Added clock to space. Please note it may take a few seconds for the clocks to update')
예제 #47
0
def register():
    form = InputOfRegister()
    if form.validate_on_submit():
        name = form.username.data
        pswd = form.password.data
        email = form.email.data
        form.username.data = ""
        form.password.data = ""
        form.email.data = ""
        if User.checkByEmail(email):
            flash('This email has been used!', 'danger')
        else:
            sess.add(User(id=0, name=name, password=pswd, email=email))
            sess.commit()
            g.user = User.checkByEmail(email)
            session['logged_in'] = True
            session['user_id'] = g.user.id
            return redirect('/')
    return render_template('register.html', form=form)
예제 #48
0
def parse_nem12_200record(row):
    """ Parse an NMI details (200) record from a row of a NEM12 file"""
    record_indicator, nmi, _, _, _, _, meter_serial_num, units_of_measure, interval_length, _ = row
    assert (record_indicator == '200')
    assert (units_of_measure == "KWH")
    nem12_record200 = Nem12Record200(nmi=int(nmi),
                                     meter_serial_num=meter_serial_num,
                                     units_of_measure=units_of_measure,
                                     interval_length=int(interval_length))

    # check if it is in the database already.  add it if it isn't
    q = session.query(Nem12Record200).filter_by(nmi=nem12_record200.nmi)
    if q.count():
        nem12_record200 = q.first()
    else:
        session.add(nem12_record200)
        session.commit()

    return nem12_record200
예제 #49
0
def app():
    '''
    Main application function
    '''
    app_running = True
    while app_running:
        choice = menu()
        if choice == '1':
            # add book
            title = input('Title: ')
            author = input('Author: ')
            date_error = True
            while date_error:
                date = input('Published Date (Ex: October 25, 2017): ')
                date = clean_date(date)
                if type(date) == datetime.date:
                    date_error = False
            price_error = True
            while price_error:
                price = input('Price (Ex: 25.64): ')
                price = clean_price(price)
                if type(price) == int:
                    price_error = False
            new_book = Book(title=title,
                            author=author,
                            published_date=date,
                            price=price)
            session.add(new_book)
            session.commit()
            print('Book added!')
            time.sleep(1.5)
        elif choice == '2':
            # view books
            for book in session.query(Book):
                print(f'{book.id} | {book.title} | {book.author}')
            input('\nPress enter to return to the main menu.')
        elif choice == '3':
            pass
        elif choice == '4':
            pass
        else:
            print('GOODBYE')
            app_running = False
예제 #50
0
 def post(self, account_id):
     logger.log(auth.username(), request.date, request.remote_addr,
                request.url)
     authorizer.authorize_user(auth.username(), account_id)
     joke_cnt = None
     for i in xrange(self.POST_GENERATION_RETRY_TIMES):
         new_joke_text = geek_jokes.get_a_joke()
         if not new_joke_text:
             abort(500)
         joke_cnt = session.query(JokeModel)
         joke_cnt = joke_cnt.filter(JokeModel.text == new_joke_text).count()
         if joke_cnt == 0:
             break
     else:
         abort(500)
     new_joke = JokeModel(text=new_joke_text, user_id=account_id)
     session.add(new_joke)
     session.commit()
     return make_response(jsonify(joke=new_joke.serialize), 201)
예제 #51
0
 def post(self):
     content = request.json
     if not content:
         abort(400, 'username and password data is required')
     username = content.get('username')
     password = content.get('password')
     if not password or not username:
         abort(400, 'username and password are mandatory')
     user = session.query(UserModel)
     user = user.filter(UserModel.username == username).first()
     if user is not None:
         abort(409, 'user already exists')
     new_user = UserModel(username=username)
     new_user.hash_password(password)
     session.add(new_user)
     session.commit()
     logger.log(new_user.username, request.date, request.remote_addr,
                request.url)
     return make_response(jsonify(user=new_user.serialize), 201)
예제 #52
0
def instagram_oauth():
    provided_ips = request.headers.getlist("X-Forwarded-For")
    if len(provided_ips) > 0:
        ip = provided_ips[0]
    else:
        ip = "72.69.141.254"
    code = request.args.get("code")
    if not code:
        return 'Missing code'
    try:
        CONFIG = {
            'client_id': INSTAGRAM_KEY,
            'client_secret': INSTAGRAM_SECRET,
            'redirect_uri': INSTAGRAM_REDIRECT
        }
        unauthenticated_api = instagram.client.InstagramAPI(**CONFIG)
        access_token, user_info = unauthenticated_api.exchange_code_for_access_token(
            code)
        if not access_token:
            return 'Could not get access token'
        api = instagram.client.InstagramAPI(access_token=access_token)  #,
        #client_ips=ip,
        #client_secret=INSTAGRAM_SECRET)

        user = api.user()
        print access_token
        if session.query(User).filter_by(username=user.username).first():
            new_user = session.query(User).filter_by(
                username=user.username).first()
            new_user.access_token = access_token
        else:
            new_user = User(
                username=user.username,
                followers=user.__dict__.get("counts").get("followed_by"),
                access_token=access_token,
            )
            session.add(new_user)
        session.commit()
        login_user(new_user)
        return redirect(url_for("home"))
    except Exception, e:
        print e
        return redirect(url_for("instagram_start"))
예제 #53
0
def create_clients():
    data = request.get_json()
    client_schema = ClientSchema()
    parsed_data = {
        'first_name': data['first_name'],
        'surname': data['surname'],
        'email': data['email'],
        'age': data['age'],
        'password':
        bcrypt.generate_password_hash(data['password']).decode('utf-8'),
        'id': data['id']
    }
    try:
        client = client_schema.load(parsed_data)
    except ValidationError as err:
        return abort(400, err.messages)
    session.add(client)
    session.commit()
    return 'Client is registred'
예제 #54
0
def add_article_comment():
    p_id = request.form.get('article_id', None)
    user_id = g.user.id
    content = request.form.get('content', None)
    print p_id
    p_id = int(p_id)
    c = Comment()
    c.article_id = p_id
    c.user_id = user_id
    c.content = content
    resp = json_response()
    try:
        sess.add(c)
        sess.commit()
        resp.data = json.dumps({'code': 0, 'msg': u'发表成功'})
        print 'add succ'
    except Exception, ex:
        print ex
        resp.data = json.dumps({'code': -1, 'msg': u'发表失败'})
예제 #55
0
파일: tasks.py 프로젝트: AngelMsger/Knight
def reply_text(msg, prefix=''):
    # 将log存入数据库
    log = ChatLog(MsgId=msg.msgId,
                  FromUserName=msg.fromUserName,
                  Content=msg.content,
                  CreateTime=datetime.datetime.now())
    session.add(log)
    session.commit()

    # 向图灵机器人API请求返回内容
    request_json = json.dumps({
        'key': settings.TULING123_API_KEY,
        'info': msg.text,
        'userid': msg.fromUserName
    })
    response_json = requests.post('http://www.tuling123.com/openapi/api',
                                  data=request_json)
    response = json.loads(response_json.text)

    # 如果是普通文本回复
    if response.get('code', None) == settings.TUlING123_RESPONSE_CODE['text']:
        msg.user.send('%s%s' % (prefix, response['text']))

    # 如果是带有URL的回复
    elif response.get('code',
                      None) == settings.TUlING123_RESPONSE_CODE['link']:
        msg.user.send('%s%s' % (prefix, response['text']))
        msg.user.send(response['url'])

    # 如果是类新闻类回复
    elif response.get('code',
                      None) == settings.TUlING123_RESPONSE_CODE['news']:
        msg.user.send('%s%s' % (prefix, response['text']))
        for item in msg['list']:
            msg.user.send('%s:%s,报道来自%s' %
                          (item['article'], item['detailurl'], item['source']))
    # 如果是类菜谱类回复
    elif response.get('code',
                      None) == settings.TUlING123_RESPONSE_CODE['menu']:
        msg.user.send('%s%s' % (prefix, response['text']))
        for item in msg['list']:
            msg.user.send('%s:%s,更多详情请查看:%s' %
                          (item['name'], item['info'], item['detailurl']))
예제 #56
0
def add_csv():
    '''
    Add books from csv to database 
    if they aren't in the DB already
    '''
    with open('suggested_books.csv') as csvfile:
        data = csv.reader(csvfile)
        for row in data:
            date = clean_date(row[2])
            price = clean_price(row[3])
            book_in_db = session.query(Book).filter(
                Book.title == row[0]).one_or_none()
            if book_in_db == None:
                new_book = Book(title=row[0],
                                author=row[1],
                                published_date=date,
                                price=price)
                session.add(new_book)
    session.commit()
예제 #57
0
def follow_user(follower_user, followed_user):
    follow_relationship = FollowRelationship(follower_user_id=follower_user.id,
                                             followed_user_id=followed_user.id)

    try:
        session.add(follow_relationship)
        session.flush()
        session.commit()
    except IntegrityError:
        logging.info('Follow relationship already exists in the system!')
        session.rollback()
        session.query(FollowRelationship) \
            .filter_by(follower_user_id=follower_user.id,
                       followed_user_id=followed_user.id)\
            .update({'is_deleted': False})

        session.commit()

    return {'following': True}, HTTP_STATUS_OK
예제 #58
0
파일: api.py 프로젝트: JostLuebbe/mypv
def create_user(username: str, password: str):
    salt = os.urandom(16)

    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        iterations=100000,
        backend=default_backend()
    )

    user = User(
        username=username,
        key=base64.urlsafe_b64encode(kdf.derive(password.encode())),
        salt=base64.b64encode(salt).decode()
    )

    session.add(user)
    session.commit()
예제 #59
0
    def post(self, *args, **kwargs):

        resp = {'code': 200, 'msg': '操作成功~', 'data': {}}

        old_password = self.get_argument('old_password', '')
        new_password = self.get_argument('new_password', '')
        if old_password is None or len(old_password) < 6:
            resp['code'] = -1
            resp['msg'] = "请输入符合规范的原密码~~"
            self.write(resp)
            return

        if new_password is None or len(new_password) < 6:
            resp['code'] = -1
            resp['msg'] = "请输入符合规范的新密码~~"
            self.write(resp)
            return

        if old_password == new_password:
            resp['code'] = -1
            resp['msg'] = "请重新输入一个吧,新密码和原密码不能相同哦~~"
            self.write(resp)
            return

        user_info = self.current_user

        if user_info.uid == 1:
            resp['code'] = -1
            resp['msg'] = "该用户是演示账号,不准修改密码和登录用户名~~"
            self.write(resp)
            return

        user_info.login_pwd = UserService.genePwd(new_password,
                                                  user_info.login_salt)

        session.add(user_info)
        session.commit()
        self.clear_all_cookies()
        sess = Session(self)
        sess['user'] = user_info
        resp = {'code': 200, 'msg': '登录成功~~'}
        self.finish(resp)
예제 #60
0
def register(message):
    args = message.text.split()

    model = session.query(User).filter(User.chat_id == message.chat.id).first()
    if model is not None:
        bot.send_message(message.chat.id, 'already registered')
        return

    model = session.query(UserToken).filter(UserToken.token == args[1]).first()
    if model is None:
        bot.send_message(message.chat.id, 'no such token')
        return

    with db_transaction(session):
        session.delete(model)
        session.add(User(chat_id=message.chat.id))

    bot.send_message(message.chat.id, 'successful')
    logger.info("new user was registered with chat_id = {0}".format(
        message.chat.id))