Here is my code so far:
import flickrapi
import osmapi
import geopy
from geopy.geocoders import Nominatim
import overpy
import requests
import xmltodict
from time import sleep
api_key = "xxxxxxxxxxxxxxxxxxxx"
secret_api_key = "xxxxxxx"
flickr = flickrapi.FlickrAPI(api_key, secret_api_key)
def obtainImages3():
group_list = flickr.groups.search (api_key=api_key, text = 'Paris', per_page = 10)
for group in group_list[0]:
group_images = flickr.groups.pools.getPhotos (api_key=api_key, group_id = group.attrib['nsid'], extras = 'geo, tags, url_s')
for image in group_images[0]:
url = str(image.attrib['url_s'])
tags = (image.attrib['tags']).encode('utf-8')
#ONLY RUN THIS CODE IF IMAGE HAS GEO INFO
photo_location = flickr.photos_geo_getLocation(photo_id=image.attrib['id'])
lat = float(photo_location[0][0].attrib['latitude'])
lon = float(photo_location[0][0].attrib['longitude'])
geolocator = Nominatim()
location = geolocator.reverse("{}, {}".format(lat, lon))
dict = location.raw
osmid = dict.get('osm_id', 'default_value_if_null_here')
osmtype = dict.get('osm_type', 'default_value_if_null_here')
osmaddress = dict.get('display_name', 'default_value_if_null_here')
sleep(1)
if(osmtype == 'node'):
node_info = requests.get("http://api.openstreetmap.org/api/0.6/node/"+ osmid)
#print node_info.content
d = xmltodict.parse(node_info.content)
amenity_tags = [tag for tag in d['osm']['node']['tag'] if tag['#k'] == 'amenity']
amenity_name = [tag for tag in d['osm']['node']['tag'] if tag['#k'] == 'name']
if len(amenity_tags) != 0:
amenity_type = amenity_tags[0]['#v']
amenity_type_name = amenity_name[0]['#v']
print amenity_type
print amenity_type_name
print url
obtainImages3()
I want to run the code following the comment if and only if geo-info is available on the image obtained. In my group_images api call, I am asking for geo info to be returned if it it present.
I have tried for example if image.attrib['geo'] != None, and have tried a try/ exception block but all are returning errors, especially key errors.
Can anyone suggest an easy way to figure out if the image has geo info, so that the code below the comment will only run if it is available?
Related
I am using tweepy and would like to get tweets containing a particular word in a given time frame, posted by a certain user(BBC, CNN...) - I created this but it seems not working in the sense that it does not reflect given conditions. Do I miss something?
import tweepy
import webbrowser
import time
import pandas as pd
# to get keys: https://developer.twitter.com/en/portal/dashboard
consumer_key = 'YOUR CREDENTIALS'
consumer_secret = 'YOUR CREDENTIALS'
callback_uri = 'oob' # https://cfe.sh/twitter/callback
auth = tweepy.OAuthHandler(consumer_key, consumer_secret, callback_uri)
redirect_url = auth.get_authorization_url()
print(redirect_url)
webbrowser.open(redirect_url) # --- GET pin:
user_pin_input = input("Put the pin:__________"); # example 2178932
auth.get_access_token(user_pin_input)
api = tweepy.API(auth)
id = 'BBCWorld'
q = "Pandemic"
since="2022-01-01",
until="2022-03-28"
# search_tweets or user_timeline?
def extract_timeline_as_df(id):
number_of_tweets = 200
name = []
author = []
tweet = []
date = []
like = []
retweet = []
for i in tweepy.Cursor(api.user_timeline, id=id, q='Pandemic', since="2022-01-01", until="2022-03-28", tweet_mode='extended').items(number_of_tweets):
name.append(i.user.screen_name)
author.append(i.author.screen_name)
tweet.append(i.full_text)
date.append(i.created_at)
like.append(i.favorite_count)
retweet.append(i.retweet_count)
df = pd.DataFrame({'name':name, 'author':name, 'tweet':tweet, 'date':date, 'like':like, 'retweet':retweet})
return df
BBC = extract_timeline_as_df('BBCWorld'); BBC
CNN = extract_timeline_as_df('cnnbrk'); CNN
NT = extract_timeline_as_df('nytimes'); NT
data = pd.concat([BBC, CNN, NT]); data
I'm trying to use Openweathermap and I'm using their docs to get the calls.
I don't know why but I get always this error:
{'cod': '400', 'message': '{limit} is not a number'}
This is my code:
import requests
import json
API_KEY = "49edcdeb737a08b5371c42f85fb4ce3d"
weather_url = "http://api.openweathermap.org/geo/1.0/direct?q={city_name},{country_code}&limit={limit}&appid="
final_url = weather_url + API_KEY
limit = "1"
city_name = "Brindisi"
country_code = "3166"
weather_data = requests.get(final_url).json()
print(weather_data)
You are not replacing the query parameters with actual values. q={city_name},{country_code}&limit={limit} is hard coded in the url and is invalid.
You can use the F-string in python to replace placeholder value with actual value.
limit = "1"
city_name = "Brindisi"
country_code = "3166"
weather_url = f"http://api.openweathermap.org/geo/1.0/direct?q={city_name},{country_code}&limit={limit}&appid="
final_url = weather_url + API_KEY
I am using python 3.6 and zeep 3.4.0
Zeep returns raw data and i cannot convert it to xml/json/pandas object.
I've tried to use bs4 to get table from the text1, no luck.
Serialize text1 to get json, no luck too.
from zeep import Client, Settings
settings = Settings(xml_huge_tree=True)
client = Client('http://www.cbr.ru/secinfo/secinfo.asmx?WSDL', settings=settings)
s = '2019-06-21T00:00:00'
with client.settings(raw_response=True):
result = (client.service.IDRepoRUBXML(s))
#print(dir(result))
text1 = (result.text)
print(text1)
#
#data = literal_eval(text1.decode('utf8'),)
def escape(t):
"""HTML-escape the text in `t`."""
return (t.replace("&","&").replace("<","<" ).replace( ">",">").replace("'","'").replace(""",'"'))
m = escape(text1)
print(m)
I need to retrieve readable xml or json/pandas table from zeep.
If you're just trying to get a python dict type out of serialize_object helper, you can specify the type you want.
from zeep import helpers
_json = helpers.serialize_object(zeep_object, dict)
found a way myself :)
from zeep import Client, Settings
from bs4 import BeautifulSoup
settings = Settings(xml_huge_tree=True)
client = Client('http://www.cbr.ru/secinfo/secinfo.asmx?WSDL', settings=settings)
s = '2019-06-21T00:00:00'
with client.settings(raw_response=True):
result = (client.service.IDRepoRUBXML(s))
#print(dir(result))
text1 = (result.text)
def escape(t):
t = t.replace("&","&")
t1 = t.replace("<","<" )
t2 = t1.replace( ">",">")
t3 = t2.replace("'","'")
t4 = t3.replace(""",'"')
return t4
m = escape(text1)
#j = parser.feed(m)
if(m is not None):
soup = BeautifulSoup(m,'lxml')
else:
print("")
items = soup.find_all('item')
for item in items:
discounts = item.find_all('dt')
beg_6d = discounts[0]['beg']
min_6d = discounts[0]['min']
max_6d = discounts[0]['max']
beg7_14 = discounts[1]['beg']
min7_14 = discounts[1]['min']
max7_14 = discounts[1]['max']
for attr in item.attrs:
dateredemption = item.attrs['dateredemption']
em = item.attrs['em']
isin = item.attrs['isin']
price = item.attrs['price_fnd']
regn = item.attrs['regn']
print(isin,regn,em,dateredemption,price,beg_6d,min_6d,max_6d, beg7_14,min7_14,max7_14)
You can convert to XML using Minidom.
from zeep import Client
import xml.dom.minidom
client = Client('http://www.dneonline.com/calculator.asmx?wsdl')
def Add(num1, num2):
with client.settings(raw_response=True):
return xml.dom.minidom.parseString(client.service.Add(num1, num2).content).toprettyxml(indent=" ",encoding='utf8')
print (Add(2,5))
This is the code for the weather in a smart mirror I creating. I am using someone else's code and get these two errors when running the app (the app works it just doesn't show the weather):
ValueError: No JSON object could be decoded
Error: No JSON object could be decoded. Cannot get weather
def get_weather(self):
try:
if latitude is None and longitude is None:
# get location
location_req_url = "http://freegeoip.net/json/%s" % self.get_ip()
r = requests.get(location_req_url)
location_obj = json.loads(r.text)
lat = location_obj['latitude']
lon = location_obj['longitude']
location2 = "%s, %s" % (location_obj['city'], location_obj['region_code'])
# get weather
weather_req_url = "https://api.darksky.net/forecast/%s/%s,%s?lang=%s&units=%s" % (weather_api_token, lat,lon,weather_lang,weather_unit)
else:
location2 = ""
# get weather
weather_req_url = "https://api.darksky.net/forecast/%s/%s,%s?lang=%s&units=%s" % (weather_api_token, latitude, longitude, weather_lang, weather_unit)
r = requests.get(weather_req_url)
weather_obj = json.loads(r.text)
degree_sign= u'\N{DEGREE SIGN}'
temperature2 = "%s%s" % (str(int(weather_obj['currently']['temperature'])), degree_sign)
currently2 = weather_obj['currently']['summary']
forecast2 = weather_obj["hourly"]["summary"]
icon_id = weather_obj['currently']['icon']
icon2 = None
if icon_id in icon_lookup:
icon2 = icon_lookup[icon_id]
if icon2 is not None:
if self.icon != icon2:
self.icon = icon2
image = Image.open(icon2)
image = image.resize((100, 100), Image.ANTIALIAS)
image = image.convert('RGB')
photo = ImageTk.PhotoImage(image)
self.iconLbl.config(image=photo)
self.iconLbl.image = photo
else:
# remove image
self.iconLbl.config(image='')
if self.currently != currently2:
self.currently = currently2
self.currentlyLbl.config(text=currently2)
if self.forecast != forecast2:
self.forecast = forecast2
self.forecastLbl.config(text=forecast2)
if self.temperature != temperature2:
self.temperature = temperature2
self.temperatureLbl.config(text=temperature2)
if self.location != location2:
if location2 == ", ":
self.location = "Cannot Pinpoint Location"
self.locationLbl.config(text="Cannot Pinpoint Location")
else:
self.location = location2
self.locationLbl.config(text=location2)
except Exception as e:
traceback.print_exc()
print( "Error: %s. Cannot get weather." % e)
self.after(600000, self.get_weather)
#staticmethod
def convert_kelvin_to_fahrenheit(kelvin_temp):
return 1.8 * (kelvin_temp - 273) + 32
This is the code for the weather widget in the app. The modules im using are:
from tkinter import *
import locale
import threading
import time
import requests
import json
import traceback
import feedparser
from PIL import Image, ImageTk
from contextlib import contextmanager
so those are the modules that are being used.
Ok so after so long trying I found out that even though the api token is printing 404 meaning its forbidden, it doesnt change the output the part of the code that was wrong was:
weather_req_url = "https://api.darksky.net/forecast/%s/%s,%s?lang=%s&units=%s" % (weather_api_token, latitude, longitude, weather_lang, weather_unit)
where it was supposed to be:
weather_req_url = "%s/%s,%s?lang=%s&units=%s" % (weather_api_token, latitude, longitude, weather_lang, weather_unit)
this is because my api key is supposed to be a link so is "(https)://api.darksky.net/forecast/"
so I took it out of the weather_req_url
N.B. https in '()' so it does not form a clickable link
looking at the example provided by wordpresslib, its very straight forward on how to upload images to the media library. However, the attachment of images looks like it was never finished. Has anyone successfully attached the images?
#!/usr/bin/env python
"""
Small example script that publish post with JPEG image
"""
# import library
import wordpresslib
print 'Example of posting.'
print
url = raw_input('Wordpress URL (xmlrpc.php will be added):')
user = raw_input('Username:')
password = raw_input('Password:')
# prepare client object
wp = wordpresslib.WordPressClient(url+"xmlrpc.php", user, password)
# select blog id
wp.selectBlog(0)
# upload image for post
# imageSrc = wp.newMediaObject('python.jpg')
# FIXME if imageSrc:
# create post object
post = wordpresslib.WordPressPost()
post.title = 'Test post'
post.description = '''
Python is the best programming language in the earth !
No image BROKEN FIXME <img src="" />
'''
#post.categories = (wp.getCategoryIdFromName('Python'),)
# Add tags
post.tags = ["python", "snake"]
# do not publish post
idNewPost = wp.newPost(post, False)
print
print 'Posting successfull! (Post has not been published though)'
WordPressPost class:
class WordPressPost:
"""Represents post item
"""
def __init__(self):
self.id = 0
self.title = ''
self.date = None
self.permaLink = ''
self.description = ''
self.textMore = ''
self.excerpt = ''
self.link = ''
self.categories = []
self.user = ''
self.allowPings = False
self.allowComments = False
self.tags = []
self.customFields = []
def addCustomField(self, key, value):
kv = {'key':key, 'value':value}
self.customFields.append(kv)
Wordpress saves images as website.com/wp-content/uploads/YEAR/MONTH/FILENAME
Adding a simple image tag with the above format in to post.description display the image on the post.
where YEAR is the current year with a 4 digit format (ex. 2015)
and MONTH is the current month with a leading zero (ex. 01,02,... 12)
and FILENAME is the file name submitted via imageSrc = wp.newMediaObject('python.jpg')
Example file name: website.com/wp-content/uploads/2015/06/image.jpg
Here is how I posted my image:
import time
import wordpresslib
import Image
from datetime import datetime
time = datetime.now()
h = str(time.strftime('%H'))
m = str(time.strftime('%M'))
s = str(time.strftime('%S'))
mo = str(time.strftime('%m'))
yr = str(time.strftime('%Y'))
url = 'WORDPRESSURL.xmlrpc.php'
wp = wordpresslib.WordPressClient(url,'USERNAME','PASSWORD')
wp.selectBlog(0)
imageSrc = wp.newMediaObject('testimage'+h+m+s'.jpg') #Used this format so that if i post images with the same name its unlikely they will override eachother
img = 'http://WORDPRESSURL/wp-content/uploads/'+yr+'/'+mo+'/testimage'+h+m+s+'.jpg'
post=wordpresslib.WordPressPost()
post.title='title'
post.description='<img src="'+img+'"/>'
idPost=wp.newPost(post,true)