Converting to string not producing required value - python

I have some code that is using the 'gdshortener' module to produce a shortened version of my source URLS:
import simplejson
import httplib2
import twitter
import gdshortener
from random import randint
print("Python will now attempt to submit tweets to twitter...")
try:
api = twitter.Api(consumer_key='',
consumer_secret='',
access_token_key='',
access_token_secret='')
b = 0
for a in range(0, 1): #only range 0-1 for this question, actually 1-21
b = b + 1
a = randint(1,60000000)
randint
print ("a = ", a)
aa = str(a)
s1 = gdshortener.ISGDShortener()
print s1.shorten(url = 'http://audiotechracy.blogspot.co.uk/2014/03/reviewing-synapse-antidote-rack.html', custom_url = aa)
ss1 = str(s1)
status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + ss1 + " #propellerhead #synapse")
print("Tweets submitted successfully!")
except Exception,e:
print str(e)
print("Twitter submissions have failed!!!")
I am using a random number generator to produce six digit numbers which are then fed to the custom_url parameter of this module. That works fine and I get a series of pseudo-random numbers. However when I try and concatenate my tweet string, my dynamic short URL and some hashtags I get an error that I cannot concatenate string and integer values.
So I have then created the variable 'ss1' which is the string of 's1', however this now produces a tweet like this:
The new Synapse Antidote Rack Extension:<gdshortener.gdshortener.ISGDShortener object at 0x000000000542AA20> #propellerhead #synapse
How can i get it so that the tweet produced is:
The new Synapse Antidote Rack Extension: http://is.gd/58077181 #propellerhead #synapse
Thanks

Checked the module and discovered that it returns a tuple. See the following to extract the proper URL.
Code:
import gdshortener
s1 = gdshortener.ISGDShortener()
x1 = s1.shorten(url='http://audiotechracy.blogspot.co.uk/2014/03/reviewing-synapse-antidote-rack.html')[0]
print x1
Result:
http://is.gd/KKxmFd
[Finished in 0.8s]
Notice how I added a [0] at the end of the shorten. This is because shorten returns a tuple, which we can index similar to a list.
Hope this helps.

Related

Permutation List with Variable Dependencies- UnboundLocalError

I was trying to break down the code to the simplest form before adding more variables and such. I'm stuck.
I wanted it so when I use intertools the first response is the permutations of tricks and the second response is dependent on the trick's landings() and is a permutation of the trick's corresponding landing. I want to add additional variables that further branch off from landings() and so on.
The simplest form should print a list that looks like:
Backflip Complete
Backflip Hyper
180 Round Complete
180 Round Mega
Gumbi Complete
My Code:
from re import I
import pandas as pd
import numpy as np
import itertools
from io import StringIO
backflip = "Backflip"
one80round = "180 Round"
gumbi = "Gumbi"
tricks = [backflip,one80round,gumbi]
complete = "Complete"
hyper = "Hyper"
mega = "Mega"
backflip_landing = [complete,hyper]
one80round_landing = [complete,mega]
gumbi_landing = [complete]
def landings(tricks):
if tricks == backflip:
landing = backflip_landing
elif tricks == one80round:
landing = one80round_landing
elif tricks == gumbi:
landing = gumbi_landing
return landing
for trik, land in itertools.product(tricks,landings(tricks)):
trick_and_landing = (trik, land)
result = (' '.join(trick_and_landing))
tal = StringIO(result)
tl = (pd.DataFrame((tal)))
print(tl)
I get the error:
UnboundLocalError: local variable 'landing' referenced before assignment
Add a landing = "" after def landings(tricks): to get rid of the error.
But the if checks in your function are wrong. You check if tricks, which is a list, is equal to backflip, etc. which are all strings. So thats why none of the ifs are true and landing got no value assigned.
That question was also about permutation in python. Maybe it helps.

How to avoid these errors while fetching api in python

import requests
import json
response = requests.get('http://dataservice.accuweather.com/currentconditions/v1/2807435?apikey=secret')
print(response)
x = response.json()
y = json.dumps(x)
z = json.loads(y)
a = z['WeatherText']
print(a)
yt = input("Press Enter to close")
This code is for retrieving weather data from accuweather. When I run the file, i get an error:
TypeError: list indices must be integers or slices, not str
A sample image of the initial json file is:
This image is the output
Please help me with the error
You only need x, but you also need to recognise that it is a list:
a = x[0]['WeatherText']
Variable z is a list of dict. Therefore you need to go to the first position by:
a = z[0]['WeatherText']

String Manipulation for Json webscraping

I am trying to scrape a website and have all the data needed in very long matrices which were obtained through requests and json imports.
I am having issues getting any output.
Is it because of the merge of two strings in requests.get()?
Here is the part with the problem, all things used were declared at the start of the code.
balance=[]
for q in range(len(DepositMatrix)):
address= requests.get('https://ethplorer.io/service/service.php?data=' + str(DepositMatrix[q][0]))
data4 = address.json()
TokenBalances = data4['balances'] #returns a dictionary
balance.append(TokenBalances)
print(balance)
Example of DepositMatrix - list of lists with 4 elements, [[string , float, int, int]]
[['0x2b5634c42055806a59e9107ed44d43c426e58258', 488040277.1535826, 660, 7103],
['0x05ee546c1a62f90d7acbffd6d846c9c54c7cf94c', 376515313.83254075, 2069, 12705]]
I think the error is in this part:
requests.get('https://ethplorer.io/service/service.php?data=' + str(DepositMatrix[q][0]))
This change doesnt help either:
requests.get('https://ethplorer.io/service/service.php?data=' + DepositMatrix[q][0])
Like I said in my comment, I tried your code and it worked for me. But I wanted to highlight some things that could help your code be clearer:
import requests
import pprint
DepositMatrix = [['0x2b5634c42055806a59e9107ed44d43c426e58258', 488040277.1535826, 660, 7103],
['0x05ee546c1a62f90d7acbffd6d846c9c54c7cf94c', 376515313.83254075, 2069, 12705]]
balance=[]
for deposit in DepositMatrix:
address = requests.get('https://ethplorer.io/service/service.php?data=' + deposit[0])
data4 = address.json()
TokenBalances = data4['balances'] #returns a dictionary
balance.append(TokenBalances)
pprint.pprint(balance)
For your loop, instead of creating a range of the length of your list (q) and then using this q to get the information back from your list, it's simpler to get each element directly (for deposit in DepositMatrix:)
I've used the pprint module to ease the visualization of your data.

Python JSON get request for multiple websites

I'm trying to make a json get request on multiple urls.
(I'm new to programming as you will see below.)
My goal is to run this python script with multiple links. The only thing needs to be changed is the "b" part of the link, so it can run multiple searches with different numbers (strings), preferably using a list of numbers and exporting all the results to a csv list.
My question is: How should I change the code so I can use a list of numbers for "b" and get all the requests? (like a loop)
Please find below my code and thank you for your time.
import requests
import json
a = 'https://www.g2a.com/lucene/search/filter?jsoncallback=jQuery111002521088376353553_1491736907010&skip=28837%2C28838%2C28847%2C28849%2C28852%2C28856%2C28857%2C28858%2C28859%2C28860%2C28861%2C28862%2C28863%2C28867%2C28868%2C28869%2C29472%2C29473%2C29474%2C29475%2C29476%2C29482%2C29486%2C33104&minPrice=0.00&maxPrice=640.00&cn=&kr=&stock=all&event=&platform=0&search=&genre=0&cat=0&sortOrder=popularity+desc&start=0&rows=12&steam_app_id='
b = '515220'
c = '&steam_category=&steam_prod_type=&includeOutOfStock=false&includeFreeGames=false&_=1491736907012'
d = a + b + c
r = requests.get(d)
json_object = json.loads('{"data":%s}}' % (response.content.decode("utf-8").replace("jQuery111002521088376353553_1491736907010(", "")[:-2].replace("\'", "")))
for game in json_object["data"]["docs"]:
print ("Name: %s (Price: %s)" % (game["name"], game["minPrice"]))
You can loop over the list of b values like below,
l = [1,2,3,4]
for b in l:
d = a + b + c
r = requests.get(d)
json_object = json.loads('{"data":%s}}' % (response.content.decode("utf-8").replace("jQuery111002521088376353553_1491736907010(", "")[:-2].replace("\'", "")))
for game in json_object["data"]["docs"]:
print ("Name: %s (Price: %s)" % (game["name"], game["minPrice"]))

JSON.LOADS is picking only 2 resultset

I am trying to use JSON to search through googlemapapi. So, I give location "Plymouth" - in googlemapapi it is showing 6 resultset but when I try to parse in Json, I am getting length of only 2. I tried with multiple cities too, but all I am getting is resultset of 2 rather.
What is wrong below?
import urllib.request as UR
import urllib.parse as URP
import json
url = "http://maps.googleapis.com/maps/api/geocode/json?address=Plymouth&sensor=false"
uh = UR.urlopen(url)
data = uh.read()
count = 0
js1 = json.loads(data.decode('utf-8') )
print ("Length: ", len(js1))
for result in js1:
location = js1["results"][count]["formatted_address"]
lat = js1["results"][count]["geometry"]["location"]["lat"]
lng = js1["results"][count]["geometry"]["location"]["lng"]
count = count + 1
print ('lat',lat,'lng',lng)
print (location)
Simply replace for result in js1: with for result in js1['results']:
By the way, as posted in a comment in the question, no need to use a counter. You can rewrite your for loop as:
for result in js1['results']:
location = result["formatted_address"]
lat = result["geometry"]["location"]["lat"]
lng = result["geometry"]["location"]["lng"]
print('lat',lat,'lng',lng)
print(location)
If you look at the json that comes in, you'll see that its a single dict with two items ("results" and "status"). Add print('result:', result) to the top of your for loop and it will print result: status and result: results because all you are iterating the the keys of that outer dict. That's a general debugging trick in python... if you aren't getting the stuff you want, put in a print statement to see what you got.
The results (not surprisingly) and in a list under js1["results"]. In your for loop, you ignore the variable you are iterating and go back to the original js1 for its data. This is unnecessary and in your case, it hid the error. Had you tried to reference cities off of result you would gotten an error and it may have been easier to see that result was "status", not the array you were after.
Now a few tweaks fix the problem
import urllib.request as UR
import urllib.parse as URP
import json
url = "http://maps.googleapis.com/maps/api/geocode/json?address=Plymouth&sensor=false"
uh = UR.urlopen(url)
data = uh.read()
count = 0
js1 = json.loads(data.decode('utf-8') )
print ("Length: ", len(js1))
for result in js1["results"]:
location = result["formatted_address"]
lat = result["geometry"]["location"]["lat"]
lng = result["geometry"]["location"]["lng"]
count = count + 1
print ('lat',lat,'lng',lng)
print (location)

Categories