Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from TwitterAPI import TwitterAPI
- import os
- import json
- import requests
- cons_key = ""
- cons_sec_key = "" #Put yours keys in
- acc_tok = "" #these lines
- acc_tok_sec = ""
- api = TwitterAPI(cons_key, cons_sec_key, acc_tok, acc_tok_sec)
- def main(nick):
- reqst = api.request("users/lookup", {"screen_name": nick})
- jsn = json.loads(reqst.text)
- try:
- if jsn["errors"][0]["code"] == 17:
- print("This page doesn't exist")
- elif '""errors":' in reqst.text:
- print("ERROR\nResponse data:", reqst.txt)
- exit()
- except:
- os.mkdir("Twitter")
- count, count_for_tweets = info_about_acc(jsn[0])
- print("Downloading list of followers...")
- followers(nick, "-1", [])
- friends(nick, "-1", [])
- getting_tweets(nick, [], 0, count_for_tweets, "")
- getting_favorites(nick, count, 0, [], '')
- def parsing_tweets(tweet):
- try:
- author_nickname = tweet["retweeted_status"]["user"]["screen_name"]
- author_name = tweet["retweeted_status"]["user"]["name"]
- text = str(tweet["retweeted_status"]["text"])
- link_to_tweet = "https://twitter.com/"+author_nickname+"/status/"+tweet["id_str"]
- tweets_like = tweet["retweeted_status"]["favorite_count"]
- tweets_retweet = tweet["retweeted_status"]["retweet_count"]
- with open("Twitter/Retweets.txt", "a", encoding="utf-8") as file:
- file.write(author_name+" (@"+author_nickname+")\n\t'"+text+"'\n\n\t"+str(tweets_retweet)+" Retweet(s) "+str(tweets_like)+" Like(s)"+"\n\n\tTweet - "+link_to_tweet+"\n\n"+"--"*25+"\n")
- except:
- text = tweet["text"]
- data = tweet["created_at"]
- likes = tweet["favorite_count"]
- retweets = tweet["retweet_count"]
- link_to_tweet = "https://twitter.com/" + tweet["user"]["screen_name"] + "/status/" + tweet["id_str"]
- with open("Twitter/Tweets.txt", "a", encoding="utf-8") as file:
- file.write("'"+text+"'\n\n\t"+data+"\t\t"+str(retweets)+" Retweet(s) "+str(likes)+
- " Like(s)"+"\n\n\tTweet - "+link_to_tweet+"\n\n\n"+"--"*25+"\n")
- file.close()
- def getting_tweets(nickname, media, count, statuses, max_id):
- if max_id == "":
- r = api.request("statuses/user_timeline", {"screen_name": nickname, "count": "200"})
- else:
- r = api.request("statuses/user_timeline", {"screen_name": nickname, "count": "200", "max_id": max_id})
- jsn = json.loads(r.text)
- media += jsn
- count += 200
- if statuses > count:
- del media[-1]
- getting_tweets(nickname, media, count, statuses, jsn[-1]["id_str"])
- else:
- print("Got "+str(len(media))+" tweet(s)")
- print("Downloading tweets")
- for i in range(len(media)):
- parsing_tweets(media[i])
- print("Done")
- def parsing_followers_friends(acc_to_parse, friends_or_followers):
- name = acc_to_parse["name"]
- nickname_of_user = acc_to_parse["screen_name"]
- info = name +"\t(@"+nickname_of_user+")\n\n"
- if friends_or_followers == "Friends":
- with open("Twitter/Friends.txt", "a", encoding="utf-8",) as file:
- file.write(info)
- if friends_or_followers == "Followers":
- with open("Twitter/Followers.txt", "a", encoding="utf-8",) as file:
- file.write(info)
- def friends(nickname, cursor, media_to_parse):
- r = api.request("friends/list", {"cursor": cursor, "screen_name": nickname, "skip_status": "true",
- "include_user_entities": "false", "count": "200"})
- jsn = json.loads(r.text)
- media_to_parse += jsn["users"]
- if jsn["next_cursor"] != 0:
- friends(nickname, jsn["next_cursor"], media_to_parse)
- else:
- print("Got ", str(len(str(media_to_parse).split("'id': "))-1), " friend(s)")
- print("Downloading list of friends...")
- for i in range(len(str(media_to_parse).split("'id':"))-1):
- parsing_followers_friends(media_to_parse[i], "Friends")
- print("Done\n")
- def followers(nickname, cursor, media_to_parse):
- r = api.request("followers/list", {"cursor": cursor, "screen_name": nickname, "skip_status": "true",
- "include_user_entities": "false", "count": "200"})
- jsn = json.loads(r.text)
- print(jsn)
- media_to_parse += jsn["users"]
- if jsn["next_cursor"] != 0:
- followers(nickname, jsn["next_cursor"], media_to_parse)
- else:
- print("Got ", len(str(media_to_parse).split("'id': "))-1, " follower(s)")
- print("Downloading list of followers...")
- for i in range(len(str(media_to_parse).split("'id':"))-1):
- parsing_followers_friends(media_to_parse[i], "Followers")
- print("Done\n")
- def info_about_acc(jsn):
- print("Getting information...")
- id = str(jsn["id"])
- name = jsn["name"]
- nickname = jsn["screen_name"]
- followers = str(jsn["followers_count"])
- friends = str(jsn["friends_count"])
- data_of_creation = jsn["created_at"]
- count_of_liked_tweets = jsn["favourites_count"]
- verified = jsn["verified"]
- profile_pic = jsn["profile_image_url_https"]
- statuses_count = jsn["statuses_count"]
- lang = jsn["lang"]
- if lang is not None:
- lang = "\nLang -" + lang
- else:
- lang = ''
- location = jsn["location"]
- if location != '':
- location = "\nLocation -" + location
- description = jsn["description"]
- if description != '':
- description = "\n\tDescription -" + description
- print("Downloading photos...")
- background_pic = jsn["profile_background_image_url"]
- if background_pic is not None:
- photo_back = requests.get(background_pic)
- out_back = open("Twitter/Profile background photo.jpg", "wb")
- out_back.write(photo_back.content)
- out_back.close()
- background_pic = '\nBackground picture - ' + background_pic
- else:
- background_pic = ''
- if not jsn["profile_use_background_image"]:
- banner_pic = jsn['profile_banner_url']
- photo = requests.get(banner_pic)
- out = open("Twitter/Profile banner.jpg", "wb")
- out.write(photo.content)
- out.close()
- photo = requests.get(profile_pic.split("_normal")[0]+profile_pic.split("_normal")[1])
- out = open("Twitter/Profile photo.jpg", "wb")
- out.write(photo.content)
- out.close()
- print("Writing information to the file info.txt...")
- info = "ID - " + id + "\nName - " + name + "\nNickname - " + nickname + "\nFollowers - " + followers + "\nFriends - " + friends+\
- "\nData of creation - " + data_of_creation + "\nCount of liked twiters - " + str(count_of_liked_tweets) + "\nVerified - "+\
- str(verified) + "\nStatuses count - " + str(statuses_count) + lang + location + description + background_pic + \
- "\nProfile pic - " + profile_pic
- file = open("Twitter/Info.txt", "w")
- file.write(info)
- file.close()
- return count_of_liked_tweets, statuses_count
- def getting_favorites(nick, count, counter, media, max_id):
- if max_id == '':
- reqst = api.request("favorites/list", {"count": 200, "screen_name": nick})
- else:
- reqst = api.request("favorites/list", {"count": 200, "screen_name": nick, "max_id": max_id})
- jsn = json.loads(reqst.text)
- counter += 200
- media += jsn
- if count > counter:
- del media[-1]
- getting_favorites(nick, count, counter, media, jsn[-1]["id_str"])
- else:
- print("Got " + str(len(media)) + " Liked tweet(s)")
- print("Downloading these tweets...")
- for i in range(len(media) - 1):
- text = media[i]["text"]
- data = media[i]["created_at"]
- likes = media[i]["favorite_count"]
- retweets = media[i]["retweet_count"]
- link_to_tweet = "https://twitter.com/" + media[i]["user"]["screen_name"] + "/status/" + media[i]["id_str"]
- author_name = media[i]["user"]["name"]
- author_nickname = media[i]["user"]["screen_name"]
- with open("Twitter/Liked.txt", "a", encoding="utf-8") as file:
- file.write(author_name + " ( @" + author_nickname + ")\n" + "\n'" + text + "'\n\n\t" + data + "\t\t"
- + str(retweets) + " Retweet(s) " + str(likes) + " Like(s)" + "\n\n\tTweet - " +
- link_to_tweet + "\n\n\n" + "--" * 25 + "\n")
- print("Done")
- #write a nickname
- if __name__ == '__main__':
- main("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement