Beispiel #1
0
def get_extra_server_info():
    server_name = [getattr(settings, x) for x in ['HOSTNAME', 'CLUSTER_NAME']]
    server_name = '.'.join(x for x in server_name if x)
    server_info = {
        'name': server_name,
        'git_sha': GIT_SHA,
    }
    try:
        with open(DB_INFO_FILE, 'r') as fp:
            db_info = json.load(fp)
    except (IOError, ValueError):
        pass
    else:
        db_info['last_update'] = timeago.format(
            datetime.fromtimestamp(db_info['updated']))
        db_info['file_url'] = get_db_file_url(db_info['file_name'])
        for key, value in db_info.items():
            server_info['db_%s' % key] = value

    # Maxmind DB File Info
    try:
        geo_last_mod = os.path.getmtime(settings.MAXMIND_DB_PATH)
    except FileNotFoundError:
        pass
    else:
        server_info['geo_last_update'] = timeago.format(
            datetime.fromtimestamp(geo_last_mod))

    return server_info
Beispiel #2
0
    def _create_entry(self, position, story_id, story):

        data = {"story_id": story_id}

        w1 = urwid.Text(("entry main", "%2s. " % position))

        markup = [("entry main", story["title"])]
        # BBB: walrus operator
        url = story.get("url")
        if url:
            data["url"] = url
            markup.append(("entry url", " (%s)" % url2domain(url)))
        # NB: cursor_position is set to a big number to make it disappear
        w21 = urwid.SelectableIcon(markup, cursor_position=float("inf"))

        if story["type"] == "job":
            text = "%s" % timeago.format(story["time"])
        else:
            text = "%s %s by %s %s" % (
                story["score"],
                pluralize("point", story["score"]),
                story["by"],
                timeago.format(story["time"]),
            )
        # BBB: walrus operator
        comments = story.get("descendants")
        if comments is not None:
            text += " | %s %s" % (comments, pluralize("comment", comments))
        w22 = urwid.Text(text)

        w2 = urwid.Pile([w21, w22])

        w = urwid.Columns([("pack", w1), w2])

        return Entry(w, data)
Beispiel #3
0
def get_recent_activity():
    """ Get current time"""
    now = datetime.now() + timedelta(seconds=60 * 3.4)
    date = datetime.now()
    time_ago = timeago.format(date, now)
    """ Query mongodb for 5 most recent updates, targetting date field and sorting """
    last_five = list(
        db.streets.find({}, {
            'name_en': 1,
            'translations': 1,
            '_id': 0
        }).limit(5).sort('translations.date_posted', -1))
    """ Some streets have multiple translations in a list field that must be unpacked before further sorting """
    recent = []
    for obj in last_five:
        for obj1 in obj['translations']:
            recent.append(obj1)
    """ A final sorting and limiting list to five """
    recent = sorted(recent, key=itemgetter('date_posted'), reverse=True)
    recent = recent[:5]
    """ Add time stamp (time ago) to each translation """
    for translation in recent:
        date_posted = timeago.format(translation['date_posted'], now)
        translation['date_posted'] = date_posted
    return recent
Beispiel #4
0
def show_all_posts():

    if not session['user_logged_in']:
        flash(login_msg, "error")
        return redirect('/login/')

    query = "SELECT posts.post_id, posts.post_content, posts.created_at AS 'posted_date', posts.created_at AS " \
            "'posted_date_readable', posts.user_id AS 'post_user_id', users.user_id AS 'user_user_id', " \
            "users.username, users.user_image FROM posts JOIN users on users.user_id = posts.user_id ORDER BY" \
            " posts.created_at DESC"
    posts = mysql.query_db(query)
    for post in posts:
        now = datetime.datetime.now()
        post['posted_date'] = timeago.format(post['posted_date'], now)
        post['posted_date_readable'] = datetime.datetime.strftime(post['posted_date_readable'], "%A, %B %d, "
                                                                                                "%Y %H:%M:%S")

        query = "SELECT users.username, users.user_image, users.user_id, comments.created_at, " \
                "comments.comment_content FROM comments JOIN users on users.user_id = comments.user_id " \
                "WHERE post_id = :post_id"
        data = {"post_id": post['post_id']}
        post['post_comments'] = mysql.query_db(query, data)

        for comment in post['post_comments']:
            comment['created_at'] = parser.parse(comment['created_at'])
            comment['created_at_readable'] = datetime.datetime.strftime(comment['created_at'], "%A, %B %d, %Y %H:%M:%S")
            comment['created_at'] = timeago.format(comment['created_at'], now)

    return render_template('posts.html', posts=posts)
Beispiel #5
0
    def footer_attrs(self):

        if len(self.provider.selected_channels) == 1:
            feed = self.provider.selected_channels[0]
            return AttrDict([
                ("refreshed", lambda: timeago.format(
                    feed.fetched, datetime.now(), "en_short")),
                ("updated", lambda: timeago.format(feed.updated, datetime.now(
                ), "en_short")),
                ("selected", lambda: self.body.focus_position + 1
                 if len(self) else 0),
                ("shown", lambda: len(self)),
                ("matching", lambda: self.body.query_result_count()),
                ("fetched", lambda: self.provider.feed_item_count),
                # ("fetched total", lambda: self.provider.total_item_count)
            ])
        else:
            # FIXME: aggregate stats
            return AttrDict([
                ("refreshed", lambda: "?"),
                ("updated", lambda: "?"),
                ("selected", lambda: self.body.focus_position + 1),
                ("shown", lambda: 0),
                ("matching", lambda: 0),
                ("fetched", lambda: 0),
                # ("fetched total", lambda: self.provider.total_item_count)
            ])
Beispiel #6
0
    def update(self):
        if self.model.get_status() == Status.CONFLICT:
            self.controller.ack_conflict()
            self.open_merge_popup()

        self.status_label.config(text=self.model.get_status())

        sync_event = self.model.get_last_sync_event()
        sync_status_text = ""

        if sync_event == None:
            last_checked_time = self.model.get_last_checked()

            if last_checked_time == None:
                sync_status_text = "Addons have not been synced yet"
            else:
                sync_time_text = timeago.format(last_checked_time,
                                                datetime.now())
                sync_status_text = "Last checked {sync_time}".format(
                    sync_time=sync_time_text)
        else:
            sync_time = sync_event.get_sync_time()
            print(sync_time)

            sync_time_text = timeago.format(sync_time, datetime.now())
            print(sync_time_text)

            sync_type = sync_event.get_sync_type()
            sync_type_text = sync_type.value

            sync_status_text = "Last {sync_type} {sync_time}".format(
                sync_type=sync_type_text, sync_time=sync_time_text)

        self.last_checked_label.config(text=sync_status_text)
Beispiel #7
0
    def getMessagesFromRoom(self, roomId, sender, receiver):
        """Get messages for user

        Args:
            roomId (int): 
            sender (int): 
            receiver (int): 
        """
        print("Chat dump")
        print("---------------------------------------------------------------")

        messages = self.currentUser.getMessages()

        for msg in messages:
            for stamp, data in msg.items():
                if data['receiver'] == receiver:
                    leftPrint(data['message'])
                    leftPrint("-"*30)
                    leftPrint(timeago.format(
                        data["timestamp"], time.strftime(
                            "%Y-%m-%d %H:%M:%S")
                    ))
                    leftPrint("")
                else:
                    rightPrint(data['message'])
                    rightPrint("-"*30)
                    rightPrint(timeago.format(
                        data["timestamp"], time.strftime(
                            "%Y-%m-%d %H:%M:%S")
                    ))
                    rightPrint("")
Beispiel #8
0
def get_questions(request):
    if request.is_ajax:
        object_length = Questions.objects.all().count()
        query = Questions.objects.all().order_by("-created")[0:50]
        questions_list = []
        for item in query:
            question = {}
            question["id"] = item.id
            question["title"] = item.title
            question["slug"] = item.slug
            question["answersCount"] = item.queanswers.count()
            question["viewsCount"] = item.views
            creation_date = item.created
            last_modified = item.modified
            lagos = pytz.timezone('Africa/Lagos')
            if last_modified < creation_date:
                time_ago = timeago.format(last_modified, datetime.now(lagos))
                question["time"] = time_ago
            else:
                time_ago = timeago.format(creation_date, datetime.now(lagos))
                question["time"] = time_ago
            # view_no
            questions_list.append(question)

        payload = {"questionLog": questions_list, "total": object_length}
        payload = json.dumps(payload)
        return JsonResponse(json.loads(payload), safe=False)
Beispiel #9
0
    def _main_menu(self):
        # start menu build
        menu = gtk.Menu()

        now = datetime.datetime.utcnow()
        now = now.replace(tzinfo=pytz.utc)

        for repo in self.travis_repos:
            builds = [
                (self.builds[repo.slug][k], k) for k in self.builds[repo.slug]
                if self.builds[repo.slug][k].state in
                {'queued', 'started', 'ready', 'created'} or
                self._build_cmp_date(self.builds[repo.slug][k].finished_at, 7)
            ]
            if len(builds) > 0:
                builds = sorted(builds, key=lambda b: b[1], reverse=True)
                menu.append(
                    self._label_menu_item(
                        "{}".format(repo.slug),
                        "https://travis-ci.org/{}".format(repo.slug)))

                for build, k in builds[:10]:

                    # create time string
                    if build.state == 'started' and not build.started_at is None:
                        time_str = ' (started ' + timeago.format(
                            build.started_at, now) + ')'
                    elif build.state in {'passed', 'failed', 'errored'
                                         } and not build.finished_at is None:
                        time_str = ' (' + timeago.format(
                            build.finished_at, now) + ')'
                    else:  # build.state in {'pending', 'queued', 'ready'}
                        time_str = ''

                    #submenu = gtk.Menu()
                    #for i in build.job_ids:
                    #  job = self.travis.job(i)
                    #  label = "#{} {}".format(job.number, job.config['os'])
                    #  if "compiler" in job.config:
                    #    label = label + "/" + job.config['compiler']
                    #  submenu.append(self._image_menu_item(label, self.state_icon[job.state], "https://travis-ci.org/{}/jobs/{}".format(repo.slug, job.id)))

                    label = self._build_label_str(build)

                    menu.append(
                        self._image_menu_item(
                            label + time_str,
                            self.get_gtk_icon(build.state),
                            url="https://travis-ci.org/{}/builds/{}".format(
                                repo.slug, build.id)))

                menu.append(self._seperator_menu_item())

        q = gtk.MenuItem("Quit")
        q.connect("activate", self.stop)
        menu.append(q)

        menu.show_all()
        return menu
Beispiel #10
0
def ValueInUSD(amount, ticker) -> str:
    global conn
    try:
        openConnection()
        conn.ping(reconnect=True)  # reconnecting mysql
        with conn.cursor() as cursor:
            # Read a single record
            sql = "SELECT * FROM `cmc_v2` WHERE `symbol`=%s ORDER BY `id` DESC LIMIT 1"
            cursor.execute(sql, (ticker.upper(), ))
            result = cursor.fetchone()

            #coingecko
            sql = "SELECT * FROM `coingecko_v2` WHERE `symbol`=%s ORDER BY `id` DESC LIMIT 1"
            cursor.execute(sql, (ticker.lower(), ))
            result2 = cursor.fetchone()

            if all(v is None for v in [result, result2]):
                return '`We can not find ticker {} in Coinmarketcap`'.format(
                    ticker.upper())
            else:
                if result:
                    name = result['name']
                    ticker = result['symbol'].upper()
                    price = result['priceUSD']
                    totalValue = amount * price
                    if float(totalValue) > 0.01:
                        totalValue = '{:,.4f}'.format(float(totalValue))
                    else:
                        totalValue = '{:,.8f}'.format(float(totalValue))
                    update = datetime.datetime.strptime(
                        result['last_updated'].split(".")[0],
                        '%Y-%m-%dT%H:%M:%S')
                    ago = timeago.format(update, datetime.datetime.utcnow())

                if result2:
                    name2 = result2['name']
                    ticker2 = result2['symbol'].upper()
                    price2 = result2['marketprice_USD']
                    totalValue2 = amount * price2
                    if (float(totalValue2) > 0.01):
                        totalValue2 = '{:,.4f}'.format(float(totalValue2))
                    else:
                        totalValue2 = '{:,.8f}'.format(float(totalValue2))
                    update2 = datetime.datetime.strptime(
                        result2['last_updated'].split(".")[0],
                        '%Y-%m-%dT%H:%M:%S')
                    ago2 = timeago.format(update2, datetime.datetime.utcnow())

                MsgPrice = ''
                if result:
                    MsgPrice = MsgPrice + '`{}{} = {}USD. Updated {} from Coinmarketcap`\n'.format(
                        amount, ticker, totalValue, ago)
                if result2:
                    MsgPrice = MsgPrice + '`{}{} = {}USD. Updated {} from Coingecko`'.format(
                        amount, ticker2, totalValue2, ago2)
                return MsgPrice
    except Exception as e:
        traceback.print_exc(file=sys.stdout)
Beispiel #11
0
def personalizedEmbed(member):
    def statusString(status):
        return "Online" if status == discord.Status.online else \
         "Offline" if status == discord.Status.offline else \
         "Idle" if status == discord.Status.idle else \
         "Do not disturb" if status == discord.Status.dnd else \
         status

    u = member
    data = {
        "Generic": {
            "Name": str(u),
            "Display Name": u.display_name,
            "Mention": u.mention,
            "ID": u.id,
            "Animated Avatar": str(u.is_avatar_animated()),
            "Robot": str(u.bot)
        },
        "Age": {
            "Joined at": str(u.joined_at),
            "Joined server": timeago.format(u.joined_at),
            "Created at": str(u.created_at),
            "Created": timeago.format(u.created_at)
        },
        "Temporary data": {
            "Last Nitro Boost":
            str(u.premium_since) if u.premium_since else "",
            "Activity":
            f"{u.activity}" if u.activity else "",
            "Voice State":
            str(u.voice),
            "Top Role":
            str(u.top_role),
            "Roles":
            ", ".join([
                f"{r} ({timeago.format(r.created_at)})" for r in u.roles
            ]).replace("@everyone ", "everyone ")
        },
        "Status": {
            "Status": statusString(u.status),
            "Desktop Status": statusString(u.desktop_status),
            "Mobile Status": statusString(u.mobile_status),
            "Web Status": statusString(u.web_status),
            "Currently Mobile": str(u.is_on_mobile())
        }
    }
    embed = discord.Embed(
        title=f"{u.name} ({u.nick})" if u.nick is not None else u.name,
        type="rich",
        color=discord.Color.from_rgb(0, 139, 139))
    embed.set_image(url=u.avatar_url)
    for catName, cat in data.items():
        embed.add_field(name=catName,
                        value="\n".join([f"{k}: {v}" for k, v in cat.items()]),
                        inline=False)

    return embed
Beispiel #12
0
def htmlTable(members):
    members = [{
        "name": user.name,
        "nick": user.nick if user.nick else "",
        "mention": user.mention,
        "joined": timeago.format(user.joined_at),
        "age": timeago.format(user.created_at)
    } for user in members]

    return render_template("index.html", users=members)
Beispiel #13
0
def format_timeago(val):
    if val:
        try:
            return timeago.format(datetime.strptime(val, '%Y-%m-%dT%H:%M:%SZ'),
                                  datetime.utcnow())
        except:
            return timeago.format(
                datetime.strptime(val, '%Y-%m-%dT%H:%M:%S.%fZ'),
                datetime.utcnow())

    return val
Beispiel #14
0
    def can_delete(self):
        time_delta = timedelta(minutes=30)
        timer = self.created_at.replace(tzinfo=None)
        now = datetime.now()
        timeago.format(timer, now)

        if now - timer < time_delta:
            return True
        else:
            return False

        def __repr__(self):
            return f"<Message object: id: {self.id}, title: {self.user}>"
Beispiel #15
0
    def data(self, idx: QModelIndex, role=None):

        if role == Qt.TextAlignmentRole and idx.column() in (3, 4, 5):
            return Qt.AlignCenter

        if not idx.isValid():
            return None

        perm_obj = self._data[idx.row()]

        if role == Qt.EditRole and idx.column() in (1, 5):
            return perm_obj.address

        if role != Qt.DisplayRole:
            return None

        if idx.column() == 0:
            if self._alias_list and perm_obj.address in self._alias_list.keys(
            ):
                return self._alias_list[perm_obj.address]
            return ''
        if idx.column() == 1:
            return perm_obj.address
        if idx.column() == 2:
            if self._perm_type == enums.MINE:
                if perm_obj.address in self.last_mined:
                    return timeago.format(self.last_mined[perm_obj.address],
                                          datetime.now())
            if self._perm_type == enums.ADMIN:
                if perm_obj.address in self.last_voted:
                    return timeago.format(self.last_voted[perm_obj.address],
                                          datetime.now())
            return 'Never'
        if idx.column() == 3:
            if self._perm_type == enums.MINE:
                return "{} Blocks".format(
                    self.last_24_h_mine_count[perm_obj.address] if perm_obj.
                    address in self.last_24_h_mine_count else 0)
            else:
                return "{} Votes".format(
                    self.last_24_h_vote_count[perm_obj.address] if perm_obj.
                    address in self.last_24_h_vote_count else 0)
        if idx.column() == 4:
            return "{} of {}".format(
                self.count_revokes[perm_obj.address]
                if perm_obj.address in self.count_revokes else 0,
                math.ceil(self.num_guardians *
                          ADMIN_CONSENUS_ADMIN if self._perm_type ==
                          enums.ADMIN else ADMIN_CONSENUS_MINE))
    def data(self, idx: QModelIndex, role=None):

        if role == Qt.TextAlignmentRole and idx.column() in (3, 4, 5):
            return Qt.AlignCenter

        if not idx.isValid():
            return None

        perm_obj = self._data[idx.row()]

        if role == Qt.EditRole and idx.column() in (1, 5):
            return perm_obj.address_id

        if role != Qt.DisplayRole:
            return None

        if idx.column() == 0:
            return perm_obj.address.alias
        if idx.column() == 1:
            return perm_obj.address_id
        if idx.column() == 2:
            if self._perm_type == Permission.MINE:
                last_mined = perm_obj.address.get_last_mined()
                if last_mined:
                    return timeago.format(last_mined, datetime.now())
            if self._perm_type == Permission.ADMIN:
                last_voted = perm_obj.address.get_last_voted()
                if last_voted:
                    return timeago.format(last_voted, datetime.now())
            return 'Never'
        if idx.column() == 3:
            if self._perm_type == Permission.MINE:
                return "{} Blocks".format(
                    perm_obj.address.get_mined_last_24_hours())
            else:
                return "{} Votes".format(
                    perm_obj.address.get_voted_last_24_hours())
        if idx.column() == 4:
            if self._perm_type == Permission.MINE:
                return "{} of {}".format(
                    perm_obj.address.num_validator_revokes(),
                    math.ceil(Permission.num_guardians() *
                              ADMIN_CONSENUS_MINE))
            else:
                return "{} of {}".format(
                    perm_obj.address.num_guardian_revokes(),
                    math.ceil(Permission.num_guardians() *
                              ADMIN_CONSENUS_ADMIN))
Beispiel #17
0
def getCovid(state="Kerala"):
    curr_time = datetime.datetime.now(pytz.timezone('Asia/Calcutta'))
    curr_time_split = str(curr_time).split('.', 1)[0]
    json_url = requests.get(url)
    data = json.loads(json_url.text)
    states = data["statewise"]
    if state == "Total":
        states = data["total"]
    else:
        for j in states:
            if j["state"] == state:
                k = j
                break
        else:
            failed = "Wrong input. Eg - <code>/covid Kerala</code> \n\nVisit https://www.covid19india.org/ for more info."
            return failed
    reft = str(k['lastRefreshed'])
    date = reft.split(" ", 1)[0]
    day = date.split("/", 2)[0]
    month = date.split("/", 2)[1]
    year = date.split("/", 2)[2]
    lref = year + "-" + month + "-" + day + " " + reft.split(" ", 1)[1]
    refresht = timeago.format(lref, curr_time_split)
    status = str("<b>State: <u>{} - {}</u></b> \n<code>😷  Confirmed  |  {} \n🤗  Recovered  |  {} \n💀  Deaths     |  {} \n🤧  Total      |  {}</code> \n\n<i>Refreshed: {}</i>".format(k['state'], k['statecode'], k['active'], k['recovered'], k['deaths'], k['confirmed'], refresht))
    return status
Beispiel #18
0
    def get(self, request, *args, **kwargs):
        # Get the URL of the API using requests module
        url = requests.get('https://api.opendota.com/api/proMatches')
        # Decode the data
        json_data = url.json()

        # List that will contain dictionaries
        game_data = []

        for data in json_data:
            # Will contain the values from the dictionary data of json_data
            j_data = {}
            j_data['League'] = data['league_name']
            j_data['Radiant'] = data['radiant_name']
            j_data['Dire'] = data['dire_name']
            j_data['Duration'] = str(timedelta(seconds=data['duration']))
            j_data['Finished'] = timeago.format(
                datetime.now() - datetime.fromtimestamp(data['start_time']))

            # Check who the winner of the match
            if data['radiant_win'] == False:
                j_data['Winner'] = data['dire_name']
            else:
                j_data['Winner'] = data['radiant_name']

            # Put the data in our game_data list
            game_data.append(j_data)

        return render(request, self.template_name, {'game_data': game_data})
Beispiel #19
0
async def get_inactive(client, message):
    cmd = message.command
    start = time.time()
    limit = int(' '.join(cmd[1:])) if len(cmd) > 1 else 0
    messages = [
        m async for member in client.iter_chat_members(
            message.chat.id,
            limit=limit,
            filter='recent',
        ) if not member.user.is_deleted async for m in client.search_messages(
            message.chat.id,
            limit=1,
            from_user=member.user.id,
        )
    ]

    delta = time.time() - start
    messages.sort(key=lambda k: k['date'])

    return '\n'.join([
        '{} last [message]({}) was {}'.format(
            m.from_user.mention,
            m.link,
            timeago.format(m.date),
        ) for m in messages
    ] + [f'`{int(delta * 1000)}ms`'], )
Beispiel #20
0
    def get(self):
        """[Retrieves all Boards by user]
        
        Raises:
            InternalServerError: [If Error in retrieval]
        
        Returns:
            [json] -- [Json object with message and status code]
        """
        try:
            user_id = get_jwt_identity()
            user = User.objects(id=ObjectId(user_id)).only('username')
            now = datetime.datetime.now()
            boards = Board.objects(added_by=ObjectId(user_id))
            boards_list = []
            for board in boards:
                board_dict = board.to_mongo().to_dict()
                data = timeago.format(board.created_at, now)
                board_dict['time_stamp'] = data
                board_dict['username'] = user[0].username
                boards_list.append(board_dict)
            res = {'data': boards_list, 'message': "Successfully retrieved", "count": len(boards_list)}
            boards_josn = dumps(res)
            return Response(boards_josn, mimetype="application/json", status=200)


        except Exception as e:
            print(e)
            raise InternalServerError
Beispiel #21
0
    def get(self, request, pk):
        pipeline = get_object_or_404(PipeLine, pk=pk)
        if pipeline.user.pk == request.user.pk:
            if pipeline.is_github_pipeline:
                return redirect(
                    reverse('dashboard:pipeline_builds',
                            kwargs={'pk': pipeline.pk}))
            running_pipelines = PipeLineResult \
                                    .objects \
                                    .filter(Q(pipeline=pk)
                                            & Q(status=PipeLineStatus.IN_PROGRESS.value)
                                            | Q(status=PipeLineStatus.IN_QUEUE.value)).order_by('pk') \
                                    .reverse()[:5]

            for build in running_pipelines:
                build.elapsed_time = timeago.format(build.created_at, now())

            context = {
                'pipeline': pipeline,
                'running_pipelines': running_pipelines
            }
            return render(request, 'dashboard/pipeline/pipeline_view.html',
                          context)
        else:
            return redirect(reverse('login'))
Beispiel #22
0
def fetch_post_from_api(post_id: int, rank: int) -> AsyncResult:
    """Makes a request to Hackernews API to get post information. Fires tasks to persist it
    in db, and fetch sentiment for it. This don't traverse all kids to find num of comment. It's
    so heavy operation. However, if we really need to; we can do it by firing async tasks to
    traverse all kids and count number of kids using cache with lock.
    """
    r = requests.get(f"{settings.HACKERNEWS_API_URL}item/{post_id}.json")
    r.raise_for_status()

    post_res = r.json()
    url = post_res.get("url", f"{settings.HACKERNEWS_URL}item?id={post_id}")
    post = {
        "id": post_id,
        "url": url,
        "subject": post_res.get("title"),
        "rank": rank,
        "age": timeago.format(
            datetime.fromtimestamp(post_res.get("time")), datetime.now()
        ),
        "score": post_res.get("score"),
        "submitted_by": post_res.get("by"),
        "num_of_comments": len(post_res.get("kids", [])),
    }

    persist_result = persist.delay(post)
    fetch_sentiment.delay(post)
    return persist_result
    def start_scraping(response):

        ### REMOVE TBODY FROM ANY XPATH ! WARNING, else [] empty response
        for ticket in response.xpath(
                '/html/body/div/table/tr[position() > 2]'
        ):  # don't put get or extract here, > 2 because first two are header rows

            item = TicketItem()

            for date in ticket.xpath(
                    'normalize-space(./td[11]/div/text())').extract(
                    ):  # date string grabbed from every xpath
                datetime_object = datetime.datetime.strptime(
                    date, '%d/%m/%Y %I:%M %p'
                )  # formatted into proper datetime date object (comes like '27/05/2020 12:17 PM' on the website)
                time_ago = timeago.format(
                    datetime_object, datetime.datetime.now(
                    ))  # given date vs time taken now to povide time ago
                item[
                    'time_ago'] = time_ago + ' (' + date + ')'  # and finally, i have the time ago ( date ) for double check!

            item['id'] = ticket.xpath(
                'normalize-space(./td[6]/div/text())').extract()
            item['subject'] = ticket.xpath('td[7]/div/a/text()').extract()

            yield item

        open_in_browser(response)
Beispiel #24
0
    async def aboutbot(self, ctx):
        """ About the bot """
        ram_usage = self.process.memory_full_info().rss / 1024**2
        avg_members = round(len(self.bot.users) / len(self.bot.guilds))

        embed = discord.Embed(colour=int(repo.VERSION_DATA["ColourHex"], 16))
        embed.set_thumbnail(url=ctx.bot.user.avatar_url)
        embed.add_field(name="Last boot",
                        value=timeago.format(datetime.datetime.now() -
                                             self.bot.uptime),
                        inline=True)
        embed.add_field(name="Developers",
                        value="Ari Bowe, Julianne Cai",
                        inline=True)
        embed.add_field(name="Library", value="discord.py", inline=True)
        embed.add_field(
            name="Servers",
            value=f"{len(ctx.bot.guilds)} ( avg: {avg_members} users/server )",
            inline=True)
        embed.add_field(name="Commands loaded",
                        value=str(len([x.name for x in self.bot.commands])),
                        inline=True)
        embed.add_field(name="RAM", value=f"{ram_usage:.2f} MB", inline=True)

        await ctx.send(
            content=
            f"ℹ About **{ctx.bot.user}** | **Version {repo.VERSION_DATA['Version']}**",
            embed=embed)
Beispiel #25
0
 def format(self, value):
     try:
         value = self.parse(value)
         now = datetime.now(pytz.UTC)
         return timeago.format(value, now)
     except (AttributeError, ValueError) as e:
         raise fields.MarshallingError(e)
Beispiel #26
0
def cmd_list_pr(assignee, label, limit, state, project, organization) -> None:
    """
    Command for listing pull requests.

    devops: https://docs.microsoft.com/en-us/cli/azure/ext/azure-devops/repos/pr?view=azure-cli-latest#ext_azure_devops_az_repos_pr_list
    github cli: https://cli.github.com/manual/gh_pr_list
    """  # noqa
    assignee = replace_user_aliases(assignee)

    query = "az repos pr list "
    query += f'--status "{state}" '
    query += f"--top {limit} "
    query += f'--org "{organization}" '
    query += f'--project "{project}" '
    query += f'--repository "{get_repo_name()}" '

    # Generate a jmespath.org query
    # Example:
    # az repos pr list --status active --query "[*].{title: title, pullRequestID: pullRequestID, status: status, labels:labels[*].name, reviewers: reviewers[*].uniqueName} | [*].labels" -o jsonc # noqa
    jmespath_query = "[*].{title: title, pullRequestId: pullRequestId, creationDate: creationDate, status: status, labels:labels[*].name, reviewers: reviewers[*].uniqueName}"  # noqa
    if assignee and label:
        jmespath_query += " | [? reviewers!=\`null\` && labels!=\`null\`]"  # noqa
        jmespath_query += f" | [?{generate_jmespath(assignee, 'reviewers')} && {generate_jmespath(label, 'labels')}]"
    elif assignee:
        jmespath_query += " | [? reviewers!=\`null\`]"  # noqa
        jmespath_query += f" | [?{generate_jmespath(assignee, 'reviewers')}]"
    elif label:
        jmespath_query += " | [? labels!=\`null\`]"  # noqa
        jmespath_query += f" | [?{generate_jmespath(label, 'labels')}]"

    query += f'--query "{jmespath_query}" '

    prs = run_command(query)

    if len(prs) == 0:
        msg = "[dark_orange3]>[/dark_orange3] No pull requests given filters used"
        console.print(msg)
        return

    table = Table(title=f"{state} Pull requests")
    table.add_column("ID", justify="right", style="cyan", no_wrap=True)
    table.add_column("Title", justify="right", style="cyan", no_wrap=True)
    table.add_column("Reviewers", justify="right", style="cyan", no_wrap=True)
    table.add_column("Created", justify="right", style="cyan", no_wrap=True)
    table.add_column("Status", justify="right", style="cyan", no_wrap=True)

    for pr in prs:
        reviewers = ", ".join(pr.get("reviewers"))

        # Get created date
        # Example: '2021-06-18T09:57:56.653886+00:00'
        created = pr.get("creationDate")
        created = datetime.datetime.strptime(created, "%Y-%m-%dT%H:%M:%S.%f%z")
        now = datetime.datetime.now(timezone.utc)
        created = timeago.format(created, now)

        table.add_row(str(pr.get("pullRequestId")), pr.get("title"), reviewers,
                      created, pr.get("status"))

    console.print(table)
Beispiel #27
0
    def last_updated(self):
        if self.current_commit_timestamp:
            latest_datetime = datetime.fromtimestamp(
                self.current_commit_timestamp)
            return timeago.format(latest_datetime)

        return 'unknown'
Beispiel #28
0
async def get_inactive(client, message):
    cmd = message.command
    start = time.time()
    if len(cmd) > 1:
        limit = int(" ".join(cmd[1:]))
    else:
        limit = 0

    messages = [
        m
        async for member in client.iter_chat_members(
            message.chat.id, limit=limit, filter="recent"
        )
        if not member.user.is_deleted
        async for m in client.search_messages(
            message.chat.id, limit=1, from_user=member.user.id
        )
    ]

    delta = time.time() - start
    messages.sort(key=lambda k: k["date"])

    return "\n".join(
        [
            "[{}](tg://user?id={}) last [message](https://t.me/c/{}/{}) was {}".format(
                m.from_user.first_name,
                m.from_user.id,
                get_channel_id(m.chat.id),
                m.message_id,
                timeago.format(m.date),
            )
            for m in messages
        ]
        + [f"`{int(delta * 1000)}ms`"]
    )
Beispiel #29
0
def helpdata(city, res):
    output = Supplier.objects.filter(status="helpful",
                                     city__name=city,
                                     resources__name=res).first()
    print(output)
    if output:
        now = datetime.now()
        tz = pytz.timezone('Asia/Kolkata')
        now = now.astimezone(tz)
        name = output.name
        number = str(output.Number)
        email = output.email
        des = output.description
        updated = str(timeago.format(output.date_created, now))
        dic = {
            "name": name,
            "email": email,
            "des": des,
            "number": number,
            "updated": updated,
            "id": output.id,
            "isany": True
        }
        return (dic)
    else:
        dic = {"isany": False}
        return (dic)
Beispiel #30
0
def build_upload_date(update):
    yourdate = dateutil.parser.parse(update)
    now = datetime.datetime.now(
        datetime.timezone.utc) + datetime.timedelta(seconds=60 * 3.4)
    date = yourdate
    dtstring = timeago.format(date, now)
    return dtstring
def monitor_devices(devices):
    qry = """SELECT * FROM `phinder`.`devices` where id IN (%s);""" % devices
    devices = db.ex(qry)

    for d in devices:
        print 'Device: %s' % d[1]
        print '\tLast seen %s' % timeago.format(d[3])
        print '\tSeen by %s' % d[7]
Beispiel #32
0
def get_extra_server_info():
    server_name = [getattr(settings, x) for x in ['HOSTNAME', 'DEIS_APP', 'DEIS_DOMAIN']]
    server_name = '.'.join(x for x in server_name if x)
    server_info = {
        'name': server_name,
        'git_sha': GIT_SHA,
    }
    try:
        with open(DB_INFO_FILE, 'r') as fp:
            db_info = json.load(fp)
    except (IOError, ValueError):
        pass
    else:
        db_info['last_update'] = timeago.format(datetime.fromtimestamp(db_info['updated']))
        db_info['file_url'] = get_db_file_url(db_info['file_name'])
        for key, value in db_info.items():
            server_info['db_%s' % key] = value

    return server_info
Beispiel #33
0
def list_torrents(media_type, query):
    """
    Parameters: Media type and a query.
    Returns: N/A.
    Desc: Main function that lists torrents matching supplied media type
          and query. User can select from list of torrents, page forward and
          back, or exit.
    """
    torrent_hrefs = []  # List containing urls to torrents.

    # Check for an avilable mirror.
    link = check_kats()
    if not link:
        exit()  # No mirrors available so exit.

    # Generate API object with link returned by check_kats.
    kat = api(link)

    if media_type:
        kat.category(media_type)

    torrent_search = kat.search(query)

    page = 1  # Start at page 1. Page 0 indicates exit.
    while page > 0:

         # Print a heading replacing doubles spaces with single.
        print(' '.join(('Top '
                        + media_type.upper()
                        + ' torrents - Page '
                        + str(page)).split()))
        print('|{0: ^5}|{1: <65}|{2: >9}|{3: >15}|{4: >10}|{5: >10}|'
              .format('No.', 'Name', 'Size', 'Age', 'Seeds', 'Leechers'))
        print('|{0:-<119}|'.format('-'))

        count = 0
        for torrent in torrent_search.page(page):
            torrent_date = datetime.strptime(torrent["pubDate"],
                                             '%A %d %b %Y %H:%M:%S %z')
            now = datetime.now()
            print('|{0: ^5}|{1: <65}|{2: >9}|{3: >15}|{4: >10}|{5: >10}|'
                  .format(count, torrent["title"][:60],
                          pretty_size(torrent["size"]),
                          timeago.format(
                              torrent_date.replace(tzinfo=None), now),
                          torrent["seeds"],
                          torrent["leechs"]))
            torrent_hrefs.append(torrent["link"])
            count += 1

        # Footer
        print('|{0:-<119}|'.format('-'))

        if page > 1:
            req_torrents = input('Enter torrent numbers to download, '
                                 '"e" to exit, "n" for next page, or '
                                 '"p" for previous page: ')
        else:
            req_torrents = input('Enter torrent numbers to download, '
                                 '"e" to exit, or "n" for next page: ')

        if 'e' in req_torrents.lower():  # Exit.
            page = 0
        elif 'n' in req_torrents.lower():  # Next page.
            page += 1
        elif 'p' in req_torrents.lower():  # Previous page.
            page = max(1, page - 1)
        else:  # Download torrents
            page = 0  # Exit after the torrents have been downloaded.
            if ',' in req_torrents:
                for x in req_torrents.split(','):
                    try:
                        i = int(x)
                    except:
                        print(x + " is an invalid torrent number - ignored!")
                        continue
                    if i >= 0 and i < count:
                        download_torrent(torrent_hrefs[i])
                    else:
                        print(x + " is an invalid torrent number - ignored!")
            else:
                try:
                    i = int(req_torrents)
                except:
                    print(req_torrents
                          + " is an invalid torrent number - ignored!")
                    continue
                if i >= 0 and i < count:
                    download_torrent(torrent_hrefs[i])
                else:
                    print(req_torrents
                          + " is an invalid torrent number - ignored!")
Beispiel #34
0
 def test_timeago_en(self):
     locale = None
     date = datetime.datetime.now()
     now = date + datetime.timedelta(seconds = 2)
     self.assertEqual(timeago.format(date, now, locale), 'just now')
     
     now = date + datetime.timedelta(seconds = 10)
     self.assertEqual(timeago.format(date, now, locale), '10 seconds ago')
     
     now = date + datetime.timedelta(seconds = 12)
     self.assertEqual(timeago.format(date, now, locale), '12 seconds ago')
     
     now = date + datetime.timedelta(seconds = 60)
     self.assertEqual(timeago.format(date, now, locale), '1 minute ago')
     
     now = date + datetime.timedelta(seconds = 60 * 3.4)
     self.assertEqual(timeago.format(date, now, locale), '3 minutes ago')
     
     now = date + datetime.timedelta(seconds = 3600)
     self.assertEqual(timeago.format(date, now, locale), '1 hour ago')
     
     now = date + datetime.timedelta(seconds = 3600 * 2)
     self.assertEqual(timeago.format(date, now, locale), '2 hours ago')
     
     now = date + datetime.timedelta(seconds = 86400)
     self.assertEqual(timeago.format(date, now, locale), '1 day ago')
     
     now = date + datetime.timedelta(seconds = 86400 * 4.5)
     self.assertEqual(timeago.format(date, now, locale), '4 days ago')
     
     now = date + datetime.timedelta(seconds = 2592000)
     self.assertEqual(timeago.format(date, now, locale), '1 month ago')
     
     now = date + datetime.timedelta(seconds = 2592000 * 3.5)
     self.assertEqual(timeago.format(date, now, locale), '3 months ago')
     
     now = date + datetime.timedelta(seconds = 31536000)
     self.assertEqual(timeago.format(date, now, locale), '1 year ago')
     
     now = date + datetime.timedelta(seconds = 31536000 * 1.1)
     self.assertEqual(timeago.format(date, now, locale), '1 year ago')
     
     now = date + datetime.timedelta(seconds = 31536000 * 2.1)
     self.assertEqual(timeago.format(date, now, locale), '2 years ago')
Beispiel #35
0
 def test_timeago_delta(self):
     locale = None
     date = datetime.timedelta(seconds = 9)
     self.assertEqual(timeago.format(date, None, locale), 'just now')
Beispiel #36
0
 def updated_at_ago(self):
     return (
         timeago.format(self.updated_at, datetime.now()) if self.updated_at else None
     )
Beispiel #37
0
    def test_timeago_cn_in(self):
        locale = 'zh_CN'

        date = datetime.datetime.now()
        now = date + datetime.timedelta(seconds = 2)
        self.assertEqual(timeago.format(now, date, locale), u'片刻后')
        
        now = date + datetime.timedelta(seconds = 10)
        self.assertEqual(timeago.format(now, date, locale), u'10秒后')
        
        now = date + datetime.timedelta(seconds = 12)
        self.assertEqual(timeago.format(now, date, locale), u'12秒后')
        
        now = date + datetime.timedelta(seconds = 60)
        self.assertEqual(timeago.format(now, date, locale), u'1分钟后')
        
        now = date + datetime.timedelta(seconds = 60 * 3.4)
        self.assertEqual(timeago.format(now, date, locale), u'3分钟后')
        
        now = date + datetime.timedelta(seconds = 3600)
        self.assertEqual(timeago.format(now, date, locale), u'1小时后')
        
        now = date + datetime.timedelta(seconds = 3600 * 2)
        self.assertEqual(timeago.format(now, date, locale), u'2小时后')
        
        now = date + datetime.timedelta(seconds = 86400)
        self.assertEqual(timeago.format(now, date, locale), u'1天后')
        
        now = date + datetime.timedelta(seconds = 86400 * 4.5)
        self.assertEqual(timeago.format(now, date, locale), u'4天后')
        
        now = date + datetime.timedelta(seconds = 2592000)
        self.assertEqual(timeago.format(now, date, locale), u'1月后')
        
        now = date + datetime.timedelta(seconds = 2592000 * 3.5)
        self.assertEqual(timeago.format(now, date, locale), u'3月后')
        
        now = date + datetime.timedelta(seconds = 31536000)
        self.assertEqual(timeago.format(now, date, locale), u'1年后')
        
        now = date + datetime.timedelta(seconds = 31536000 * 1.1)
        self.assertEqual(timeago.format(now, date, locale), u'1年后')
        
        now = date + datetime.timedelta(seconds = 31536000 * 2.1)
        self.assertEqual(timeago.format(now, date, locale), u'2年后')
Beispiel #38
0
    def test_timeago_en_in(self):
        locale = 'en'

        date = datetime.datetime.now()
        now = date + datetime.timedelta(seconds = 2)
        self.assertEqual(timeago.format(now, date, locale), 'a while')
        
        now = date + datetime.timedelta(seconds = 10)
        self.assertEqual(timeago.format(now, date, locale), 'in 10 seconds')
        
        now = date + datetime.timedelta(seconds = 12)
        self.assertEqual(timeago.format(now, date, locale), 'in 12 seconds')
        
        now = date + datetime.timedelta(seconds = 60)
        self.assertEqual(timeago.format(now, date, locale), 'in 1 minute')
        
        now = date + datetime.timedelta(seconds = 60 * 3.4)
        self.assertEqual(timeago.format(now, date, locale), 'in 3 minutes')
        
        now = date + datetime.timedelta(seconds = 3600)
        self.assertEqual(timeago.format(now, date, locale), 'in 1 hour')
        
        now = date + datetime.timedelta(seconds = 3600 * 2)
        self.assertEqual(timeago.format(now, date, locale), 'in 2 hours')
        
        now = date + datetime.timedelta(seconds = 86400)
        self.assertEqual(timeago.format(now, date, locale), 'in 1 day')
        
        now = date + datetime.timedelta(seconds = 86400 * 4.5)
        self.assertEqual(timeago.format(now, date, locale), 'in 4 days')
        
        now = date + datetime.timedelta(seconds = 2592000)
        self.assertEqual(timeago.format(now, date, locale), 'in 1 month')
        
        now = date + datetime.timedelta(seconds = 2592000 * 3.5)
        self.assertEqual(timeago.format(now, date, locale), 'in 3 months')
        
        now = date + datetime.timedelta(seconds = 31536000)
        self.assertEqual(timeago.format(now, date, locale), 'in 1 year')
        
        now = date + datetime.timedelta(seconds = 31536000 * 1.1)
        self.assertEqual(timeago.format(now, date, locale), 'in 1 year')
        
        now = date + datetime.timedelta(seconds = 31536000 * 2.1)
        self.assertEqual(timeago.format(now, date, locale), 'in 2 years')
Beispiel #39
0
  def add(cls, story):
    story_id_int = story.get('id')
    story_id = str(story_id_int)
    short_id = shortener.encode(story_id_int)
    hn_url = "https://news.ycombinator.com/item?id={}".format(story_id)
    story_url = story.get('url')

    # Check memcache and databse, maybe this story was already sent
    if memcache.get(story_id):
      logging.info('STOP: {} in memcache'.format(story_id))
      return
    post = ndb.Key(cls, story_id).get()
    if post:
      logging.info('STOP: {} in DB'.format(story_id))
      post.add_memcache()
      return
    logging.info('SEND: {}'.format(story_id))

    story['title'] = story.get('title').encode('utf-8')
    comments_count = story.get('descendants', 0)
    buttons = []

    if development():
      short_hn_url = 'http://*****:*****@hacker_news_feed_st', message,
                            {'inline_keyboard': [buttons]})
    else:
      result = send_message('@hacker_news_feed', message,
                            {'inline_keyboard': [buttons]})

    logging.info('Telegram response: {}'.format(result))

    telegram_message_id = None
    if result and result.get('ok'):
      telegram_message_id = result.get('result').get('message_id')
    post = cls(id=story_id, title=story.get('title'), url=story.get('url'),
               score=story.get('score'), text=story.get('text'),
               short_url=short_url, short_hn_url=short_hn_url,
               message=message, telegram_message_id=telegram_message_id)
    post.put()
    post.add_memcache()
Beispiel #40
0
 def published_at_ago(self):
     return (
         timeago.format(self.published_at, datetime.now())
         if self.published_at
         else None
     )
Beispiel #41
0
 def timeago(date):
     return timeago.format(date, datetime.datetime.now())
Beispiel #42
0
    def last_updated(self):
        if self.latest_ref_timestamp:
            latest_datetime = datetime.fromtimestamp(self.latest_ref_timestamp)
            return timeago.format(latest_datetime)

        return 'unknown'
Beispiel #43
0
 def test_timeago_cn(self):
     locale = 'zh_CN'
     date = datetime.datetime.now()
     now = date + datetime.timedelta(seconds = 2)
     self.assertEqual(timeago.format(date, now, locale), u'刚刚')
     
     now = date + datetime.timedelta(seconds = 10)
     self.assertEqual(timeago.format(date, now, locale), u'10秒前')
     
     now = date + datetime.timedelta(seconds = 12)
     self.assertEqual(timeago.format(date, now, locale), u'12秒前')
     
     now = date + datetime.timedelta(seconds = 60)
     self.assertEqual(timeago.format(date, now, locale), u'1分钟前')
     
     now = date + datetime.timedelta(seconds = 60 * 3.4)
     self.assertEqual(timeago.format(date, now, locale), u'3分钟前')
     
     now = date + datetime.timedelta(seconds = 3600)
     self.assertEqual(timeago.format(date, now, locale), u'1小时前')
     
     now = date + datetime.timedelta(seconds = 3600 * 2)
     self.assertEqual(timeago.format(date, now, locale), u'2小时前')
     
     now = date + datetime.timedelta(seconds = 86400)
     self.assertEqual(timeago.format(date, now, locale), u'1天前')
     
     now = date + datetime.timedelta(seconds = 86400 * 4.5)
     self.assertEqual(timeago.format(date, now, locale), u'4天前')
     
     now = date + datetime.timedelta(seconds = 2592000)
     self.assertEqual(timeago.format(date, now, locale), u'1月前')
     
     now = date + datetime.timedelta(seconds = 2592000 * 3.5)
     self.assertEqual(timeago.format(date, now, locale), u'3月前')
     
     now = date + datetime.timedelta(seconds = 31536000)
     self.assertEqual(timeago.format(date, now, locale), u'1年前')
     
     now = date + datetime.timedelta(seconds = 31536000 * 1.1)
     self.assertEqual(timeago.format(date, now, locale), u'1年前')
     
     now = date + datetime.timedelta(seconds = 31536000 * 2.1)
     self.assertEqual(timeago.format(date, now, locale), u'2年前')