Beispiel #1
0
    def fetch_problem(self, slug, accepted=False):
        print(f"🤖 Fetching problem: https://leetcode.com/problem/{slug}/...")
        query_params = {
            'operationName': "getQuestionDetail",
            'variables': {'titleSlug': slug},
            'query': '''query getQuestionDetail($titleSlug: String!) {
                        question(titleSlug: $titleSlug) {
                            questionId
                            questionFrontendId
                            questionTitle
                            questionTitleSlug
                            content
                            difficulty
                            stats
                            similarQuestions
                            categoryTitle
                            topicTags {
                            name
                            slug
                        }
                    }
                }'''
        }

        resp = self.session.post(
            "https://leetcode.com/graphql",
            data=json.dumps(query_params).encode('utf8'),
            headers={
                "content-type": "application/json",
            })
        body = json.loads(resp.content)

        # parse data
        question = get(body, 'data.question')

        Problem.replace(
            id=question['questionId'], display_id=question['questionFrontendId'], title=question["questionTitle"],
            level=question["difficulty"], slug=slug, description=question['content'],
            accepted=accepted
        ).execute()

        for item in question['topicTags']:
            if Tag.get_or_none(Tag.slug == item['slug']) is None:
                Tag.replace(
                    name=item['name'],
                    slug=item['slug']
                ).execute()

            ProblemTag.replace(
                problem=question['questionId'],
                tag=item['slug']
            ).execute()
        random_wait(10, 15)
Beispiel #2
0
 def get(self):
     key = self.request.get("Key")
     aKey = ndb.Key(urlsafe=key)
     anArticle = aKey.get()
     data = {}
     filters = []
     user = getUser()
     if user:
         data['LoggedIn'] = True
         q1 = Tag.query()
         q1 = q1.filter(Tag.user == user.user_id())
         for aFilter in q1:
             filters.append(aFilter.json())
         theProxy = getTags(user, anArticle)
         if theProxy:
             if theProxy.score == -1:
                 data['Vote'] = {'Scan': 'No Tag', 'ScanKey': 'Unknown'}
             else:
                 data['Vote'] = theProxy.json()
         else:
             data['Vote'] = {'Scan': 'Unknown', 'ScanKey': 'Unknown'}
     else:
         data['LoggedIn'] = False
     data['Post'] = anArticle.json()
     url = users.create_logout_url("VoteData?Key=" + key)
     data['Logout'] = url
     data['Filter'] = filters
     template = JINJA_ENVIRONMENT.get_template('Vote.html')
     self.response.out.write(template.render(data))
Beispiel #3
0
 async def settag(self, ctx: commands.Context, tag: adjust_tag, *,
                  content: str):
     """Create or update a tag. If content is -d, deletes tag."""
     if content == '-d':
         Tag.delete_by_id(tag)
         regen_cache()
         await ctx.send("Tag deleted.")
         return
     title = ""
     if '\n' in content:
         title, content = content.split("\n", 1)
     (Tag.insert(content=content, tag=tag,
                 title=title).on_conflict_replace().execute())
     regen_cache()
     await ctx.send(embed=Embed(
         description=f"Created or updated tag {tag} ({title})"))
Beispiel #4
0
 async def list_tags(self, ctx: commands.Context):
     paginator = commands.Paginator()
     for tag in Tag.select(Tag.tag, Tag.title):
         paginator.add_line(f"{tag.tag} - {tag.title}")
     for page_number, page in enumerate(paginator.pages):
         await ctx.author.send(embed=Embed(
             title=f"Tags Page #{page_number}", description=page))
     await ctx.message.delete()
Beispiel #5
0
 def post(self):
     key = self.request.get("Key")
     tag = self.request.get("Tag")
     aKey = ndb.Key(urlsafe=key)
     anArticle = aKey.get()
     user = getUser()
     if user:
         q1 = Tag.query()
         q1 = q1.filter(Tag.user == user.user_id())
         q1 = q1.filter(Tag.scan == tag)
         aTag = q1.get()
         if not aTag:
             aTag = Tag(scan=str(tag),
                        scanKey=str(tag),
                        user=user.user_id())
             aTag.put()
     self.response.out.write('Create Tag')
Beispiel #6
0
 async def help(self, ctx: commands.Context):
     d_tag = Tag.get_or_none(Tag.tag == 'help')
     if d_tag is None:
         await ctx.send(
             content='Currently there is no help tag set. Use listcommands.'
         )
     else:
         await send_tag(ctx.author, d_tag)
     await ctx.message.delete()
def test_isolation2(app):
    tag = Tag(id=str(uuid.uuid4()), name="Test tag 3")
    db.session.add(tag)
    db.session.commit()

    assert len(Tag.query.all()) == 1
    # test with query
    tag_db = tag.query.first()
    assert tag_db.name == "Test tag 3"
Beispiel #8
0
 def post(self):
     key = self.request.get("Key")
     tag = self.request.get("Tag")
     aKey = ndb.Key(urlsafe=key)
     anArticle = aKey.get()
     user = getUser()
     if user:
         q1 = Tag.query()
         q1 = q1.filter(Tag.scan == tag)
         q1 = q1.filter(Tag.user == user.user_id())
         aTag = q1.get()
         if not aTag:
             aTag = Tag(scan=str(tag),
                        scanKey=str(tag),
                        user=user.user_id())
             aTag.put()
         q1 = ProxyTag.query()
         q1 = q1.filter(ProxyTag.tag == aTag.key)
         q1 = q1.filter(ProxyTag.article == anArticle.key)
         q1 = q1.filter(ProxyTag.user == user.user_id())
         aProxy = q1.get()
         if aProxy:
             if aProxy.score == -1:
                 self.response.out.write('Already Updated')
                 return
             aProxy.key.delete()
             aProxy = ProxyTag(tag=aTag.key,
                               article=anArticle.key,
                               user=user.user_id(),
                               score=-1).put()
             queue = taskqueue.Queue(name='default')
             task = taskqueue.Task(url='/RemoveFromFilter',
                                   target='worker',
                                   params={
                                       'article': anArticle.key.urlsafe(),
                                       'tag': aTag.scan,
                                       'user': user.user_id()
                                   })
             rpc = queue.add_async(task)
             # Wait for the rpc to complete and return the queued task.
             task = rpc.get_result()
             self.response.out.write('Updated')
         else:
             self.response.out.write('Not Present')
Beispiel #9
0
 async def whatis(self, ctx: commands.Context, *, tag: adjust_tag):
     d_tag = Tag.get_or_none(Tag.tag == tag)
     if d_tag is None:
         await ctx.send(
             embed=Embed(title=f"Unknown tag {tag}",
                         description="Did you mean:\n" +
                         '\n'.join(find_similar_results(tag)[:5])))
     else:
         await send_tag(ctx.author, d_tag)
     await ctx.message.delete()
Beispiel #10
0
def update_or_insert(info):
    session = DBSession()
    target_movie = session.query(Movie).filter_by(**{'title': info['basic']['title'], 'year': info['basic']['year']}) \
        .first()
    if target_movie:
        target_movie.uri = info['basic']['uri']
        target_movie.update_date = info['basic']['update_date']
        target_movie.viedo_files = info['basic']['viedo_files']

    else:
        new_movie = Movie(**info['basic'])
        session.add(new_movie)
        # 推送新电影信息
        session.flush()
        push_run(info, new_movie.id)

        # 更新Tag
        for tag in info['tags']:
            if session.query(Tag).filter(Tag.text == tag).count() > 0:
                tar_tag = session.query(Tag).filter(Tag.text == tag).first()
            else:
                tar_tag = Tag(tag)
                session.add(tar_tag)
                session.flush()
            new_movie_tag = MovieTag(new_movie.id, tar_tag.id)
            session.add(new_movie_tag)

        # 更新导演信息
        for director in info['directors']:
            if session.query(Role).filter(
                    Role.name == director['name']).count() > 0:
                tar_role = session.query(Role).filter(
                    Role.name == director['name']).first()
            else:
                tar_role = Role(director['name'], director['info'])
                session.add(tar_role)
                session.flush()
            new_movie_director = MovieDirector(new_movie.id, tar_role.id)
            session.add(new_movie_director)

        # 更新演员信息
        for actor in info['actors']:
            if session.query(Role).filter(
                    Role.name == actor['name']).count() > 0:
                tar_role = session.query(Role).filter(
                    Role.name == actor['name']).first()
            else:
                tar_role = Role(actor['name'], actor['info'])
                session.add(tar_role)
                session.flush()
            new_movie_director = MovieActor(new_movie.id, tar_role.id)
            session.add(new_movie_director)

    session.commit()
    session.close()
Beispiel #11
0
 def get(self, *args, **kwargs):
     tag = args[0]
     tag_query = Tag.select().where(Tag.content == tag).first()
     if tag_query:
         archives2tag_query = Archive2Tag.select(Archive2Tag.archive_id).where(Archive2Tag.tag_id == tag_query.id)
         archive_ids = [one.archive_id for one in archives2tag_query]
         archives = Archive.select().where(
             (Archive.id << archive_ids) & (Archive.status == 1) & (Archive.type == 0)).order_by(
             Archive.published_time.desc())
         archives_groups = [{'year': year, 'archives': list(archives)}
                            for year, archives in groupby(archives, key=lambda a: humantime(a.published_time, "%Y"))]
         self.render("timeline.html", archives_groups=archives_groups, first_title="Tag: %s" % tag)
Beispiel #12
0
def compose():
    if request.method == "POST":
        if len(request.form.get("name")) < 4:
            flash("Snippet name must be at least 4 characters long")
            return redirect(request.referrer)
        request_tags = request.form.get("tags").replace(' ', '').split(',')
        if '' in request_tags:
            flash("Please provide at least one tag")
            return redirect(request.referrer)
        db_session()
        tags = {tag.name.lower(): tag for tag in db_session.query(Tag).all()}
        snippet = Snippet(name=request.form.get("name"),
                          snippet=request.form.get("codeSnippet"),
                          description=request.form.get("description"),
                          mode=request.form.get("mode"),
                          user=db_session.query(User).filter(
                              User.id == session["user_id"]).first())
        sn_tags = []
        new_tags = []
        for form_tag in request_tags:
            if form_tag.lower() in tags.keys():
                sn_tags.append(tags[form_tag.lower()])
            else:
                new_tags.append(Tag(form_tag))

        if new_tags:
            db_session.add_all(new_tags)
            for tag in new_tags:
                sn_tags.append(tag)
        snippet.add_tags(sn_tags)
        db_session.add(snippet)
        db_session.commit()
        db_session.remove()
        flash("Snippet saved")

        return redirect(url_for('index'))

    else:
        db_session()
        tags = [tag.name for tag in db_session.query(Tag).all()]
        db_session.remove()
        return render_template("compose.html", tags=tags)
Beispiel #13
0
def process_outline(outline: Element, current_tags: list,
                    user: UserModel) -> None:
    """Processes an OPML outline element. Feed elements will be added or updated as necessary, and child tags will be
    recursively processed.

    :param outline: The outline element to process
    :param current_tags: Current tags in this branch of the XML tree
    :param user: The user importing these feeds & tags
    """

    # get the attributes out of the outline
    attrib: dict = outline.attrib

    # is this a feed?
    outline_type: str = attrib.get('type')
    if outline_type is not None and outline_type == 'rss':
        url: str = attrib.get('xmlUrl')
        fetch_and_store_feed(url, current_tags, user)
        return

    # if this isn't a feed, it's a tag
    text: str = attrib['text']
    tag: Optional[TagModel] = TagModel.get(label=text, user=user)
    if tag is None:
        tag = TagModel(label=text, user=user)

    # tags can be child elements of other tags (thanks OPML), so if this tag isn't already in the list of tags, add it
    # also need to remember if this branch of the XML tree already has this tag, so we don't remove it prematurely
    if tag in current_tags:
        tag = None
    else:
        current_tags.append(tag)

    # process child outlines
    for child in outline:
        process_outline(child, current_tags, user)

    # remove the tag if this was the outermost instance of the tag
    if tag is not None:
        current_tags.remove(tag)
Beispiel #14
0
def addTags():
    post_data = request.get_json()
    url = post_data.get('article_url')
    exists = db.session.query(
        Article.query.filter(Article.article_url == url).exists()).scalar()
    if exists:
        tags = post_data.get("tags")
        for tag in tags:
            db.session.add(Tag(article_url=url, tag=tag))
            db.session.commit()

        response_object = {
            'status': 'success',
            'message': f'{tags} was added!'
        }
        return jsonify(response_object), 201

    else:
        response_object = {
            'status': 'Failure',
            'message': 'Schema constraint error'
        }
        return jsonify(response_object), 409
Beispiel #15
0
def test_add_tag_to_kind(client):
    # somehow check_quick_token() looses the request in test setup
    with mock.patch("flask_security.decorators._check_token",
                    return_value=True):
        with mock.patch("flask_principal.Permission.can", return_value=True):
            kind_id = str(uuid.uuid4())
            kind = Kind(
                id=kind_id,
                name="Indica",
                c=False,
                h=False,
                i=True,
                s=False,
                short_description_nl="Short NL",
                description_nl="Description NL",
                short_description_en="Short NL",
                description_en="Description EN",
            )
            tag = Tag(id=str(uuid.uuid4()), name="Gigly")
            db.session.add(kind)
            db.session.add(tag)
            db.session.commit()

            data = {
                "kind_id": str(kind.id),
                "tag_id": str(tag.id),
                "amount": 60
            }
            response = client.post(f"/v1/kinds-to-tags",
                                   json=data,
                                   follow_redirects=True)
            assert response.status_code == 201

            response = client.get(f"/v1/kinds-to-tags", follow_redirects=True)
            assert response.status_code == 200
            json = response.json
            assert len(json) == 1
Beispiel #16
0
def update_or_insert(info):
    session = DBSession()
    target_movie = session.query(Movie).filter_by(**{'title': info['basic']['title'], 'year': info['basic']['year']}) \
        .first()
    if target_movie:
        target_movie.uri = info['basic']['uri']
        target_movie.update_date = info['basic']['update_date']

    else:
        new_movie = Movie(**info['basic'])
        session.add(new_movie)

        for tag in info['tags']:
            if session.query(Tag).filter(Tag.text == tag).count() > 0:
                tar_tag = session.query(Tag).filter(Tag.text == tag).first()
            else:
                tar_tag = Tag(tag)
                session.add(tar_tag)
                session.flush()
            new_movie_tag = MovieTag(new_movie.id, tar_tag.id)
            session.add(new_movie_tag)

    session.commit()
    session.close()
Beispiel #17
0
 def post(self):
     """New Tag"""
     tag = Tag(id=str(uuid.uuid4()), **api.payload)
     save(tag)
     return tag, 201
Beispiel #18
0
def regen_cache():
    tag_id_cache.clear()
    for t in Tag.select(Tag.tag):
        tag_id_cache.add(t.tag)
Beispiel #19
0
 async def on_member_join(self, member: Member):
     d_tag = Tag.get_or_none(Tag.tag == 'welcome')
     if d_tag:
         await send_tag(member, d_tag)
Beispiel #20
0
def edit(snippet_id):
    if request.method == "GET":
        if not snippet_id:
            flash("No snippet id provided")
            return redirect(url_for('index'))
        db_session()
        tags = [tag.name for tag in db_session.query(Tag).all()]
        snippet = db_session.query(Snippet).filter(Snippet.id == snippet_id). \
            filter(Snippet.user_id == session["user_id"]).first()
        if not snippet:
            db_session.remove()
            flash(
                "Something went wrong, probably provided the wrong snippet id or the snippet is not yours"
            )
            return redirect(url_for('index'))
        return render_template("edit.html", snippet=snippet, tags=tags)
    else:
        # Update snippet
        if not snippet_id:
            flash("No snippet id provided")
            return redirect(url_for('index'))
        if len(request.form.get("name")) < 4:
            flash("Snippet name must be at least 4 characters long")
            return redirect(request.referrer)
        db_session()
        snippet = db_session.query(Snippet).filter(Snippet.id == snippet_id). \
            filter(Snippet.user_id == session["user_id"]).first()
        if not snippet:
            db_session.remove()
            flash(
                "Something went wrong, probably provided the wrong snippet id or the snippet is not yours"
            )
            return redirect(url_for('index'))
        snippet.name = request.form.get("name")
        snippet.snippet = request.form.get("codeSnippet")
        snippet.description = request.form.get("description")
        snippet.mode = request.form.get("mode")
        snippet.tags = []
        tags = {tag.name.lower(): tag for tag in db_session.query(Tag).all()}
        sn_tags = []
        new_tags = []
        request_tags = request.form.get("tags").replace(' ', '').split(',')
        if '' in request_tags:
            db_session.remove()
            flash("Please provide at least one tag")
            return redirect(request.referrer)
        for form_tag in request_tags:
            if form_tag.lower() in tags.keys():
                sn_tags.append(tags[form_tag.lower()])
            else:
                new_tags.append(Tag(form_tag))

        if new_tags:
            db_session.add_all(new_tags)
            for tag in new_tags:
                sn_tags.append(tag)
        snippet.add_tags(sn_tags)
        db_session.commit()
        db_session.remove()
        flash("Snippet updated")
        return redirect(url_for('index'))
def find_or_create_tag(tagName):
    tag = Tag.query.filter_by(name=tagName).first()
    return tag if tag else Tag(name=tagName)