Advertisement
Guest User

Untitled

a guest
Nov 11th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. ###########################################################################
  2. # IP address changer, rout traffic through Tor, it needs Tor to be running
  3. ###########################################################################
  4. import socket
  5. import socks
  6.  
  7. import requests
  8. from bs4 import BeautifulSoup
  9.  
  10. from stem import Signal
  11. from stem.control import Controller
  12.  
  13.  
  14. controller = Controller.from_port(port=9151)
  15.  
  16. def connectTor():
  17. socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5 , "127.0.0.1", 9150, True)
  18. socket.socket = socks.socksocket
  19.  
  20. def renew_tor():
  21. controller.authenticate()
  22. controller.signal(Signal.NEWNYM)
  23.  
  24. def showmyip():
  25. url = "http://www.showmyip.gr/"
  26. r = requests.Session()
  27. page = r.get(url)
  28. soup = BeautifulSoup(page.content, "lxml")
  29. ip_address = soup.find("span",{"class":"ip_address"}).text.strip()
  30. print(ip_address)
  31.  
  32. #############################################################
  33. # Random Name for custom_useragent
  34. #############################################################
  35.  
  36. def randomName(length):
  37. import string
  38. import random
  39. return ''.join(random.choice(string.ascii_letters) for i in range(length))
  40.  
  41. ################################################################
  42. # Main functions
  43. ################################################################
  44.  
  45. def requestDailydatafromGT(keywords, geography, date): #parameters must be strings
  46. from pytrends.request import TrendReq
  47. import time
  48. from random import randint
  49.  
  50.  
  51. google_username = "" #put your gmail account
  52. google_password = "" #passwrd
  53. mes=0
  54.  
  55. if date =='today':
  56. requestdate='today 3-m'
  57. else:
  58. requestdate=str(date)+' 3m'
  59. trend_payload = {'q': keywords,'hl': 'en-US','geo': geography, 'date': requestdate} #define parameters of the request
  60. pytrend = TrendReq(google_username, google_password, custom_useragent=randomName(randint(5,10)) #connect to Google
  61.  
  62. while mes==0:
  63. try:
  64. results= pytrend.trend(trend_payload, return_type='dataframe').sort_index(axis=0, ascending=False) #launch request in Google tren0ds
  65. mes=1
  66.  
  67. except Exception:
  68. renew_tor()
  69. connectTor()
  70. showmyip() #optional
  71. pytrend = TrendReq(google_username, google_password, custom_useragent=randomName(randint(5,10)) #connect to Google
  72. mes=0
  73.  
  74. return results
  75.  
  76. def requestAllDailyData(keywords,geography,start_date,end_date):
  77. import numpy as np
  78. import pandas as pd
  79. from datetime import datetime
  80.  
  81. Time_end_date=datetime(int(end_date[3:7]),int(end_date[0:2]), 1)
  82.  
  83. dailyData=requestDailydatafromGT(keywords, geography, start_date) #we look for the first period
  84.  
  85. lastDate=datetime(dailyData.index[-1].year,dailyData.index[-1].month,1) #This is the lastDate of the dailyData Frame.
  86. RemainingTime=lastDate-Time_end_date
  87.  
  88. while RemainingTime.days >0:
  89.  
  90. #we look for the new date of the data Frame to be added at the end
  91. Month=(dailyData.index[-1]-pd.DateOffset(months=2)).month
  92. Year=(dailyData.index[-1]-pd.DateOffset(months=2)).year
  93. NewDate="{0}/{1}".format(Month,Year)
  94.  
  95. NewdailyData=requestDailydatafromGT(keywords, geography,NewDate)
  96.  
  97. NewdailyData[NewdailyData==0]=0.00001
  98. NewdailyData=NewdailyData[NewdailyData.index<=dailyData.index[-1]]
  99.  
  100. #compute the coef in order to adjust the new data
  101. MatrixFullCoef= pd.DataFrame(index=NewdailyData.index, columns=NewdailyData.columns)
  102. MatrixFullCoef[:1]=dailyData[-1:]/NewdailyData[:1]
  103.  
  104. for i in range(len(MatrixFullCoef)):
  105. MatrixFullCoef.iloc[i]=MatrixFullCoef.iloc[0]
  106.  
  107. #Normalize the NewDailyData with the coef calculated before
  108. AdjustedNewdailyData=(NewdailyData*MatrixFullCoef)[1:]
  109.  
  110. #add the new data Frame of daily datas to the existing one
  111. dailyData= dailyData.append(AdjustedNewdailyData,ignore_index=False)
  112.  
  113. #return the last date of the computed dataframe of daily datas
  114. lastDate=datetime(dailyData.index[-1].year,dailyData.index[-1].month,dailyData.index[-1].day)
  115.  
  116. RemainingTime=lastDate-Time_end_date
  117.  
  118. return dailyData
  119.  
  120.  
  121. ##############################################
  122. # Execution
  123. #############################################
  124. import matplotlib.pyplot as plt
  125. %matplotlib inline
  126.  
  127. keywords='trump'
  128. Geo='US'
  129. start_date="today"
  130. end_date="01/2014"
  131.  
  132. DailyGTData=requestAllDailyData(keywords,Geo,start_date,end_date)
  133.  
  134. #plot the data
  135. DailyGTData.sort_index(axis=0, ascending=True).plot()
  136. plt.xlabel('time')
  137. plt.ylabel(keywords)
  138.  
  139. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement