my code is in python3 and I used it before to live stream tweets in English. However the same code when searching for an Arabic query, it returns all tweets in symbols and random characters.here is a screenshot, and the code. (ps: I am a beginner in coding) (thank you!) here is my code:
import twitter,json,csv
CONSUMER_KEY = '<consumer key>'
CONSUMER_SECRET = '<consumer secret>'
OAUTH_TOKEN = '<oauth token>'
OAUTH_TOKEN_SECRET = '<oauth token secret>'
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
# setup a file to write to
csvfile = open('tweets_extended.csv', 'w')
csvwriter = csv.writer(csvfile, delimiter='|')
# heres a function that takes out characters that can break
# our import into Excel and replaces them with spaces
# it also does the unicode bit
def getVal(val):
clean = ""
if val:
val = val.replace('|', ' ')
val = val.replace('\n', ' ')
val = val.replace('\r', ' ')
clean = val.encode('utf-8')
return clean
q = "سلمان" # Comma-separated list of terms can go here
print ('Filtering the public timeline for track="%s"' % (q,))
twitter_stream = twitter.TwitterStream(auth=twitter_api.auth)
stream = twitter_stream.statuses.filter(track=q)
for tweet in stream:
try:
if tweet['truncated']:
tweet_text = tweet['extended_tweet']['full_text']
else:
tweet_text = tweet['text']
# write the values to file
csvwriter.writerow([
tweet['created_at'],
getVal(tweet['user']['screen_name']),
getVal(tweet_text),
getVal(tweet['user']['location']),
tweet['user']['statuses_count'],
tweet['user']['followers_count'],
tweet['user']['lang'],
tweet['user']['id'],
])
# print something to the screen, mostly so we can see what is going on...
print (tweet['user']['screen_name'].encode('utf-8'), tweet['text'].encode('utf-8'))
except Exception as err:
print (err)
pass
I am trying to get unique values from the all lists titled "DataClasses" from an API request. With my code it only prints the first list titled "DataClasses" but there are at least 40 other lists called that.
I am confused on how to get all of the lists titled "DataClasses" if I don't know how many times it will be listed on an API request.
import requests
import json
import time
import re
with open('dataclasstest2.txt', 'r') as f:
emails = [line.strip() for line in f]
def main():
for name in emails:
url = 'https://haveibeenpwned.com/api/v2/breachedaccount/' + name
headers = {
'User-Agent': 'dataclasses.py'
'Accept: application/vnd.haveibeenpwned.v2+json'
}
r = requests.get(url, headers=headers)
if r.status_code == 200:
data = r.json()[0].get('DataClasses')
print(name + ":" + ', '.join(data))
time.sleep(2)
else:
time.sleep(2)
if __name__ == "__main__":
main()
Here is the output I am getting:
test#example.com:Email addresses, IP addresses, Names, Passwords
The script I have is exporting all users but I am looking to export users who have a type = xyz. There are two types of users in the directory such as type a and type b and i only want to export users who have type attribute matches b.
Please help me to add a clause/statement in the script so it should only pull users with Type "B" and ignore other users with ant other type.
import requests
import json
import re
import sys
import csv
orgName = ""
apiKey = ""
api_token = "SSWS "+ apiKey
headers = {'Accept':'application/json','Content-Type':'application/json','Authorization':api_token}
def GetPaginatedResponse(url):
response = requests.request("GET", url, headers=headers)
returnResponseList = []
responseJSON = json.dumps(response.json())
responseList = json.loads(responseJSON)
returnResponseList = returnResponseList + responseList
if "errorCode" in responseJSON:
print "\nYou encountered following Error: \n"
print responseJSON
print "\n"
return "Error"
else:
headerLink= response.headers["Link"]
while str(headerLink).find("rel=\"next\"") > -1:
linkItems = str(headerLink).split(",")
nextCursorLink = ""
for link in linkItems:
if str(link).find("rel=\"next\"") > -1:
nextCursorLink = str(link)
nextLink = str(nextCursorLink.split(";")[0]).strip()
nextLink = nextLink[1:]
nextLink = nextLink[:-1]
url = nextLink
response = requests.request("GET", url, headers=headers)
responseJSON = json.dumps(response.json())
responseList = json.loads(responseJSON)
returnResponseList = returnResponseList + responseList
headerLink= response.headers["Link"]
returnJSON = json.dumps(returnResponseList)
return returnResponseList
def DownloadSFUsers():
url = "https://"+orgName+".com/api/v1/users"
responseJSON = GetPaginatedResponse(url)
if responseJSON != "Error":
userFile = open("Only-Okta_Users.csv", "wb")
writer = csv.writer(userFile)
writer.writerow(["login","type"]).encode('utf-8')
for user in responseJSON:
login = user[u"profile"][u"login"]
type = user[u"credentials"][u"provider"][u"type"]
row = ("+login+","+type).encode('utf-8')
writer.writerow([login,type])
if __name__ == "__main__":
DownloadSFUsers()
Wrap your statement that writes a user to the csv file in an if statement that tests for the correct type.
Here is my code:
import base64
import requests
import json
import csv
USERNAME, PASSWORD = 'Username', 'Password'
req = requests.get(
url="https://api.mysportsfeeds.com/v1.1/pull/nhl/2017-2018-regular/game_startinglineup.json?gameid=20171109-EDM-NJD",
params={
"fordate": "20171009"
},
headers={
"Authorization": "Basic " +
base64.b64encode('{}:{}'.format(USERNAME,PASSWORD)\
.encode('utf-8')).decode('ascii')
}
)
data = req.json()
for i in range(1):
team_home = data['gamestartinglineup']['teamLineup'][i]['expected']['starter']
for i in range(2):
team_away = data['gamestartinglineup']['teamLineup'][i]['expected']['starter']
#headers = ["HomeTeam", "AwayTeam"]
for i in range(18):
homeplayer = team_home[i]['position']
awayplayer = team_away[i]['position']
homename = team_home[i]['player']['LastName']
awayname = team_away[i]['player']['LastName']
my_data =[]
my_data.append([homeplayer, homename, awayplayer, awayname])
print(my_data)
Here is what i am requesting:
{"gamestartinglineup":{"lastUpdatedOn":"2017-12-12 11:56:50 PM","game":{"id":"41009","date":"2017-11-09","time":"7:00PM","awayTeam":{"ID":"24","City":"Edmonton","Name":"Oilers","Abbreviation":"EDM"},"homeTeam":{"ID":"7","City":"New Jersey","Name":"Devils","Abbreviation":"NJD"},"location":"Prudential Center"},"teamLineup":[{"team":{"ID":"24","City":"Edmonton","Name":"Oilers","Abbreviation":"EDM"},"expected":{"starter":[{"position":"Goalie-Backup","player":{"ID":"5552","LastName":"Brossoit","FirstName":"Laurent","JerseyNumber":"1","Position":"G"}},{"position":"ForwardLine1-RW","player":{"ID":"4854","LastName":"Maroon","FirstName":"Patrick","JerseyNumber":"19","Position":"LW"}},{"position":"ForwardLine2-C","player":{"ID":"4993","LastName":"Nugent-Hopkins","FirstName":"Ryan","JerseyNumber":"93","Position":"C"}},{"position":"ForwardLine4-C","player":{"ID":"4730","LastName":"Letestu","FirstName":"Mark","JerseyNumber":"55","Position":"C"}},{"position":"ForwardLine3-LW","player":{"ID":"11308","LastName":"Caggiula","FirstName":"Drake","JerseyNumber":"91","Position":"LW"}},{"position":"ForwardLine4-RW","player":{"ID":"5875","LastName":"Khaira","FirstName":"Jujhar","JerseyNumber":"16","Position":"LW"}},{"position":"ForwardLine3-C","player":{"ID":"3637","LastName":"Jokinen","FirstName":"Jussi","JerseyNumber":"36","Position":"LW"}},{"position":"ForwardLine3-RW","player":{"ID":"4997","LastName":"Strome","FirstName":"Ryan","JerseyNumber":"18","Position":"C"}},{"position":"ForwardLine1-C","player":{"ID":"5576","LastName":"McDavid","FirstName":"Connor","JerseyNumber":"97","Position":"C"}},{"position":"ForwardLine1-LW","player":{"ID":"5417","LastName":"Draisaitl","FirstName":"Leon","JerseyNumber":"29","Position":"C"}},{"position":"ForwardLine2-LW","player":
My Output is: [['DefensePair1-R', 'Klefbom', 'DefensePair1-R', 'Severson']]
I created for loops for home and away and also the players within them. There are 20 per Home and Away. But it is only returning just 18.. I thought the expression is 0-18 not only return 18 so there must be something else in the script that is poorly written.
for i in range(18):
homeplayer = team_home[i]['position']
awayplayer = team_away[i]['position']
homename = team_home[i]['player']['LastName']
awayname = team_away[i]['player']['LastName']
my_data =[] ## HERE!
my_data.append([homeplayer, homename, awayplayer, awayname])
print(my_data)
Bring that out of the loop..
my_data =[] ## HERE!
for i in range(18):
homeplayer = team_home[i]['position']
awayplayer = team_away[i]['position']
homename = team_home[i]['player']['LastName']
awayname = team_away[i]['player']['LastName']
my_data.append([homeplayer, homename, awayplayer, awayname])
print(my_data)
Edit:
Found another snippet, possibly an error
for i in range(2):
team_away = data['gamestartinglineup']['teamLineup'][i]['expected']['starter']
In this snippet, you are overwriting the team_away. Not sure if you would like that
I can't find what I need to change in order to make it run, url and cookies are removed for my account safety etc.
What it is supposed to do is increment the url and tell me if it finds a page that has a string of html in its coding. It just displays error messages.
import urllib
import urllib2
import cookielib
import re
import os
from random import choice
item = '0'
password= '0'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [
('User-Agent', ''),
('Cookie', ''),
]
## Login
values = {}
print "",
url = "**************login.php"
data = urllib.urlencode(values)
home = opener.open(url)
#### START LOOP
i = 0
items = 0
page =["*****************&id="]
while x < 900:
i += 1
url = page + str(i)
home = opener.open(url).read()
#print home
match = re.search(r"<center>You do not have", home)
if match:
link = match.group(1)
print (page + str(i))
The following line:
page =["*****************&id="]
initialises page as a list containing one string. When you later do page + str(i), you're trying to add a list and a string, which doesn't make sense. Change the above line to:
page = "*****************&id="
and you will avoid that error.