Skip to content

Instantly share code, notes, and snippets.

@SkamDart
Forked from jimmc2/twitterurl.py
Last active January 8, 2018 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SkamDart/3ee5fe2c1f21edaac162c3d9848f1972 to your computer and use it in GitHub Desktop.
Save SkamDart/3ee5fe2c1f21edaac162c3d9848f1972 to your computer and use it in GitHub Desktop.
## Import libraries and functions needed
## define URL
def get_url(All=None, Exact=None, Any=None, Not=None, Hashtag=None, From=None,
To=None, Mention=None, Near=None, Dates=None):
#Base URL
url = "https://twitter.com/search?l=&q="
# All
if All != None:
a = All.replace(" ", "%20")
url = url + a
#Exact
if Exact != None:
b = Exact.replace(" ", "%20")
url = url + "%22" + b + "%22"
#Any
if Any != None:
c = Any.replace(" ", "%20OR%20")
url = url + "%22" + c + "%22"
#Not
if Not != None:
d = Not.replace(" ", "%20-")
url = url + "%20-" + d
#Hashtag
if Hashtag != None:
e = Hashtag.replace(" ", "%20OR%20%23")
url = url + "%23" + e
#From
if Hashtag != None:
f = Hashtag.replace(" ", "%20OR%20%23")
url = url + "%23" + f
#To
#Mention
#Near
#Dates
#Base URL end
url = url + "&src=typd"
return(url)
## Scrape URL
#print(get_url())
def create_url(*args, **kwargs):
"""
*args is an array of arguments ignore this for right now
**kwargs is dictionary "key-value" mapping of keywords and their arguments
in this case, you can pass any arbitrary amount of named arguments to your parameters
and iterate through them. I'll explain this a little more when I start writing code
"""
# https://www.tutorialspoint.com/python/dictionary_items.htm
"""
if you called, create_url(near="im", dates="jim")
this for loop would print
near -> im
dates -> jim
"""
print(args)
print(kwargs)
for key, value in kwargs.items():
# here is where you add the checks that you have above!
# for right now i'm just printing the key and it's value!
# this is also just a fancy way to print things nicely
# every {} in the string is replaced with the parameter in the format argument
print("{} -> {}".format(key, value))
"""
If you run `python twitterurl.py` this function below is what python looks for and executes
"""
if __name__ == '__main__':
"""
Look at what my code says and see if you can figure out what the commented out
function calls will print
Then uncomment and see if you were right
"""
# create_url(im='jim')
"""
see what you can do now is
"""
# create_url(hashtag="mydude")
"""
a little more advanced would be to make it so you can call this from the command line
http://www.pythonforbeginners.com/system/python-sys-argv
python twitterurl.py boner soup
for arg in argv:
print(arg)
twitterurl.py
boner
soup
"""
from sys import argv
# create an empty dictionary
tags = {}
# iterate through all
for arg in argv[1:]:
# assumes input looks like
# name=cam
# splits a string into a name and a value by the single equals sign
name, value = arg.split("=", 1)
# creates a dictionary with single item
tag = {
name: value
}
# adds that tag to the rest of our tags
tags.update(tag)
"""
https://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/
python twitterurl.py any=yes from=2017 to=2018
(I just made up that call so don't blame me if it doesn' twork)
"""
#create_url(**tags)
# Create csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment