I need python code fixed
- or -
Post a project like this2344
$20
- Posted:
- Proposals: 1
- Remote
- #1782745
- PRE-FUNDED
- Completed
Description
Experience Level: Intermediate
Estimated project duration: 1 day or less
I am currently using python and tweepy to access a MySQL db and pull down screen_names in the store, I then request the tweets associated with the specific screen name and save them to MySQL for processing.
I have a central table where I store if an account has been pulled from Twitter, when I get the repose I update the central table to indicate that the screen_name has been processed.
The problem is that the central table is not being updated to indicate that the screen_name has been processed. Not being effective with Python I am not sure the syntax to use (I have tried strings and %s but that does not work). I need someone to urgently help and assist in fixing the below code so that I am able to execute the program. The line of code below I am having problems with is:
cursor.execute("UPDATE `xxxxxxxx.xxxxxxxxx` SET submitted = 'yes' WHERE screen_name=%s;", (screen_name,))
I need to finish this within the next two hours.
=============================================================================================
#!/usr/bin/env python
import tweepy
import MySQLdb
import sys
# Keys
consumer_key = 'xxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxx'
access_token = 'xxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxx'
host = "xxxxxxxxxx"
user = "xxxxxxxx"
passwd = "xxxxxxxxxxxx"
database = "xxxxxxxxx"
# Twitter initialization
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# MySQL initialization
connection = MySQLdb.connect(host=host, user=user, passwd=passwd, db=database, charset="latin1")
cursor = connection.cursor()
# The table schema: CREATE TABLE tweets
# tweet_id BIGINT NOT NULL,
# tweet_text VARCHAR(160) NOT NULL,
# screen_name VARCHAR(160) NOT NULL,
cursor.execute("SELECT screen_name FROM xxxxxx.xxxxxxxx WHERE platform = 'tw' and submitted = 'no' LIMIT 1")
screen_name = cursor.fetchone()[0]
print(screen_name)
cursor.execute("UPDATE `xxxxxxxx.xxxxxxxxx` SET submitted = 'yes' WHERE screen_name=%s;", (screen_name,))
print(screen_name)
print("submitting to twitter")
try:
statuses = api.user_timeline(screen_name=screen_name, count=200)
for user in statuses:
print(user.id, user.text.encode('UTF-8'), user.author.screen_name.encode('UTF-8'), user.author.id, user.created_at)
# load to MySQL server
cursor.execute("INSERT INTO tweets (tweet_id, tweet_text, screen_name, author_id, created_at, inserted_at) VALUES (%s, %s, %s, %s, %s, NOW());", (user.id, user.text.encode('UTF-8'), user.author.screen_name.encode('UTF-8'), user.author.id, user.created_at))
connection.commit()
if not cursor.rowcount:
print("no values found")
except tweepy.error.TweepError:
print("Tweepy error, could not fetch data!")
except UnicodeEncodeError:
print('Unicode error')
else:
print("Unexpected error: ", sys.exc_info()[0])
finally:
cursor.close()
connection.close()
I have a central table where I store if an account has been pulled from Twitter, when I get the repose I update the central table to indicate that the screen_name has been processed.
The problem is that the central table is not being updated to indicate that the screen_name has been processed. Not being effective with Python I am not sure the syntax to use (I have tried strings and %s but that does not work). I need someone to urgently help and assist in fixing the below code so that I am able to execute the program. The line of code below I am having problems with is:
cursor.execute("UPDATE `xxxxxxxx.xxxxxxxxx` SET submitted = 'yes' WHERE screen_name=%s;", (screen_name,))
I need to finish this within the next two hours.
=============================================================================================
#!/usr/bin/env python
import tweepy
import MySQLdb
import sys
# Keys
consumer_key = 'xxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxx'
access_token = 'xxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxx'
host = "xxxxxxxxxx"
user = "xxxxxxxx"
passwd = "xxxxxxxxxxxx"
database = "xxxxxxxxx"
# Twitter initialization
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# MySQL initialization
connection = MySQLdb.connect(host=host, user=user, passwd=passwd, db=database, charset="latin1")
cursor = connection.cursor()
# The table schema: CREATE TABLE tweets
# tweet_id BIGINT NOT NULL,
# tweet_text VARCHAR(160) NOT NULL,
# screen_name VARCHAR(160) NOT NULL,
cursor.execute("SELECT screen_name FROM xxxxxx.xxxxxxxx WHERE platform = 'tw' and submitted = 'no' LIMIT 1")
screen_name = cursor.fetchone()[0]
print(screen_name)
cursor.execute("UPDATE `xxxxxxxx.xxxxxxxxx` SET submitted = 'yes' WHERE screen_name=%s;", (screen_name,))
print(screen_name)
print("submitting to twitter")
try:
statuses = api.user_timeline(screen_name=screen_name, count=200)
for user in statuses:
print(user.id, user.text.encode('UTF-8'), user.author.screen_name.encode('UTF-8'), user.author.id, user.created_at)
# load to MySQL server
cursor.execute("INSERT INTO tweets (tweet_id, tweet_text, screen_name, author_id, created_at, inserted_at) VALUES (%s, %s, %s, %s, %s, NOW());", (user.id, user.text.encode('UTF-8'), user.author.screen_name.encode('UTF-8'), user.author.id, user.created_at))
connection.commit()
if not cursor.rowcount:
print("no values found")
except tweepy.error.TweepError:
print("Tweepy error, could not fetch data!")
except UnicodeEncodeError:
print('Unicode error')
else:
print("Unexpected error: ", sys.exc_info()[0])
finally:
cursor.close()
connection.close()
Robin M.
100% (22)Projects Completed
17
Freelancers worked with
14
Projects awarded
63%
Last project
29 Jan 2021
South Africa
New Proposal
Login to your account and send a proposal now to get this project.
Log inClarification Board Ask a Question
-
cursor.execute("UPDATE `xxxxxxxx.xxxxxxxxx` SET submitted = 'yes' WHERE screen_name=%s;", (screen_name,)) -> what's the comma (,) after 'screen_name'? Could that be a syntax error?
537189
We collect cookies to enable the proper functioning and security of our website, and to enhance your experience. By clicking on 'Accept All Cookies', you consent to the use of these cookies. You can change your 'Cookies Settings' at any time. For more information, please read ourCookie Policy
Cookie Settings
Accept All Cookies