Get only tweets which are retweeted by users and mentions - twitter

Here I am trying to get user's retweets:
Goal: To get the retweets of user for last one month
I already tried similar threads here is my try.
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
header('Location: ./clearsessions.php');
}
$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$content = $connection->get('account/verify_credentials');
$tweets = $connection->get("https://api.twitter.com/1.1/statuses user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
//var_dump($tweets);
foreach ($tweets as $item)
{
echo $item->text;
echo "<br/>";
$tweet = $item->text;
$insertQuery1 = "INSERT INTO user_tweets (`id`,`name`,`tweet`,`date`) VALUES ('".$id."','".$name."','".$tweet."','".$date."')";
if (!mysqli_query($con,$insertQuery1))
{
//die('Error: ' . mysqli_error($con));
}
}
Which gives messed json response where I can not extracts retweets.
Here is another try:
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/retweets_of_me.json?screen_name=".$twitteruser.");
Which gives not result.
One more try:
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2&include_rts=true
which also gives no result.
Please not, my account has some tweets which I have retweeted

Related

tweepy user_timeline with pagination returning max of 3200 tweets per twitter user

I'm using the code from here to scrape the tweets of a few users and export is as a .csv: https://towardsdatascience.com/tweepy-for-beginners-24baf21f2c25
I want to ideally get all the tweets of each user, but it seems to be limited to only the most recent 3200 tweets. Here's the exact code I'm using with trump as an example:
ids = ['realDonaldTrump']
def extract_hashtags(hashtag_list):
final_hashtag = ''
for hashtag in hashtag_list:
final_hashtag = final_hashtag + ' ' + hashtag['text']
return final_hashtag.strip()
#from https://towardsdatascience.com/tweepy-for-beginners-24baf21f2c25
class TweetMiner(object):
result_limit = 20
data = []
api = False
twitter_keys = { #redacted }
def __init__(self, keys_dict=twitter_keys, api=api, result_limit = 20):
self.twitter_keys = keys_dict
auth = tw.OAuthHandler(keys_dict['consumer_key'], keys_dict['consumer_secret'])
auth.set_access_token(keys_dict['access_token_key'], keys_dict['access_token_secret'])
self.api = tw.API(auth)
self.twitter_keys = keys_dict
self.result_limit = result_limit
def mine_user_tweets(self, user,
mine_rewteets=False,
max_pages=5):
data = []
last_tweet_id = False
page = 1
while page <= max_pages:
if last_tweet_id:
statuses = self.api.user_timeline(screen_name=user,
count=self.result_limit,
max_id=last_tweet_id - 1,
tweet_mode = 'extended',
include_retweets=True
)
else:
statuses = self.api.user_timeline(screen_name=user,
count=self.result_limit,
tweet_mode = 'extended',
include_retweets=True)
for item in statuses:
mined = {
'tweet_id': item.id,
'name': item.user.name,
'screen_name': item.user.screen_name,
'retweet_count': item.retweet_count,
'text': item.full_text,
'mined_at': datetime.datetime.now(),
'created_at': item.created_at,
#'time_zone': item._json['time_zone'],
'favourite_count': item.favorite_count,
'hashtags': extract_hashtags(item.entities['hashtags']),
#'links': extract_
'status_count': item.user.statuses_count,
'location': item.place,
'source_device': item.source
}
try:
mined['retweet_text'] = item.retweeted_status.full_text
except:
mined['retweet_text'] = 'None'
try:
mined['quote_text'] = item.quoted_status.full_text
mined['quote_screen_name'] = status.quoted_status.user.screen_name
except:
mined['quote_text'] = 'None'
mined['quote_screen_name'] = 'None'
last_tweet_id = item.id
data.append(mined)
page += 1
return data
#result_limit * max_pages is the no of tweets for each id
miner=TweetMiner(result_limit = 460) #200
counter = 0
counter2 = 0
for id in ids:
try:
print("Fetching tweets of " + id+ " now...")
mined_tweets = miner.mine_user_tweets(user= id, max_pages=460) #100
mined_tweets_df= pd.DataFrame(mined_tweets)
counter2 = counter2 +1
#after 40 tries, pause for 15 mins
if counter2%40==0: #5
print("Couldn't fetch, sleeping for 15 mins")
time.sleep(900) #15 minute sleep time
except:
print(id, 'is invalid or locked')
if counter>0:
final_df = pd.concat([final_df, mined_tweets_df], ignore_index = True)
print("Fetched and added!")
else:
final_df = mined_tweets_df
print("Fetched and added!")
counter +=1
print(final_df)
final_df.to_csv('tweets.csv', encoding='UTF-8')
The number of tweets returned should be 460*460 = 211,600 tweets for each user in ids, but it only returns a total of 3200 tweets per id. Is this limit a strict one built into the API, and if so, is there any way to get more than 3200 tweets per user?
This is a limit built into the Twitter API. The user timeline can only return a maximum of 3200 Tweets (in 200 Tweets per “page”). To retrieve more, you would need to use the premium or enterprise full archive search API.

WooCommerce Subscriptions get Billing Interval & ID

I am using a stripe "wc_gateway_stripe_process_response" to get payment information. What is needed is what is the payment interval for that product and what the user's subscription id is. This is the code I have to date:
function woointerface_ProcessOrder($order_id)
{
$order = new WC_Order( $order_id );
$TransactionId = $order->get_transaction_id();
$szOrderId = $order->get_order_number();
// Find all products associated with this order
$order_data = $order->get_data();
$order_billing_email = $order->get_billing_email();
$payment_method = $order->get_payment_method_title();
$PaymentDate = $order->get_date_paid();
$OrderNumber = $order->get_order_number();
$User = get_user_by( 'email', $order_billing_email );
$FirstName = $User->first_name;
$LastName = $User->last_name;
$UserId = $User->ID;
$PayerName = $FirstName . ' ' . $LastName;
$items = $order->get_items();
foreach( $items as $item_id => $product )
{
$ProductName = $product->get_name();
$ProductId = $product->get_product_id();
$PaymentAmount = $product->get_total();
$ProductIndex = wc_get_product($ProductId);
if(! WC_Subscriptions_Product::is_subscription( $ProductIndex ) )
continue;
$UserSubscriptionId = ??
$BillingCycle = ??
// Store in local table.
}
Looking for how to get the User subscription id and billing cycle for this order.
Found the solution for billing and Id. The following calls give you the billing and user subscription id information:
$BillingCycle = WC_Subscriptions_Product::get_interval($ProductIndex); // how many times within the period to bill
$BillingPeriod = WC_Subscriptions_Product::get_period($ProductIndex); // Month/week/Year
$subscriptions = wcs_get_subscriptions(array('order_id' => $order->id, 'product_id' => $ProductId));
if (!empty($subscriptions))
{
$subscription = array_pop($subscriptions);
$PaidSubscriptionId = $subscription->id;
}

Basic Twitter Data Mining Causing Problem

This is my first attempt to extract tweets using twitter api and tweepy. When I execute my code it keep printing 401 every time in a new line. What am I doing wrong is I am not able to figure out. Any help is appreciated.
import tweepy
import json
access_token = ""
access_token_secret = ""
consumer_key = ""
consumer_secret = ""
auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_token_secret)
class MyStreamListener(tweepy.StreamListener):
def __init__(self, api=None):
super(MyStreamListener, self).__init__()
self.num_tweets = 0
self.file = open("tweets.txt", "w")
def on_status(self, status):
tweet = status._json
self.file.write( json.dumps(tweet) + '\n' )
self.num_tweets += 1
if self.num_tweets < 100:
return True
else:
return False
self.file.close()
def on_error(self, status):
print(status)
l = MyStreamListener()
stream=tweepy.Stream(auth,l)
stream.filter()
tweets_data_path = 'tweets.txt'
tweets_file = open(tweets_data_path, "r")
tweets_data = []
for line in tweets_file:
tweet = json.loads(line)
tweets_data.append(tweet)
tweets_file.close()
print(tweets_data[0].keys())
Go to your twitter account settings and change timezone to that as of your computer. Then, go to twitter app settings and generate new consumer key and new access token. These newly generated keys and tokens you should use to avoid 401 error.

Neo4jPHP Transaction queries: getting errors in query construction

I am trying to implement basic transaction based queries using Jadell's Neo4jPHP.
Here is my code:
$transaction = $client->beginTransaction();
$query = new Query($client, "CREATE UNIQUE (u:users {email})-[r:visited {'time':'1425283200'}]->(e:halls {hallId}) RETURN r", array('email' => array('email' => 'test#test.com'), 'hallId' => array('hallId' => 1234)));
$result = $transaction->addStatements($query);
$transaction->commit();
I am getting an error:
[message] => Invalid input ''': expected whitespace, a property key
name, '}', an identifier or UnsignedDecimalInteger (line 1, column
52)\n
Any clues as to what is going wrong here?
UPDATE
I tried the following (removed parameters) and still getting an error:
$transaction = $client->beginTransaction();
$query = new Query($client, "CREATE UNIQUE (u:users {'email':'" . $email . "'})-[r:visited]->(e:halls {'hallId':'" . $hallId . "'}) RETURN r");
$result = $transaction->addStatements($query);
$transaction->commit();
Getting an error:
[message] => Unable to deserialize request: Can not deserialize
instance of java.util.LinkedHashMap out of START_ARRAY token\n
This should work.
Use query parameters instead of string concatenation
$transaction = $client->beginTransaction();
$cypher="CREATE UNIQUE (u:users {email:{email}})-[r:visited]->(e:halls {hallId:{hallId}}) RETURN r";
$query = new Query($client,$cypher,array('email'=>$email,'hallId'=>$hallId));
$result = $transaction->addStatements($query);
$transaction->commit();

Twitter API call not working

Hi Friends i am trying to run twitter apis to get tweets for a hashtag using below code. When i tried get the user timeline it's not giving any error for authentication but when it tried to search for tweets which contains hahstag it's giving authentication error.
$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$host = 'api.twitter.com';
$method = 'GET';
$path = '/1.1/statuses/user_timeline.json'; // api call path
$query = array( // query parameters
'screen_name' => 'twitterapi'
);
$oauth = array(
'oauth_consumer_key' => $consumer_key,
'oauth_token' => $token,
'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
'oauth_timestamp' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_version' => '1.0'
);
$oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
$query = array_map("rawurlencode", $query);
$arr = array_merge($oauth, $query); // combine the values THEN sort
asort($arr); // secondary sort (value)
ksort($arr); // primary sort (key)
// http_build_query automatically encodes, but our parameters
// are already encoded, and must be by this point, so we undo
// the encoding step
$querystring = urldecode(http_build_query($arr, '', '&'));
$url = "https://$host$path";
// mash everything together for the text to hash
$base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);
// same with the key
$key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);
// generate the hash
$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));
// this time we're using a normal GET query, and we're only encoding the query params
// (without the oauth params)
$url .= "?".http_build_query($query);
$url=str_replace("&","&",$url); //Patch by #Frewuill
$oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
ksort($oauth); // probably not necessary, but twitter's demo does it
// also not necessary, but twitter's demo does this too
function add_quotes($str) { return '"'.$str.'"'; }
$oauth = array_map("add_quotes", $oauth);
// this is the full value of the Authorization line
$auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));
echo $auth;exit;
// if you're doing post, you need to skip the GET building above
// and instead supply query parameters to CURLOPT_POSTFIELDS
$options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
//CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
// do our business
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
$twitter_data = json_decode($json);
echo "<pre>";print_r($twitter_data);
When i run this code i am successfully able to get the user time line so i wnt for next step to get tweets for a particular hashtag by chnaging code like below
$host = 'api.twitter.com';
$method = 'GET';
$path = '/1.1/search/tweets.json'; // api call path
$query = array( // query parameters
'q' => '#Polls2013'
);
But now it's giving a weird error like below.
stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[message] => Could not authenticate you
[code] => 32
)
)
)
The query you are posting for search should be url encoded in the manner specified by twitter,
See this documentation (https://dev.twitter.com/docs/auth/percent-encoding-parameters)

Resources