Commit 9fe2ec60 authored by Luis García's avatar Luis García
Browse files

Merge branch 'master' of gitlab.up.technology:luis.garcia/collabmap

parents bd02fa05 61af8a48
......@@ -26,9 +26,8 @@ def TwitterMining(consultation):
for question in questions:
#If is a collabmap question minin its data
if question.question.type == 'cm':
query = "%23"+consultation.slug
query = '#{slug1} #{slug2} #{slug2}'.format(slug1=consultation.phase.slug, slug2=consultation.slug, slug3=question.question.spatial_data.all()[0].slug)
results = api.search(q=query, result_type='recent')
query = '#{slug1} #{slug2} #{slug3}'.format(slug1=consultation.phase.slug, slug2=consultation.slug, slug3=question.question.spatial_data.all()[0].slug)
results = api.search(q=query)
for tweet in results:
location = False
if tweet.coordinates:
......@@ -38,5 +37,27 @@ def TwitterMining(consultation):
box = tweet.place.bounding_box
location = Polygon.from_bbox((box.origin()[0], box.origin()[1], box.corner()[0], box.corner()[1])).centroid
if location:
input = SocialInput(description='')
input.save()
\ No newline at end of file
description = tweet.text
descriptionTB = TextBlob(tweet.text)
#Try to translate
try:
descriptionTB = descriptionTB.translate(to='en')
except:
pass
polarity = descriptionTB.sentiment.polarity
media_url = []
#Get the url for the images
if 'media' in tweet.entities.keys():
for media in tweet.entities['media']:
if media['type'] == 'photo':
media_url.append(media['media_url'])
if len(media_url):
#Download the image
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
r = requests.get(media_url[0])
file_dir = BASE_DIR + "/media/social_media_photos/twitter/" + datetime.now().strftime("%b-%d-%Y-%H-%M-%S.%f") + ".jpg"
with open(file_dir, "wb+") as image:
image.write(r.content)
SocialInput.objects.create(description=description, username=tweet.user.screen_name, survey=survey, network='twitter', polarity=polarity, created=tweet.created_at, geometry=location, question=question.question, photo=File(image))
else:
SocialInput.objects.create(description=description, username=tweet.user.screen_name, survey=survey, network='twitter', polarity=polarity, created=tweet.created_at, geometry=location, question=question.question)
\ No newline at end of file
......@@ -22,4 +22,6 @@ instaloader==4.2.9
drf-yasg==1.17.0
drf-extensions==0.5.0
django-cors-headers==3.1.1
django-allauth==0.41.0
\ No newline at end of file
django-allauth==0.41.0
django-rest-auth==0.9.5
django-extra-fields==2.0.3
\ No newline at end of file
......@@ -27,7 +27,7 @@ class SpatialQuestionSerializer(ModelSerializer):
class Meta:
model = SpatialQuestion
fields = ('id', 'center', 'zoom', 'study_area', 'map', 'question')
fields = ('id', 'slug', 'center', 'zoom', 'study_area', 'map', 'question')
class QuestionSerializer(ModelSerializer):
options = QuestionOptionSerializer_temp(many=True)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment