Exemplo n.º 1
0
def getFollowers() :
    requestBody = loads(request.body.read())
    token = requestBody['token']
    username = requestBody['username']

    # check if user exists
    query = {"username" : username}
    user = database.find(database="brazaar2",
                         collection="users",
                         query=query)
    if user :
        #  get followers with username
        query = {'following' : username}
        followings = list(database.find(database="brazaar2",
                                        collection="followings",
                                        query=query))
        followingArray = []
        for following in followings:
            followingArray.append(following['username'])
        # append to request body
        requestBody['followings'] = followingArray
        return dumps(requestBody)
                
    else :
        response.status = 300
        return {'error': 'User not exists'}
Exemplo n.º 2
0
def singtao():
    # cons = sqlite3.connect(path + '/singtao.db')
    # cons.text_factory = str
    url = 'http://news.singtao.ca/vancouver/' + datetime.date.today().strftime(
        "%Y-%m-%d") + '/'
    res = httpfetch(url, 'utf-8')
    #    f=file('a.html','r')
    #    res =f.read()
    #    f.close()
    res2 = re.compile(r'>headline(.*?)\.html', re.DOTALL).findall(res)

    for topic in res2:
        web_site = '星島日報'
        if database.find(topic, web_site):
            return
        urlbase = url + 'headline' + topic + '.html'

        try:
            item_page = httpfetch(urlbase, 'utf-8', report=True)
        except Exception:
            print "Unexpected error:", sys.exc_info()[1]

        try:
            title = re.compile(r'<title>(.*?)</title>',
                               re.DOTALL).findall(item_page)[0].split('_')[0]
            content = re.compile(r'<div class="content" id="Zoom">(.*?)</div>',
                                 re.DOTALL).findall(item_page)[0]
            content = re.compile(r'<br />', re.DOTALL).sub('\n', content)
            content = re.compile(r'<.*?>', re.DOTALL).sub('', content)
            content = re.compile(r'&.*?;', re.DOTALL).sub(' ', content)
            content = re.compile(r'\n\s+', re.DOTALL).sub('\n', content)
        # content = content.strip()
        except:
            print "Unexpected error:", sys.exc_info()[1]
            print urlbase
        source = '星島日報'

        post_date = datetime.date.today().strftime("%Y-%m-%d")
        tries = 0
        while tries < 2:
            try:
                if not database.find(topic, web_site):
                    database.insert(topic, title, source, content, post_date,
                                    urlbase, web_site)
                else:
                    continue
            except Exception:
                print urlbase
                print sys.exc_info()[0]
                tries += 1
                time.sleep(10)
                continue
            break
    return
Exemplo n.º 3
0
def search_courses():
    requestJson = request.get_json(force=True)

    result = json.dumps(
        database.find(requestJson['query'], requestJson['offset']))

    return Response(result, mimetype='application/json')
Exemplo n.º 4
0
def getNearbyProducts():
    requestBody = loads(request.body.read())
    token = requestBody['token']
    location = requestBody['location']

    # search in cache first
    nearestProducts = None
    # key = str(token + 'getNearbyProducts')
    # nearestProducts = cache.get_cache(key)
    if not nearestProducts :
        # search in database
        query = {'location': {'$nearSphere':{
                                            '$geometry': { 'type' : 'Point' ,
                                                           'coordinates' : [location['longitude'],location['latitude']]}}}}

        nearestProducts = database.find(database="brazaar2", 
                                        collection="products",
                                        limit=10)
        nearestProducts = dumps(nearestProducts)
        # add to cache
        # cache.set_cache(key, nearestProducts)

    if nearestProducts :
        return nearestProducts
    else :
        response.status = 300
        return {'error': 'request collection error'}
Exemplo n.º 5
0
def update_fee_using_relations():
    session = database.get_session()

    for meeting_row in database.find('meeting'):
        meeting_schema = schema.MeetingSchema.from_flat(meeting_row)
        fees = meeting_schema['fees']
        for fee in fees:
            try:
                fee_model = (models.Fee.select()
                    .where(data__contains={'id': str(fee['id'].value)})
                    .where(meeting=meeting_row.id)
                    .get())
            except models.Fee.DoesNotExist:
                fee_data = fee.flatten()
                fee_data = [(re.sub('(fees_\d+_)', '', k), v)
                             for k, v in fee_data]
                fee_model = models.Fee()
                fee_model.meeting = meeting_row.id
                fee_model.data = {}
                fee_model.data.update(fee_data)
                fee_model.save()

        meeting_schema['fees'].set(None)
        meeting_row.clear()
        meeting_row.update(meeting_schema.flatten())
        session.save(meeting_row)
        session.commit()
Exemplo n.º 6
0
def searchKey():
    u_mail = request.args['mail']
    exist = database.is_exist(u_mail)
    if exist:
        status, data = database.find(u_mail)
        return {'status': status, 'data': data}
    else:
        return {'status': exist, 'data': '信息不存在'}
Exemplo n.º 7
0
def update_persons_using_relations():
    session = database.get_session()

    for person_row in database.find('person', meeting_id='1'):
        meeting_id = person_row.pop('meeting_id')
        person_id = person_row.id
        models.PersonMeeting.create(meeting=meeting_id, person=person_id)
        session.save(person_row)
        session.commit()
Exemplo n.º 8
0
def retrieve_items(subreddit, from_, to, keyword=None):
    # Validate given parameters
    if subreddit is None or from_ is None or to is None:
        raise BadRequestException("The following query parameters are "
                                  "mandatory: 'subreddit', 'from', 'to'")
    try:
        from_ = int(from_)
        to = int(to)
    except ValueError:
        raise BadRequestException("Query parameters 'from' and 'to' must be "
                                  "valid UNIX timestamps")

    return db.find(subreddit, from_, to, keyword)
Exemplo n.º 9
0
    def __handle_get(name, reply):
        if not name:
            reply["type"] = "ERROR"
            reply["msg"] = "No name provided"
            return

        answer = database.find(name)
        if answer:
            reply["type"] = "SUCCESS"
        else:
            reply["type"] = "ERROR"
            reply["msg"] = "Name not found in Database"

        reply["data"] = answer
Exemplo n.º 10
0
def activity(meeting_id):
    page = flask.request.args.get('page', 1)
    meeting = sugar.get_object_or_404(models.Meeting, id=meeting_id)
    staff = {s.id: schema.Staff.from_flat(s)
             for s in database.get_all('staff')}

    try:
        staff_id = int(flask.request.args.get('staff_id', 0))
        what_id = int(flask.request.args.get('what_id', 0))
        seconds = int(flask.request.args.get('seconds', 0))
    except ValueError:
        flask.abort(404)


    activities = [schema.Activity.from_flat(a, staff=staff)
                  for a in database.find('activity',
                                         meeting_id=str(meeting_id))]
    user_ids = set([a['staff_id'] for a in activities if a['staff_id']])
    users = [schema.Staff.from_flat(s) for s in database.get_all('staff')
             if s.id in user_ids]

    # filter activities by staff
    if staff_id:
        activities = [a for a in activities if a['staff_id'] == staff_id]
    # filter activities by time span
    if seconds:
        timestamp = lambda d: int(time.mktime(d.timetuple()))
        date_start = int(time.time()) - seconds
        activities = [a for a in activities
                      if timestamp(a['date']) >= date_start]
    # filter activities by person
    if what_id:
        activities = [a for a in activities if a['what_id'] == what_id]
        what = activities[0]['what'] if activities else None
    else:
        what = None

    paginator = sugar.Paginator(objects=activities, per_page=50, page=page)
    activities = paginator.results()

    return {
        'meeting': meeting,
        'users': users,
        'paginator': paginator,
        'activities': activities,
        'what_id': what_id,
        'what': what,
    }
Exemplo n.º 11
0
def registerDeviceToken() :
    requestBody = loads(request.body.read())
    token = requestBody['token']
    
    query = {'token' : token}
    tokenID = database.find(database="brazaar2",
                            collection="devices",
                            query=query)
    if not tokenID :
        tokenID = database.insert(database="brazaar2",
                                    collection="devices",
                                    data=requestBody)

    if tokenID :
        return dumps(requestBody)
    else :
        response.status = 300
        return {'error': 'register token error'}

    return 
Exemplo n.º 12
0
def mail_logs(meeting_id):
    app = flask.current_app

    meeting = sugar.get_object_or_404(models.Meeting, id=meeting_id)
    all_mail_logs = database.find('mail_log', meeting_id=str(meeting.id))
    mail_logs = []

    for log in all_mail_logs:
        mail_type = log.get('type')

        if mail_type == 'bulk_mail':
            mail_log = schema.BulkMailLog.from_flat(log)
            lang = mail_log['mail']['to']
            mail_log['mail']['to'] = 'All %s speakers' % refdata.language.get(lang)

            if not mail_log['mail']['categories']:
                mail_log['mail']['to'] += ', All categories'
            else:
                for c in meeting.get_categories():
                    if c.data['id'] in mail_log['mail']['categories']:
                        mail_log['mail']['to'] += ', ' + c.decoded_data['name_E']

            mail_log['person'] = None

        else:
            mail_log = schema.MailLog.from_flat(log)

            person = sugar.get_person_or_none(meeting_id, mail_log['person_id'])

            if not person:
                continue

            mail_log['person'] = person

        mail_logs.append(mail_log)

    return {
        'meeting': meeting,
        'mail_logs': mail_logs,
    }
Exemplo n.º 13
0
def update_categories_using_relations():
    session = database.get_session()

    for meeting_row in database.find('meeting'):
        meeting_schema = schema.MeetingSchema.from_flat(meeting_row)
        categories = meeting_schema['categories']
        for categ in categories:
            try:
                category = models.Category.select().where(
                    data__contains={'id': str(categ['id'].value)}).get()
            except models.Category.DoesNotExist:
                categ_data = categ.flatten()
                categ_data = [(re.sub('(categories_\d+_)', '', k), v)
                              for k, v in categ_data]

                category = models.Category()
                category.data = {}
                category.data.update(categ_data)
                category.save()

            try:
                category_meeting = (
                    models.CategoryMeeting.select()
                          .where(meeting=meeting_row.id, category=category.id)
                          .get()
                )
            except models.CategoryMeeting.DoesNotExist:
                category_meeting = models.CategoryMeeting.create(
                    meeting=meeting_row.id, category=category.id
                )

        meeting_schema['categories'].set(None)
        meeting_row.clear()
        meeting_row.update(meeting_schema.flatten())
        session.save(meeting_row)
        session.commit()
Exemplo n.º 14
0
def addFollowing():
    requestBody = loads(request.body.read())
    token = requestBody['token']
    relationship = requestBody['relationship']
    username = relationship['username']
    following = relationship['following']
    # check if user exists
    query = {"username" : username}
    user = database.find(database="brazaar2",
                         collection="users",
                         query=query)

    if user :
        followingID = database.insert(database="brazaar2",
                                        collection="followings",
                                        data=relationship)
        if followingID :
            return dumps(relationship)
        else :
            response.status = 300
            return {'error': 'Relationship exists'}
    else :
        response.status = 300
        return {'error': 'User not exists'}
Exemplo n.º 15
0
print('\ttype \'help\' to show a list of commands')

while (active):
    line = input(prompt)
    if not line:
        pass
    elif line.lower() == 'quit':
        active = False
    elif 'delete' in line:
        args = line.split(' ')
        ## delete the <id> <x> from <table>
        database.delete(args[1], args[2], args[3])
    elif 'find' in line:
        args = line.split(' ')
        try:
            database.find(args[1], args[2], args[3])
        except:
            print("Invalid query")
    elif line == 'help':
        help()
    elif 'showall' in line:
        try:
            database.showall(line[8:])
        except:
            print("Invalid query")
    elif 'update' in line:
        args = line.split(' ')
        try:
            ## update <col> to <x> in <table> where <col2> = <y>
            database.update(args[1], args[2], args[3], args[4], args[5])
        except:
Exemplo n.º 16
0
    def on_click(self):
        try:
            textboxValue = self.EspacioTexto.text()
            item = self.comboBox.currentText()

            if (item == "year"):
                b = int(textboxValue)
                a = db.find(item, b).limit(50)
            elif (item == "suicides_no"):
                b = int(textboxValue)
                a = db.find(item, b).limit(50)
            elif (item == "population"):
                b = int(textboxValue)
                a = db.find(item, b).limit(50)
            elif (item == "suicides_per_100k"):
                b = float(textboxValue)
                a = db.find(item, b).limit(50)
            elif (item == "gdp_per_capita"):
                b = int(textboxValue)
                a = db.find(item, b).limit(50)
            elif (item == "age"):
                print("ffgdfgfd")
                b = {'$regex': textboxValue}
                a = db.find(item, b).limit(50)

            else:
                a = db.find(item, {'$regex': textboxValue}).limit(50)

            for i, rates in enumerate(a):
                self.tabla.setItem(i, 0, QTableWidgetItem(rates["country"]))
                self.tabla.setItem(i, 1, QTableWidgetItem(str(rates["year"])))
                self.tabla.setItem(i, 2, QTableWidgetItem(rates["sex"]))
                self.tabla.setItem(i, 3, QTableWidgetItem(rates["age"]))
                self.tabla.setItem(i, 4,
                                   QTableWidgetItem(str(rates["suicides_no"])))
                self.tabla.setItem(i, 5,
                                   QTableWidgetItem(str(rates["population"])))
                self.tabla.setItem(
                    i, 6, QTableWidgetItem(str(rates["suicides_per_100k"])))
                self.tabla.setItem(
                    i, 7, QTableWidgetItem(str(rates["gdp_per_capita"])))
                self.tabla.setItem(i, 8, QTableWidgetItem(rates["generation"]))

            row = 0
            for endian in a:
                self.tabla.setRowCount(row + 1)

                idDato = QTableWidgetItem(endian[0])
                idDato.setTextAlignment(4)

                self.tabla.setItem(row, 0, idDato)
                self.tabla.setItem(row, 1, QTableWidgetItem(endian[1]))
                self.tabla.setItem(row, 2, QTableWidgetItem(endian[2]))
                self.tabla.setItem(row, 3, QTableWidgetItem(endian[3]))
                self.tabla.setItem(row, 4, QTableWidgetItem(endian[4]))
                self.tabla.setItem(row, 5, QTableWidgetItem(endian[5]))

                row += 1

        except Exception as e:
            pass
Exemplo n.º 17
0
def fetch(i, debug=False):
    path = os.path.dirname(os.path.realpath(sys.argv[0]))
    conn = sqlite3.connect(path + '/news.sqlite3.db')
    conn.text_factory = str
    # print 'fetching topic', i, '...'
    urlbase = 'http://www.wenxuecity.com'
    url = urlbase + i
    news_id = i.split('/')[5]
    news_id = news_id.split('.')[0]
    w = "文学城"
    # if database.find(news_id, w, conn):
    #     return
    res = ''
    for _ in range(3):
        try:
            res = httpfetch(url, 'utf-8', report=True)
            break
        except:
            print sys.exc_info()[1]
            continue
    res = re.compile(r'<div class="maincontainer">(.*?)<div class="banners">',
                     re.DOTALL).findall(res)[0]
    title = re.compile(r'<h3>(.*?)</h3>', re.DOTALL).findall(res)
    if title:
        title = title[0].encode('utf-8')
        link = url
        web_site = '文学城'
        try:
            parse = re.compile(r'<div id="postmeta">(.*?) <span>',
                               re.DOTALL).search(res).group(1)
            source = re.compile(r'itemprop="author">(.*?)</span>',
                                re.DOTALL).findall(parse)[0]
            post_date = re.compile(r'datetime(.*?)</time>',
                                   re.DOTALL).findall(parse)[0]
            post_date = post_date.split('>')[1]
            content = re.compile(
                r'<div id="articleContent" class="article">(.*?)<div class="sharewechat">',
                re.DOTALL).findall(res)[0]
            if content:
                # content = content[0]
                content = re.compile(r'<div style=(.*?)>',
                                     re.DOTALL).sub('', content)
                content = re.compile(r'<br>', re.DOTALL).sub('', content)
                content = re.compile(r'<.*?>', re.DOTALL).sub('', content)
                content = re.compile(r'&.*?;', re.DOTALL).sub(' ', content)
                content = re.compile(r'\n\s+', re.DOTALL).sub('\n', content)
                content = content.strip()
            else:
                content = ''
                print news_id

            if debug:
                print title
                print source
                print content
                print post_date
                print web_site

            if not database.find(news_id, web_site, conn):
                database.insert(news_id, title, source, content, post_date,
                                link, web_site, conn)
            else:
                database.update(news_id, title, source, content, post_date,
                                link, web_site, conn)
        except:
            print ""
    return post_date
main_content = soup.find('div', id='mainColumn')\
    .find('div', id="fpMainContent")\
    .find('div', attrs={"class": "gridCategory removeHidden", "data-blockname": "best"})



products = main_content.find_all('div', 'fpGridBox grid  frontpage firedeal')

for product in products:
    print(product.find('img')['title'])



print(len(products))

existed_items = []

while 1:
    # if the program is runned the first time, add all deals to database without any notification
    if database.check_empty():
        for product in products:
            database.insert(product.find('img')['title'])
    else:
        for product in products:
            firedeal = product.find('img')['title']
            if not database.find(firedeal):
                notification.balloon_tip("Slickdeal Firedeal", firedeal)
                database.insert(firedeal)

    time.sleep(300)
Exemplo n.º 19
0
def getEvents():
    #return all database records to UI
    return database.find({}), 200
Exemplo n.º 20
0
	def get(self, data_id):
		abort_if_doesnt_exist(data_id)
		return json_dump(database.find(data_id))
Exemplo n.º 21
0
 def ping():
     list(database.find('meeting'))
     return "cites is ok"
Exemplo n.º 22
0
def home():
    return database.find({})
Exemplo n.º 23
0
def participants_csv(meeting_id):
    f = StringIO()
    writer = sugar.UnicodeWriter(f)

    # get valid schema with usersdatabase
    person_schema = schema.PersonSchema()
    headers = [(p.label, 'personal_%s' % p.name)
               for p in person_schema['personal'].values()]
    headers_representing = [(p.label, 'representing_%s' % p.name) for p in
                            person_schema['representing'].values()]
    headers.extend(headers_representing)
    meeting_headers = [(p.label, 'meeting_flags_%s' % p.name) for p in
                       person_schema['meeting_flags'].values()]

    photo_header = [(u'Picture URL', person_schema['photo'].name)]
    # write headers to CSV file
    headers_list = [head[0] for head in headers]
    headers_list.extend([head[0] for head in meeting_headers])
    headers_list.extend([head[0] for head in photo_header])

    writer.writerow(headers_list)

    # write content to CSV file
    db_generator = database.find('person', meeting_id=str(meeting_id))
    users = [user for user in db_generator]

    languages = _load_json('../refdata/languages.json')
    categories = _load_json('../refdata/categories.json')
    category = {c['id']: c['name'] for c in categories}
    fees = _load_json('../refdata/fee.json')
    fee = {f['id']: f['name'] for f in fees}
    regions = _load_json('../refdata/regions.json')
    region = {r['id']: r['name'] for r in regions}
    tf_dict = {'1': 'Y', '0': 'N'}

    for user in users:
        row_list = []

        for field in headers:
            db_field = field[1]
            value_field = user.get(field[1])
            if value_field is None:
                    value_field = u''

            if db_field == 'personal_language' and value_field != '':
                value_field = languages[value_field]
            elif db_field == 'personal_country' and value_field != '':
                value_field = country[value_field]
            elif db_field == 'representing_country' and value_field != '':
                value_field = country[value_field]
            elif db_field == 'personal_category' and value_field != '':
                value_field = category[value_field][user['personal_language']]
            elif db_field == 'personal_fee' and value_field != '':
                value_field = fee[value_field][user['personal_language']]
            elif db_field == 'representing_region' and \
                    value_field != '' and value_field != '0':
                    value_field = region[value_field][user['personal_language']]
            elif db_field == 'representing_organization_show' and value_field != '':
                value_field = tf_dict[value_field]

            row_list.append(value_field)

        for field in meeting_headers:
            db_field = field[1]
            value_field = user[field[1]]
            if db_field == 'meeting_flags_acknowledged':
                row_list.append(value_field)
                continue
            try:
                value_field = tf_dict[value_field]
            except KeyError:
                value_field = ''
                pass

            row_list.append(value_field)

        for field in photo_header:
            db_field = field[1]
            photo_url = u''

            try:
                value_field = user[field[1]]
            except KeyError:
                pass

            if value_field:
                photo_url = flask.url_for('static',
                    filename='photos/%s/%s' % (meeting_id, value_field),
                    _external=True)

            row_list.append(photo_url)

        writer.writerow(row_list)

    data = f.getvalue()
    f.close()
    return flask.Response(data, mimetype='text/csv')
Exemplo n.º 24
0
#! This program can keep your passwords encrypted
""" With this program, you can keep all your passwords in safety.

    Commands: help, add, remove, find, change, showall, clear """

import database as data

while True:
    task = str(input("Enter a command here: "))
    if task == 'help':
        print(__doc__)
    elif task == 'add':  # Добавления данных в словарь
        a = input(str("Enter resource: "))
        b = input(str("Enter password: "******"Enter a key: "))
        data.remove(a)
    elif task == 'find':  # Поиск данных в словаре
        a = input(str("Enter a key:"))
        data.find(a)
    elif task == 'change':  # Изменение данных в словаре
        a = input(str("Enter a key you want to rename: "))
        data.change(a)
    elif task == 'showall':  # Показать все данные в словаре
        data.showall()
    elif task == 'clear':
        data.clear()
    else:
        print("No such command %30s" % task)
        elif 'what is' in statement:
            answer = statement
            speak('searching about' + answer)
            pywhatkit.info(answer)  
            speak("Here is the result!")
            time.sleep(15)  
            
        elif 'take a screenshot' in statement or 'screenshot' in statement:
            speak("Taking a screenshot")
            screenshot()
            speak("Successfully took a screenshot, You can find it in the screenshots folder")

        elif 'database' in statement:
            speak('What you want to search in database?')
            find(takeCommand())

        elif 'who is' in statement:
             person = statement.replace('who is', '')
             info = wikipedia.summary(person, 3)
             speak("According to Wikipedia Summary")
             print(info)
             speak(info)

        elif 'tell me a story' in statement:
            speak("Preeti will now tell you a story")
            exec(open("file\\storydbase.py").read())

        elif 'tell me a hindi story' in statement or 'tell me a story in hindi' in statement:
            speak("Okay, Playing a Hindi story")
            exec(open("file\\hindistories.py").read())
Exemplo n.º 26
0
        #	print "AAya"
        exit = int(raw_input("Enter Destination :"))
        user = int(time.time())
        database.insert(user, entry, exit)

        if abs(entry - cur_floor_lift1) < abs(entry - cur_floor_lift2):
            cur_floor_lift1 = move_lift1(cur_floor_lift1, entry)
            cur_floor_lift1 = move_lift1(entry, exit)

        else:
            cur_floor_lift2 = move_lift2(cur_floor_lift2, entry)
            cur_floor_lift2 = move_lift2(entry, exit)

        face_recognition.post("Img.png", user)

    elif database.find(user, entry) == -1:
        #	print "Pss"
        exit = int(raw_input("Enter Destination :"))

        database.insert(user, entry, exit)

        if abs(entry - cur_floor_lift1) < abs(entry - cur_floor_lift2):
            cur_floor_lift1 = move_lift1(cur_floor_lift1, entry)
            cur_floor_lift1 = move_lift1(entry, exit)

        else:
            cur_floor_lift2 = move_lift2(cur_floor_lift2, entry)
            cur_floor_lift2 = move_lift2(entry, exit)
    else:

        exit = database.find(user, entry)
Exemplo n.º 27
0
while True:
    command = commandUser()
    c = str(command)

    if (c == 'a'):
        key = input("Enter a name: ")
        key = key.lower()
        value = input("Enter the phone number(only enter digits): ")
        value = int(value)
        database.add("data.txt", key, value)

    elif (c == 'f'):
        key = input("Enter the name you would like to search for: ")
        key = key.lower()
        database.find("data.txt", key)

    elif (c == 'd'):
        key = input("Enter the name you would like to delete: ")
        key = key.lower()
        database.delete("data.txt", key)

    elif (c == 'u'):
        key = input("Enter the name you would like to search for: ")
        key = key.lower()
        database.update("data.txt", key)

    elif (c == 'q'):
        break

    else:
Exemplo n.º 28
0
def abort_if_doesnt_exist(data_id):
	if database.find(data_id) is None:
		abort(404, message = "Data {} doesn't exist".format(data_id))