SHARE
TWEET

Untitled

a guest Oct 21st, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from TwitterAPI import TwitterAPI
  2. import os
  3. import json
  4. import requests
  5.  
  6. cons_key = ""
  7. cons_sec_key = ""   #Put yours keys in
  8. acc_tok = ""        #these lines
  9. acc_tok_sec = ""
  10.  
  11. api = TwitterAPI(cons_key, cons_sec_key, acc_tok, acc_tok_sec)
  12.  
  13. def main(nick):
  14.     reqst = api.request("users/lookup", {"screen_name": nick})
  15.     jsn = json.loads(reqst.text)
  16.     try:
  17.         if jsn["errors"][0]["code"] == 17:
  18.             print("This page doesn't exist")
  19.         elif '""errors":' in reqst.text:
  20.             print("ERROR\nResponse data:", reqst.txt)
  21.         exit()
  22.     except:
  23.         os.mkdir("Twitter")
  24.         count, count_for_tweets = info_about_acc(jsn[0])
  25.     print("Downloading list of followers...")
  26.     followers(nick, "-1", [])
  27.     friends(nick, "-1", [])
  28.     getting_tweets(nick, [], 0, count_for_tweets, "")
  29.     getting_favorites(nick, count, 0, [], '')
  30.  
  31.  
  32.  
  33. def parsing_tweets(tweet):
  34.     try:
  35.         author_nickname = tweet["retweeted_status"]["user"]["screen_name"]
  36.         author_name = tweet["retweeted_status"]["user"]["name"]
  37.         text = str(tweet["retweeted_status"]["text"])
  38.         link_to_tweet = "https://twitter.com/"+author_nickname+"/status/"+tweet["id_str"]
  39.         tweets_like = tweet["retweeted_status"]["favorite_count"]
  40.         tweets_retweet = tweet["retweeted_status"]["retweet_count"]
  41.         with open("Twitter/Retweets.txt", "a", encoding="utf-8") as file:
  42.             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")
  43.  
  44.     except:
  45.         text = tweet["text"]
  46.         data = tweet["created_at"]
  47.         likes = tweet["favorite_count"]
  48.         retweets = tweet["retweet_count"]
  49.         link_to_tweet = "https://twitter.com/" + tweet["user"]["screen_name"] + "/status/" + tweet["id_str"]
  50.         with open("Twitter/Tweets.txt", "a", encoding="utf-8") as file:
  51.             file.write("'"+text+"'\n\n\t"+data+"\t\t"+str(retweets)+" Retweet(s)  "+str(likes)+
  52.                        " Like(s)"+"\n\n\tTweet - "+link_to_tweet+"\n\n\n"+"--"*25+"\n")
  53.  
  54.     file.close()
  55.  
  56.  
  57. def getting_tweets(nickname, media, count, statuses, max_id):
  58.     if max_id == "":
  59.         r = api.request("statuses/user_timeline", {"screen_name": nickname, "count": "200"})
  60.     else:
  61.         r = api.request("statuses/user_timeline", {"screen_name": nickname, "count": "200", "max_id": max_id})
  62.     jsn = json.loads(r.text)
  63.     media += jsn
  64.     count += 200
  65.  
  66.     if statuses > count:
  67.         del media[-1]
  68.         getting_tweets(nickname, media, count, statuses, jsn[-1]["id_str"])
  69.  
  70.     else:
  71.         print("Got "+str(len(media))+" tweet(s)")
  72.         print("Downloading tweets")
  73.         for i in range(len(media)):
  74.             parsing_tweets(media[i])
  75.         print("Done")
  76.  
  77.  
  78. def parsing_followers_friends(acc_to_parse, friends_or_followers):
  79.     name = acc_to_parse["name"]
  80.     nickname_of_user = acc_to_parse["screen_name"]
  81.  
  82.     info = name +"\t(@"+nickname_of_user+")\n\n"
  83.  
  84.     if friends_or_followers == "Friends":
  85.         with open("Twitter/Friends.txt", "a", encoding="utf-8",) as file:
  86.             file.write(info)
  87.     if friends_or_followers == "Followers":
  88.         with open("Twitter/Followers.txt", "a", encoding="utf-8",) as file:
  89.             file.write(info)
  90.  
  91.  
  92. def friends(nickname, cursor, media_to_parse):
  93.     r = api.request("friends/list", {"cursor": cursor, "screen_name": nickname, "skip_status": "true",
  94.                                        "include_user_entities": "false", "count": "200"})
  95.     jsn = json.loads(r.text)
  96.     media_to_parse += jsn["users"]
  97.  
  98.     if jsn["next_cursor"] != 0:
  99.         friends(nickname, jsn["next_cursor"], media_to_parse)
  100.     else:
  101.         print("Got ", str(len(str(media_to_parse).split("'id': "))-1), " friend(s)")
  102.         print("Downloading list of friends...")
  103.         for i in range(len(str(media_to_parse).split("'id':"))-1):
  104.             parsing_followers_friends(media_to_parse[i], "Friends")
  105.         print("Done\n")
  106.  
  107.  
  108. def followers(nickname, cursor, media_to_parse):
  109.     r = api.request("followers/list", {"cursor": cursor, "screen_name": nickname, "skip_status": "true",
  110.                                        "include_user_entities": "false", "count": "200"})
  111.     jsn = json.loads(r.text)
  112.     print(jsn)
  113.     media_to_parse += jsn["users"]
  114.  
  115.     if jsn["next_cursor"] != 0:
  116.         followers(nickname, jsn["next_cursor"], media_to_parse)
  117.     else:
  118.         print("Got ", len(str(media_to_parse).split("'id': "))-1, " follower(s)")
  119.         print("Downloading list of followers...")
  120.         for i in range(len(str(media_to_parse).split("'id':"))-1):
  121.             parsing_followers_friends(media_to_parse[i], "Followers")
  122.         print("Done\n")
  123.  
  124.  
  125. def info_about_acc(jsn):
  126.     print("Getting information...")
  127.     id = str(jsn["id"])
  128.     name = jsn["name"]
  129.     nickname = jsn["screen_name"]
  130.     followers = str(jsn["followers_count"])
  131.     friends = str(jsn["friends_count"])
  132.     data_of_creation = jsn["created_at"]
  133.     count_of_liked_tweets = jsn["favourites_count"]
  134.     verified = jsn["verified"]
  135.     profile_pic = jsn["profile_image_url_https"]
  136.     statuses_count = jsn["statuses_count"]
  137.  
  138.     lang = jsn["lang"]
  139.     if lang is not None:
  140.         lang = "\nLang -" + lang
  141.     else:
  142.         lang = ''
  143.  
  144.     location = jsn["location"]
  145.     if location != '':
  146.         location = "\nLocation -" + location
  147.  
  148.     description = jsn["description"]
  149.     if description != '':
  150.         description = "\n\tDescription -" + description
  151.  
  152.     print("Downloading photos...")
  153.     background_pic = jsn["profile_background_image_url"]
  154.     if background_pic is not None:
  155.         photo_back = requests.get(background_pic)
  156.         out_back = open("Twitter/Profile background photo.jpg", "wb")
  157.         out_back.write(photo_back.content)
  158.         out_back.close()
  159.         background_pic = '\nBackground picture - ' + background_pic
  160.     else:
  161.         background_pic = ''
  162.  
  163.     if not jsn["profile_use_background_image"]:
  164.         banner_pic = jsn['profile_banner_url']
  165.         photo = requests.get(banner_pic)
  166.         out = open("Twitter/Profile banner.jpg", "wb")
  167.         out.write(photo.content)
  168.         out.close()
  169.  
  170.     photo = requests.get(profile_pic.split("_normal")[0]+profile_pic.split("_normal")[1])
  171.     out = open("Twitter/Profile photo.jpg", "wb")
  172.     out.write(photo.content)
  173.     out.close()
  174.  
  175.     print("Writing information to the file info.txt...")
  176.     info = "ID - " + id + "\nName - " + name + "\nNickname - " + nickname + "\nFollowers - " + followers + "\nFriends - " + friends+\
  177.     "\nData of creation - " + data_of_creation + "\nCount of liked twiters - " + str(count_of_liked_tweets) + "\nVerified - "+\
  178.     str(verified) + "\nStatuses count - " + str(statuses_count) + lang + location + description + background_pic + \
  179.            "\nProfile pic - " + profile_pic
  180.  
  181.     file = open("Twitter/Info.txt", "w")
  182.     file.write(info)
  183.  
  184.     file.close()
  185.  
  186.     return count_of_liked_tweets, statuses_count
  187.  
  188. def getting_favorites(nick, count, counter, media, max_id):
  189.     if max_id == '':
  190.         reqst = api.request("favorites/list", {"count": 200, "screen_name": nick})
  191.     else:
  192.         reqst = api.request("favorites/list", {"count": 200, "screen_name": nick, "max_id": max_id})
  193.  
  194.     jsn = json.loads(reqst.text)
  195.     counter += 200
  196.     media += jsn
  197.  
  198.     if count > counter:
  199.         del media[-1]
  200.         getting_favorites(nick, count, counter, media, jsn[-1]["id_str"])
  201.     else:
  202.         print("Got " + str(len(media)) + " Liked tweet(s)")
  203.         print("Downloading these tweets...")
  204.         for i in range(len(media) - 1):
  205.             text = media[i]["text"]
  206.             data = media[i]["created_at"]
  207.             likes = media[i]["favorite_count"]
  208.             retweets = media[i]["retweet_count"]
  209.             link_to_tweet = "https://twitter.com/" + media[i]["user"]["screen_name"] + "/status/" + media[i]["id_str"]
  210.             author_name = media[i]["user"]["name"]
  211.             author_nickname = media[i]["user"]["screen_name"]
  212.             with open("Twitter/Liked.txt", "a", encoding="utf-8") as file:
  213.                 file.write(author_name + "  ( @" + author_nickname + ")\n" + "\n'" + text + "'\n\n\t" + data + "\t\t"
  214.                            + str(retweets) + " Retweet(s)  " + str(likes) + " Like(s)" + "\n\n\tTweet - " +
  215.                            link_to_tweet + "\n\n\n" + "--" * 25 + "\n")
  216.  
  217.     print("Done")
  218.  
  219. #write a nickname
  220. if __name__ == '__main__':
  221.     main("")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top