Skip to content

Commit

Permalink
merge upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
wildintellect committed Feb 27, 2021
2 parents 4288e0d + 38c9726 commit 8264551
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 6 deletions.
72 changes: 72 additions & 0 deletions ExtraField.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 5 10:39:08 2018
@author: mmtobias
"""

import json
import requests
import urllib.request
from math import ceil

userID = ''
collectionID = ''
apiKey = ''

def api_get(userID, collectionID, apiKey, limit=100, start=0):
api_url = 'https://api.zotero.org/users/%s/collections/%s/items?key=%s&limit=%s&start=%s&itemType=-attachment || note' % (userID, collectionID, apiKey, limit, start)
#QgsMessageLog.logMessage(api_url, 'LiteratureMapper', Qgis.Info)
zotero_response = requests.get(api_url)
return zotero_response

def parse_zotero(zotero_response):
#encoded_data = json.dumps(zotero_response.content)
#encoded_data = json.dumps(data.content)
#parsed_data = json.loads(encoded_data)
parsed_data = json.loads(zotero_response.content.decode('utf-8'))
return parsed_data


data = api_get(userID, collectionID, apiKey)
data_json = parse_zotero(data)



# accessing the Extra field:
# data_json[2]['data']['extra']


# Text to work with as an example of what this should look like.
text_extra = 'Notes about a citation. Some other stuff that is too important to erase. <geojson>{"type": "Point", "coordinates": [-121.943352010208, 36.5996662302192]}</geojson> Plus someone put something after this because they could.'

# STUFF THAT DIDN'T WORK BUT MIGHT HELP LATER:
# split the extra string
#text_extra_parts = text_extra.split("<geojson>")
#remove the end tag
#test_extra_parts = text_extra[1].replace("/geojson>", "")
#test_extra_parts = text_extra[1].strip("/geojson>")
#json_string = text_extra_parts[1]

# Get the geojson string from the extra field
geojson_str = text_extra[text_extra.find("<geojson>")+9:text_extra.find("</geojson>")]


# How do we update the string and then insert it into the extra string?

#get the stuff in front of the <geojson> and after </geojson>, then concatenate them with the new spatial string?
before_geojson = text_extra[0 : text_extra.find("<geojson>")]
after_geojson = text_extra[text_extra.find("</geojson>")+10:]

# Put it back together
# -----> saveZotero() function's extraString gets what we have here in geojson_str

new_extra = before_geojson + "<geojson>" + geojson_str + "</geojson>" + after_geojson








2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Literature Mapper QGIS Plugin

Literature Mapper is a plugin for QGIS that connects QGIS to a Zotero citation database, allowing the user to add georeferences to individual citations. The documentation is [here](http://micheletobias.github.io/maps/LiteratureMapper.html). Please don't hesitate to file issue. Additionally, the current developers would very much appreciate help implementing the enhancements and bug fixes listed in the issues list.
Literature Mapper is a plugin for QGIS that connects QGIS to a Zotero citation database, allowing the user to add georeferences to individual citations. The documentation is [here](http://micheletobias.github.io/maps/LiteratureMapper.html). Please don't hesitate to file an issue. Additionally, the current developers would very much appreciate help implementing the enhancements and bug fixes listed in the issues list.
40 changes: 36 additions & 4 deletions literature_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,23 @@ def saveZotero(self):
for row in rows:
#get the itemID(zotero key) and geometry cells from the table - itemAt(x,y)
itemKey = self.dlgTable.tableWidget_Zotero.item(row, 0).text()
extraString = self.dlgTable.tableWidget_Zotero.item(row, 4).text()
QgsMessageLog.logMessage("row: %s itemKey: %s extraString: %s" % (row, itemKey, extraString), 'LiteratureMapper', Qgis.Info)

request_url = 'https://api.zotero.org/users/%s/items/%s' % (self.userID, itemKey)
item_request = requests.get(request_url)
QgsMessageLog.logMessage("Item Request Response: %s" % item_request.status_code, 'LiteratureMapper', Qgis.Info)
item_json = json.load(urllib.request.urlopen(request_url))

#Put the extra string back together with the new coordinates
tablegeom = self.dlgTable.tableWidget_Zotero.item(row, 4).text()
extraZotero = item_json['data']['extra']
before_geojson = extraZotero[0 : extraZotero.find("<geojson>")]
after_geojson = extraZotero[extraZotero.find("</geojson>")+10:]
extraString = '%s<geojson>%s</geojson>%s' % (before_geojson, tablegeom, after_geojson) #build the new extraString here

QgsMessageLog.logMessage("row: %s itemKey: %s extraString: %s" % (row, itemKey, extraString), 'LiteratureMapper', Qgis.Info)


####### saving Extra field
item_json['data']['extra'] = extraString
item_json=json.dumps(item_json)
put_request = requests.put(request_url, data=item_json, headers={'Authorization': 'Bearer %s' % (self.apiKey), 'Content-Type': 'application/json'})
Expand Down Expand Up @@ -492,12 +502,33 @@ def data_get(userID, collectionID, apiKey):
self.dlgTable.tableWidget_Zotero.setItem(i, 3, title)
title_str = record['data']['title']

########## Putting the Extra field into the table
# pre-populate the table with anything already in the Extra field
if 'extra' in record['data']:
extra = QTableWidgetItem(record['data']['extra'])
#extra = QTableWidgetItem(record['data']['extra'])

extra_zotero = record['data']['extra']

#example of how to pull out the geojson string from a messy Extra field:
#geojson_str = text_extra[text_extra.find("<geojson>")+9:text_extra.find("</geojson>")]

extra_str = record['data']['extra']

if '<geojson>' in extra_zotero:
extra_str = extra_zotero[extra_zotero.find('<geojson>')+9:extra_zotero.find('</geojson>')]
#before_geojson = extra_zotero[0 : extra_zotero.find("<geojson>")]
#after_geojson = extra_zotero[extra_zotero.find("</geojson>")+10:]
elif '{"type":' in extra_zotero:
extra_str = extra_zotero[extra_zotero.find('{'):extra_zotero.find('}')]
else: extra_str = ''

extra = QTableWidgetItem(extra_str)


#prints the extra string to the log
QgsMessageLog.logMessage("Extra String: %s" % extra_str, 'LiteratureMapper', Qgis.Info)



check_point = '"type": "Point"'
check_multipoint = '"type": "Multip'
if extra_str[1:16] == check_point:
Expand Down Expand Up @@ -539,6 +570,7 @@ def data_get(userID, collectionID, apiKey):
self.multipointLayer.updateExtents()
else:
x = ''
QgsMessageLog.logMessage("Not a point or multipoint", 'LiteratureMapper', Qgis.Info)
else:
extra = QTableWidgetItem("")
self.dlgTable.tableWidget_Zotero.setItem(i, 4, extra)
Expand Down
5 changes: 4 additions & 1 deletion metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
name=Literature Mapper
qgisMinimumVersion=3.0
description=Add spatial locations to the citations in your Zotero database.
version=0.3.1
version=0.3.2
author=Michele Tobias & Alex Mandel
[email protected]
about=Literature Mapper inserts geoJSON geometry strings into an online Zotero database to facilitate the mapping of study site locations. The locations stored in the database are displayed on the map canvas in virtual point and multipoint shapefiles on start up.
Expand All @@ -21,6 +21,9 @@ about=Literature Mapper inserts geoJSON geometry strings into an online Zotero d

# Uncomment the following line and add your changelog:
changelog=
Version 0.3.2
- Preserves text notes stored in the extra fields
- Implements an html-tyle tag wrapper around the geojson string for ease of identification in the extra field
Version 0.3.1
- Fixes for large libraries, date fields, and non citations (notes, attachments, etc)
Version 0.3
Expand Down

0 comments on commit 8264551

Please sign in to comment.