0
votes

I have to retrieve tweets containing links to some YouTube videos. However, Twitter uses t.co URL-shortener service to short urls, so that if a user tweets the following link https://www.youtube.com/watch?v=x6QZn9xiuOE, the tweet reads something like https://t.co/bYeHhy9kAU

Thus, I can't use the original url, but I have to run a query using the shortened url.

How can I obtain the t.co shortened url starting from the original URL?

1
What does the request to t.co/HdshdsHe return? What headers are set by the server?user1907906
this is the shortened URL for the youtube video above: t.co/bYeHhy9kAUstochazesthai
Does t.co have a public API? If not, you are out of luck.user1907906
Mh, it seems that the only solution will be tweet the original URL, take its shortened version, and then run the query. :\stochazesthai

1 Answers

1
votes

I solved my problem by tweeting the original URL on my timeline, then reading its shortened version, and finally running a query using the shortened URL.

Here is the Python code I used:

twitter_api = twitter.Twitter(auth = auth)

# searching for tweets
video_list = ["HdwMY2Fa7G4","FuqLui0_EF8","ZLUSg1_-o4c",
"jvN5OwkwwmE","D2H839E2PIw","94wmdh23JsQ",
"MIXuXnnW4io","9Jo0uk9ewWQ","9eBHdFpmpHs",
"czr4nUEF77s","Q-P9ygH6T20","SWnXj8Nkpic",
"5IgvJ7mEl4c","grZPvHb0yA8","Gwk6FmiAKCo",
"vCub9qk4vTk","PX0qDfYNykc","HLz_4NVSO6c",
"rTB5kwmv9D4","gXwZ3dbIhjw"]

k = 0 # counter
for vid in video_list:
  k += 1
  # settings
  count = 10
  video_url = 'https://www.youtube.com/watch?v='+vid

  # tweet video URL in my timeline
  twitter_api.statuses.update(status = video_url)

  # retrieve shortened URL from my last tweet
  my_account = 'ibbessi'
  args = {'count' : 1}
  timeline = twitter_api.statuses.user_timeline(screen_name = my_account, **args)
  shortened_url = timeline[0]['text']
  print '# ', str(k), '\noriginal URL:', video_url, '\nshortened URL:', shortened_url, '\n'

  # search the shortened URL
  search_results = twitter_api.search.tweets(q = shortened_url, count = count)
  statuses = search_results['statuses']

  # output shortened URL
  print 'I have found ' + str(len(statuses)) + ' tweet(s) with video ' + shortened_url + '\n'
  for i in range(0,len(statuses)):
      print '#', str(i+1), '\nTEXT: ' , statuses[i]['text'], '\nUSER: ', statuses[i]['user']['name'], '\nRT COUNT: ', statuses[i]['retweet_count'], '\nDATE: ', statuses[i]['created_at'],'\n'