I am pretty new to Python and QGIS but I am trying to create an algorithm for a processing plugin that will import a CSV of Tweet IDs. It then hydrates them using the Twarc Python library to get a CSV with extracted metadata from Twitter, and then create points from their coordinates in the CSV. I've gotten pretty far but now reached a wall.
When I run my processing plugin script QGIS completely crashes and doesn't give any error messages. The last bug I had was an error to do with QFileDialog so I suspect it is to do with that, but removing it still causes QGIS to crash.
Unfortunately, it's also part of a major project assignment for university due in two days, and the teaching staff are not overly helpful.
Do you have any advice specifically with QGIS crashing due to the script, or on the code in general?
I am using mac OS with QGIS 3.20 Odense.
My script:
#importing the modules I think I need
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import *
from twarc.client2 import Twarc2, expansions
import json
from twarc_csv import CSVConverter
from qgis.core import QgsCoordinateReferenceSystem
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication, QFileDialog
class HydrateGeoTweetsAlgorithm(QgsProcessingAlgorithm):
#processing boilerplate
OUTPUT = 'OUTPUT'
INPUT = 'INPUT'
def initAlgorithm(self, config):
#importing csv of tweet IDs
self.addParameter(
QgsProcessingParameterFile(
self.INPUT,
self.tr('Input file')
)
)
#output point layer
self.addParameter(
QgsProcessingParameterFeatureSink(
self.OUTPUT,
self.tr('Output layer')
)
)
def processAlgorithm(self, parameters, context, feedback):
#asking user to specify a file to input
qfd = QFileDialog()
title = 'Open File'
path = "/Users/"
tweet_ids = QFileDialog.getOpenFileName(qfd, title, path)
#token you need to request from twitter API
t = Twarc2(bearer_token="")
#iterating over csv using twarc to hydrate each tweet ID
lookup = t.tweet_lookup(tweet_ids=tweet_ids)
for page in lookup:
result = expansions.flatten(page)
with open("results.jsonl", "w+") as f:
f.write(json.dumps(page) + "\n")
#converting jsonl to csv to get coordinates
with open("results.jsonl", "r") as infile:
with open("output.csv", "w") as outfile:
convert = CSVConverter(infile, outfile, json_encode_all=False, json_encode_lists=True, json_encode_text=False, inline_referenced_tweets=True, allow_duplicates=False, batch_size=1000)
convert.process()
#plotting points based on coordinates
for tweet in convert:
if tweet["geo.coordinates.coordinates"]:
processing.run("native:createpointslayerfromtable",
{'INPUT':'output.csv',
'XFIELD':'cooridnates[1]',
'YFIELD':'cooridnates[0]',
'ZFIELD':'',
'MFIELD':'',
'TARGET_CRS':QgsCoordinateReferenceSystem(''),
'OUTPUT':'\\Users\\tweet_points.shp'})
return QgsMapLayerRegistry.instance().addMapLayers([tweet_points.shp])
#other processing boilerplate
def name(self):
return 'Hydrate and plot tweets'
def displayName(self):
return self.tr(self.name())
def group(self):
return self.tr(self.groupId())
def groupId(self):
return ''
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return HydrateGeoTweetsAlgorithm()
QtResources
and then try again. Such as Icon and etc...