Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A serial mode without using Pool #280

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions twitterscraper/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def get_proxies():
list_ip = [elem[0].text for elem in list_td]
list_ports = [elem[1].text for elem in list_td]
list_proxies = [':'.join(elem) for elem in list(zip(list_ip, list_ports))]
return list_proxies
return list_proxies

def get_query_url(query, lang, pos, from_user = False):
if from_user:
if pos is None:
Expand Down Expand Up @@ -214,18 +214,44 @@ def query_tweets_once(*args, **kwargs):
else:
return []

def query_tweets(query, limit=None, begindate=dt.date(2006, 3, 21),
enddate=dt.date.today(), poolsize=20, lang=''):
number_days = (enddate - begindate).days

def query_tweets(query, limit=None, begindate=dt.date(2006, 3, 21), enddate=dt.date.today(), poolsize=20, lang='', use_proxy=True):
no_days = (enddate - begindate).days

if(no_days < 0):
sys.exit('Begin date must occur before end date.')
if(number_days < 0):
print('Begin date must occur before end date.')
return []

all_tweets = []
if poolsize > 1:
all_tweets = query_tweets_parallel(query, limit=limit, begindate=begindate,
enddate=enddate, poolsize=poolsize, lang=lang)
else:
all_tweets = query_tweets_serial(query, limit=limit, begindate=begindate,
enddate=enddate, lang=lang)
return all_tweets

def query_tweets_serial(query, limit=None, begindate=dt.date(2006, 3, 21),
enddate=dt.date.today(), lang=''):

query = F'{query} since:{begindate} until:{enddate}'

logger.info(F'query: {query}')
all_tweets = query_tweets_once(query, limit=limit, lang=lang)
logger.info(F'Got {len(all_tweets)} tweets.')

return all_tweets

def query_tweets_parallel(query, limit=None, begindate=dt.date(2006, 3, 21),
enddate=dt.date.today(), poolsize=20, lang=''):
number_days = (enddate - begindate).days

if poolsize > no_days:
if poolsize > number_days:
# Since we are assigning each pool a range of dates to query,
# the number of pools should not exceed the number of dates.
poolsize = no_days
dateranges = [begindate + dt.timedelta(days=elem) for elem in linspace(0, no_days, poolsize+1)]
# the number of pools should not exceed the number of dates.
poolsize = number_days

dateranges = [begindate + dt.timedelta(days=elem) for elem in linspace(0, number_days, poolsize+1)]

if limit and poolsize:
limit_per_pool = (limit // poolsize)+1
Expand Down