Example #1
0
def newPaperStore(paper):
    # This creates the subarxiv areas
    sub = paper["header"]["setSpec"]
    if isinstance(sub, list) == False:
        subareas = []
        subareas.append(sub)
    else:
        subareas = sub

    for subarea in subareas:
        if subArxiv.objects.filter(region=subarea).count() == 0:
            subarx = subArxiv(region=subarea)
            subarx.save()

    article = paper["metadata"]["arXiv"]

    # Checks if the paper is new or has been updated (if it's been updated it is not new)
    if "updated" in article:
        newtemp = "no"
    else:
        newtemp = "yes"

    date_added = paper["header"]["datestamp"]
    title = article["title"]
    abstract = article["abstract"]
    arxiv_no = article["id"]

    # Adding the paper to the temperary model only adds the paper if is not already in the database
    if newPaper.objects.filter(arxiv_no=arxiv_no).count() == 0:
        tempPap = newPaper(title=title, abstract=abstract, arxiv_no=arxiv_no, added_at=date_added, new=newtemp)
        tempPap.save()
        # Attaches the subarxivs to the paper
        for subarea in subareas:
            temp = subArxiv.objects.get(region=subarea)
            tempPap.area.add(temp)

    else:
        tempPap = newPaper.objects.get(arxiv_no=arxiv_no)
        for subarea in subareas:
            temp = subArxiv.objects.get(region=subarea)
            tempPap.area.add(temp)

            # Attaches the authors to the paper
    authors = article["authors"]["author"]

    if isinstance(authors, list) == False:
        newAuthors = []
        newAuthors.append(authors)
    else:
        newAuthors = authors

    for author in newAuthors:
        if "forenames" in author:
            firstName = author["forenames"]
        else:
            firstName = None

        secondName = author["keyname"]

        if Author.objects.filter(firstName=firstName, secondName=secondName).count() == 0:

            temp = Author(firstName=firstName, secondName=secondName)
            temp.save()
            # Adds the paper to the Author
            temp.newarticles.add(tempPap)

        else:
            temp = Author.objects.get(firstName=firstName, secondName=secondName)
            temp.newarticles.add(tempPap)
Example #2
0
def paperStore(paperID, api):

    if api == "Inspires":
        url = (
            "https://inspirehep.net/record/"
            + str(paperID)
            + "?of=recjson&ot=recid,number_of_citations,authors,title,abstract,publication_info,primary_report_number"
        )

    elif api == "arXiv":
        url = (
            "https://inspirehep.net/search?ln=en&p="
            + str(paperID)
            + "&of=recjson&ot=recid,number_of_citations,authors,title,abstract,publication_info,primary_report_number"
        )

    r = requests.get(url).json()[0]
    title = r["title"]["title"]
    Inspires_no = r["recid"]

    if isinstance(r["primary_report_number"], list):
        arxiv_no = r["primary_report_number"][0]
    else:
        arxiv_no = r["primary_report_number"]

        # The format the abstracts come in is non standard, this seems to pick up the cases well enough....
    if "abstract" in r:
        if isinstance(r["abstract"], list):
            abstract = r["abstract"][0]["summary"]
        else:
            abstract = r["abstract"]["summary"]
    else:
        abstract = "No abstract"

        # Create the journal ref
    info = r["publication_info"]
    if info != None:
        journal_ref = info["title"] + info["volume"] + " " + "(" + info["year"] + ")" + " " + info["pagination"] + "."
    else:
        journal_ref = None

        # Save the paper to the database
    paperObj = Paper(title=title, abstract=abstract, Inspires_no=Inspires_no, journal=journal_ref, arxiv_no=arxiv_no)
    paperObj.save()

    # Adds the authors to the database ad links them to the paper
    length = len(r["authors"])

    for i in range(length):
        PaperObj = Paper.objects.get(Inspires_no=Inspires_no)

        # Checks to see if the Author is already in the database (use unique id in future)
        if (
            Author.objects.filter(
                firstName=r["authors"][i]["first_name"], secondName=r["authors"][i]["last_name"]
            ).count()
            == 0
        ):

            temp = Author(firstName=r["authors"][i]["first_name"], secondName=r["authors"][i]["last_name"])
            temp.save()
            temp.articles.add(PaperObj)

            # Adds the paper to the Author
        else:
            temp = Author.objects.get(firstName=r["authors"][i]["first_name"], secondName=r["authors"][i]["last_name"])
            temp.articles.add(PaperObj)

    return paperObj
Example #3
0
def messageSubmission(request):
    message = request.POST["message"]
    message_id = request.POST["id"]
    arxivno = request.POST["arxivno"]

    # Check if the message is clean or not
    if not checkClean(message):
        return JsonResponse({"messageHTML": "UNCLEAN"})

    else:

        # If the paper has an arXiv number
        if arxivno != "0":

            # We check the newPaper and Paper databases
            if newPaper.objects.filter(arxiv_no=arxivno).count() != 0:
                paper = newPaper.objects.get(arxiv_no=arxivno)
                numposts = len(paper.post_set.all())
                post = Post(message=message, new_paper=paper, messageID=numposts + 1, poster=request.user)

            elif Paper.objects.filter(arxiv_no=arxivno).count() != 0:
                paper = Paper.objects.get(arxiv_no=arxivno)
                numposts = len(paper.post_set.all())
                post = Post(message=message, paper=paper, messageID=numposts + 1, poster=request.user)

                # Else we create the paper object
            else:
                p = arXivSearch(arxivno, "")["paperList"][0]
                paper = Paper(
                    title=p["title"], abstract=p["abstract"], arxiv_no=p["arxiv_no"], journal=p["journal_ref"]
                )
                paper.save()
                post = Post(message=message, paper=paper, messageID=1, poster=request.user)

                authors = p["authorlist"]

                for author in authors:

                    if (
                        Author.objects.filter(firstName=author["firstName"], secondName=author["secondName"]).count()
                        == 0
                    ):

                        temp = Author(firstName=author["firstName"], secondName=author["secondName"])
                        temp.save()
                        # Adds the paper to the Author
                        temp.articles.add(paper)

                    else:
                        temp = Author.objects.get(firstName=author["firstName"], secondName=author["secondName"])
                        temp.articles.add(paper)

                        # If the paper does not have an arXiv number
        else:
            # We just have to look for the paper in the Paper database
            if Paper.objects.filter(Inspires_no=message_id).count() == 1:
                paper = Paper.objects.get(Inspires_no=message_id)
                numposts = len(paper.post_set.all())
                post = Post(message=message, paper=paper, messageID=numposts + 1, poster=request.user)

                # Else we create the paper object
            else:
                temp = paperStore(message_id, "Inspires")
                post = Post(message=message, paper=temp, messageID=1, poster=request.user)

        post.save()

        context = {"message": post.message, "time": post.date, "number": post.messageID, "user": post.poster.username}
        template = loader.get_template("message.html")

        temp = str(template.render(context).encode("utf8"))

        return JsonResponse({"messageHTML": temp, "message_number": post.messageID})